# Generated by Django 4.2.20 on 2025-08-23 18:35

from django.db import migrations


def sync_analyzer_input_schemas(apps, schema_editor):
    """
    Sync existing analyzers to populate input_schema field that was added in migration 0010.
    This ensures analyzers created before the field existed get their schemas populated.
    """
    from opencontractserver.analyzer.utils import auto_create_doc_analyzers
    
    Analyzer = apps.get_model("analyzer", "Analyzer")
    User = apps.get_model("users", "User")
    
    # Call with update_existing=True to update existing analyzers
    auto_create_doc_analyzers(
        AnalyzerModel=Analyzer,
        UserModel=User,
        fallback_superuser=True,
        update_existing=True,  # This is the key - update existing analyzers
    )


def reverse_sync(apps, schema_editor):
    """
    No-op reverse migration.
    We don't want to remove input_schema values on rollback.
    """
    pass


class Migration(migrations.Migration):

    dependencies = [
        ("analyzer", "0012_auto_sync_doc_analyzers"),
        ("users", "0015_user_slug"),  # Ensure user model is ready
    ]

    operations = [
        migrations.RunPython(sync_analyzer_input_schemas, reverse_sync),
    ]
