# Generated manually for field rename implementation
from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ('planner', '0005_field_rename_implementation'),
        ('writer', '0007_alter_contenttaxonomyrelation_unique_together_and_more'),
    ]

    operations = [
        # Rename database columns for Tasks
        migrations.RunSQL(
            sql="""
                -- Rename Tasks columns (only if they exist)
                DO $$
                BEGIN
                    IF EXISTS (SELECT 1 FROM information_schema.columns 
                              WHERE table_name = 'igny8_tasks' AND column_name = 'entity_type') THEN
                        ALTER TABLE igny8_tasks RENAME COLUMN entity_type TO content_type;
                    END IF;
                    
                    IF EXISTS (SELECT 1 FROM information_schema.columns 
                              WHERE table_name = 'igny8_tasks' AND column_name = 'cluster_role') THEN
                        ALTER TABLE igny8_tasks RENAME COLUMN cluster_role TO content_structure;
                    END IF;
                END $$;
                
                -- Drop old indexes if they exist
                DROP INDEX IF EXISTS igny8_tasks_entity__1dc185_idx;
                DROP INDEX IF EXISTS igny8_tasks_cluster_c87903_idx;
                
                -- Create new indexes
                CREATE INDEX IF NOT EXISTS igny8_tasks_content_type_idx ON igny8_tasks(content_type);
                CREATE INDEX IF NOT EXISTS igny8_tasks_content_structure_idx ON igny8_tasks(content_structure);
            """,
            reverse_sql="""
                ALTER TABLE igny8_tasks RENAME COLUMN content_type TO entity_type;
                ALTER TABLE igny8_tasks RENAME COLUMN content_structure TO cluster_role;
                DROP INDEX IF EXISTS igny8_tasks_content_type_idx;
                DROP INDEX IF EXISTS igny8_tasks_content_structure_idx;
                CREATE INDEX igny8_tasks_entity__1dc185_idx ON igny8_tasks(entity_type);
                CREATE INDEX igny8_tasks_cluster_c87903_idx ON igny8_tasks(cluster_role);
            """
        ),
        
        # Rename database columns for Content
        migrations.RunSQL(
            sql="""
                -- Rename Content columns (only if they exist)
                DO $$
                BEGIN
                    IF EXISTS (SELECT 1 FROM information_schema.columns 
                              WHERE table_name = 'igny8_content' AND column_name = 'entity_type') THEN
                        ALTER TABLE igny8_content RENAME COLUMN entity_type TO content_type;
                    END IF;
                    
                    IF EXISTS (SELECT 1 FROM information_schema.columns 
                              WHERE table_name = 'igny8_content' AND column_name = 'cluster_role') THEN
                        ALTER TABLE igny8_content RENAME COLUMN cluster_role TO content_structure;
                    END IF;
                    
                    IF EXISTS (SELECT 1 FROM information_schema.columns 
                              WHERE table_name = 'igny8_content' AND column_name = 'html_content') THEN
                        ALTER TABLE igny8_content RENAME COLUMN html_content TO content_html;
                    END IF;
                END $$;
                
                -- Drop old indexes if they exist
                DROP INDEX IF EXISTS igny8_conte_entity__f559b3_idx;
                DROP INDEX IF EXISTS igny8_conte_cluster_32e22a_idx;
                
                -- Create new indexes
                CREATE INDEX IF NOT EXISTS igny8_content_content_type_idx ON igny8_content(content_type);
                CREATE INDEX IF NOT EXISTS igny8_content_content_structure_idx ON igny8_content(content_structure);
            """,
            reverse_sql="""
                ALTER TABLE igny8_content RENAME COLUMN content_type TO entity_type;
                ALTER TABLE igny8_content RENAME COLUMN content_structure TO cluster_role;
                ALTER TABLE igny8_content RENAME COLUMN content_html TO html_content;
                DROP INDEX IF EXISTS igny8_content_content_type_idx;
                DROP INDEX IF EXISTS igny8_content_content_structure_idx;
                CREATE INDEX igny8_conte_entity__f559b3_idx ON igny8_content(entity_type);
                CREATE INDEX igny8_conte_cluster_32e22a_idx ON igny8_content(cluster_role);
            """
        ),
        
        # Rename database columns for ContentTaxonomyMap
        migrations.RunSQL(
            sql="""
                DO $$
                BEGIN
                    IF EXISTS (SELECT 1 FROM information_schema.columns 
                              WHERE table_name = 'igny8_content_taxonomy_map' AND column_name = 'entity_type') THEN
                        ALTER TABLE igny8_content_taxonomy_map RENAME COLUMN entity_type TO content_type;
                    END IF;
                END $$;
            """,
            reverse_sql="""
                ALTER TABLE igny8_content_taxonomy_map RENAME COLUMN content_type TO entity_type;
            """
        ),
        
        # Rename database columns for TaxonomyTerms
        migrations.RunSQL(
            sql="""
                DO $$
                BEGIN
                    IF EXISTS (SELECT 1 FROM information_schema.columns 
                              WHERE table_name = 'igny8_taxonomy_terms' AND column_name = 'entity_type') THEN
                        ALTER TABLE igny8_taxonomy_terms RENAME COLUMN entity_type TO content_type;
                    END IF;
                END $$;
            """,
            reverse_sql="""
                ALTER TABLE igny8_taxonomy_terms RENAME COLUMN content_type TO entity_type;
            """
        ),
    ]
