# Generated migration for Stage 1 - Task, Content, ContentTaxonomy models refactor
# 
# Tasks: Remove cluster_role, add content_type, content_structure, taxonomy_term_id, simplify status
# Content: Remove 25+ fields, add title, content_html, simplify M2M
# ContentTaxonomy: Remove sync_status, description, parent, count, metadata, add 'cluster' type

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

    dependencies = [
        ('content', '0001_initial'),  # Adjust to your actual last migration
        ('planning', '0002_stage1_remove_cluster_context_fields'),
    ]

    operations = [
        # ============================================================
        # Tasks Model Changes
        # ============================================================
        
        # Remove deprecated fields from Tasks
        migrations.RemoveField(
            model_name='tasks',
            name='cluster_role',
        ),
        migrations.RemoveField(
            model_name='tasks',
            name='idea_id',
        ),
        migrations.RemoveField(
            model_name='tasks',
            name='content_record',
        ),
        migrations.RemoveField(
            model_name='tasks',
            name='entity_type',
        ),
        migrations.RemoveField(
            model_name='tasks',
            name='cluster_context',
        ),
        migrations.RemoveField(
            model_name='tasks',
            name='dimension_roles',
        ),
        migrations.RemoveField(
            model_name='tasks',
            name='metadata',
        ),
        
        # Add new fields to Tasks
        migrations.AddField(
            model_name='tasks',
            name='content_type',
            field=models.CharField(
                max_length=50,
                blank=True,
                null=True,
                help_text='WordPress content type (post, page, product, etc.)'
            ),
        ),
        migrations.AddField(
            model_name='tasks',
            name='content_structure',
            field=models.TextField(
                blank=True,
                null=True,
                help_text='JSON structure template for content generation'
            ),
        ),
        migrations.AddField(
            model_name='tasks',
            name='taxonomy_term_id',
            field=models.IntegerField(
                blank=True,
                null=True,
                help_text='Optional taxonomy term for categorization'
            ),
        ),
        
        # Update status field choices for Tasks
        migrations.AlterField(
            model_name='tasks',
            name='status',
            field=models.CharField(
                max_length=20,
                default='queued',
                choices=[
                    ('queued', 'Queued'),
                    ('completed', 'Completed'),
                ],
            ),
        ),
        
        # ============================================================
        # Content Model Changes
        # ============================================================
        
        # Remove deprecated fields from Content
        migrations.RemoveField(
            model_name='content',
            name='task',
        ),
        migrations.RemoveField(
            model_name='content',
            name='html_content',
        ),
        migrations.RemoveField(
            model_name='content',
            name='word_count',
        ),
        migrations.RemoveField(
            model_name='content',
            name='metadata',
        ),
        migrations.RemoveField(
            model_name='content',
            name='meta_title',
        ),
        migrations.RemoveField(
            model_name='content',
            name='meta_description',
        ),
        migrations.RemoveField(
            model_name='content',
            name='primary_keyword',
        ),
        migrations.RemoveField(
            model_name='content',
            name='secondary_keywords',
        ),
        migrations.RemoveField(
            model_name='content',
            name='entity_type',
        ),
        migrations.RemoveField(
            model_name='content',
            name='json_blocks',
        ),
        migrations.RemoveField(
            model_name='content',
            name='structure_data',
        ),
        migrations.RemoveField(
            model_name='content',
            name='content_format',
        ),
        migrations.RemoveField(
            model_name='content',
            name='cluster_role',
        ),
        migrations.RemoveField(
            model_name='content',
            name='sync_status',
        ),
        migrations.RemoveField(
            model_name='content',
            name='external_type',
        ),
        migrations.RemoveField(
            model_name='content',
            name='external_status',
        ),
        migrations.RemoveField(
            model_name='content',
            name='sync_data',
        ),
        migrations.RemoveField(
            model_name='content',
            name='last_synced_at',
        ),
        migrations.RemoveField(
            model_name='content',
            name='validation_errors',
        ),
        migrations.RemoveField(
            model_name='content',
            name='is_validated',
        ),
        
        # Rename generated_at to created_at for consistency
        migrations.RenameField(
            model_name='content',
            old_name='generated_at',
            new_name='created_at',
        ),
        
        # Add new fields to Content
        migrations.AddField(
            model_name='content',
            name='title',
            field=models.CharField(max_length=500, blank=True, null=True),
        ),
        migrations.AddField(
            model_name='content',
            name='content_html',
            field=models.TextField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name='content',
            name='cluster_id',
            field=models.IntegerField(blank=True, null=True),
        ),
        migrations.AddField(
            model_name='content',
            name='content_type',
            field=models.CharField(max_length=50, blank=True, null=True),
        ),
        migrations.AddField(
            model_name='content',
            name='content_structure',
            field=models.TextField(blank=True, null=True),
        ),
        
        # Update status field choices for Content
        migrations.AlterField(
            model_name='content',
            name='status',
            field=models.CharField(
                max_length=20,
                default='draft',
                choices=[
                    ('draft', 'Draft'),
                    ('published', 'Published'),
                ],
            ),
        ),
        
        # Replace through model with direct M2M for taxonomy_terms
        migrations.AddField(
            model_name='content',
            name='taxonomy_terms',
            field=models.ManyToManyField(
                to='content.ContentTaxonomy',
                related_name='contents',
                blank=True,
            ),
        ),
        
        # ============================================================
        # ContentTaxonomy Model Changes
        # ============================================================
        
        # Remove deprecated fields from ContentTaxonomy
        migrations.RemoveField(
            model_name='contenttaxonomy',
            name='description',
        ),
        migrations.RemoveField(
            model_name='contenttaxonomy',
            name='parent',
        ),
        migrations.RemoveField(
            model_name='contenttaxonomy',
            name='sync_status',
        ),
        migrations.RemoveField(
            model_name='contenttaxonomy',
            name='count',
        ),
        migrations.RemoveField(
            model_name='contenttaxonomy',
            name='metadata',
        ),
        migrations.RemoveField(
            model_name='contenttaxonomy',
            name='clusters',
        ),
        
        # Update taxonomy_type to include 'cluster'
        migrations.AlterField(
            model_name='contenttaxonomy',
            name='taxonomy_type',
            field=models.CharField(
                max_length=50,
                default='category',
                choices=[
                    ('category', 'Category'),
                    ('post_tag', 'Tag'),
                    ('cluster', 'Cluster'),
                ],
            ),
        ),
        
        # ============================================================
        # Remove Through Models and Relations
        # ============================================================
        
        # Delete ContentTaxonomyRelation through model (if exists)
        migrations.DeleteModel(
            name='ContentTaxonomyRelation',
        ),
        
        # Delete ContentClusterMap through model (if exists)
        migrations.DeleteModel(
            name='ContentClusterMap',
        ),
        
        # Delete ContentTaxonomyMap through model (if exists)
        migrations.DeleteModel(
            name='ContentTaxonomyMap',
        ),
        
        # Delete ContentAttribute model (if exists)
        migrations.DeleteModel(
            name='ContentAttribute',
        ),
    ]
