# Generated migration for unified status refactor

from django.db import migrations, models


def migrate_status_data(apps, schema_editor):
    """Transform existing status data to new values"""
    Keywords = apps.get_model('planner', 'Keywords')
    Clusters = apps.get_model('planner', 'Clusters')
    ContentIdeas = apps.get_model('planner', 'ContentIdeas')
    
    # Keywords: pending→new, active→mapped, archived→mapped+disabled=true
    Keywords.objects.filter(status='pending').update(status='new')
    Keywords.objects.filter(status='active').update(status='mapped')
    # Handle archived: set to mapped and mark as disabled
    archived_keywords = Keywords.objects.filter(status='archived')
    for kw in archived_keywords:
        kw.status = 'mapped'
        kw.disabled = True
        kw.save()
    
    # Clusters: active (with ideas)→mapped, active (no ideas)→new
    # Check if cluster has any related ideas using the reverse relationship
    for cluster in Clusters.objects.all():
        if cluster.ideas.exists():
            cluster.status = 'mapped'
        else:
            cluster.status = 'new'
        cluster.save()
    
    # ContentIdeas: scheduled→queued, published→completed, new stays new
    ContentIdeas.objects.filter(status='scheduled').update(status='queued')
    ContentIdeas.objects.filter(status='published').update(status='completed')


def reverse_status_data(apps, schema_editor):
    """Reverse migration: restore old status values"""
    Keywords = apps.get_model('planner', 'Keywords')
    Clusters = apps.get_model('planner', 'Clusters')
    ContentIdeas = apps.get_model('planner', 'ContentIdeas')
    
    # Keywords: new→pending, mapped→active (or archived if disabled)
    Keywords.objects.filter(status='new').update(status='pending')
    Keywords.objects.filter(status='mapped', disabled=False).update(status='active')
    Keywords.objects.filter(status='mapped', disabled=True).update(status='archived', disabled=False)
    
    # Clusters: all back to 'active'
    Clusters.objects.all().update(status='active')
    
    # ContentIdeas: queued→scheduled, completed→published
    ContentIdeas.objects.filter(status='queued').update(status='scheduled')
    ContentIdeas.objects.filter(status='completed').update(status='published')


class Migration(migrations.Migration):

    dependencies = [
        ('planner', '0005_field_rename_implementation'),
    ]

    operations = [
        # Step 1: Add disabled field to all models (with default=False)
        migrations.AddField(
            model_name='clusters',
            name='disabled',
            field=models.BooleanField(default=False, help_text='Exclude from processes'),
        ),
        migrations.AddField(
            model_name='keywords',
            name='disabled',
            field=models.BooleanField(default=False, help_text='Exclude from processes'),
        ),
        migrations.AddField(
            model_name='contentideas',
            name='disabled',
            field=models.BooleanField(default=False, help_text='Exclude from processes'),
        ),
        
        # Step 2: Alter Keywords status field choices
        migrations.AlterField(
            model_name='keywords',
            name='status',
            field=models.CharField(
                choices=[('new', 'New'), ('mapped', 'Mapped')],
                default='new',
                max_length=50
            ),
        ),
        
        # Step 3: Alter Clusters status field (add choices, change default)
        migrations.AlterField(
            model_name='clusters',
            name='status',
            field=models.CharField(
                choices=[('new', 'New'), ('mapped', 'Mapped')],
                default='new',
                max_length=50
            ),
        ),
        
        # Step 4: Alter ContentIdeas status field choices
        migrations.AlterField(
            model_name='contentideas',
            name='status',
            field=models.CharField(
                choices=[('new', 'New'), ('queued', 'Queued'), ('completed', 'Completed')],
                default='new',
                max_length=50
            ),
        ),
        
        # Step 5: Data migration - transform existing records
        migrations.RunPython(migrate_status_data, reverse_status_data),
    ]
