# Generated by Django 4.2.20 on 2025-08-20 22:24

from django.db import migrations


def sync_doc_analyzers(apps, schema_editor):
    """
    Automatically sync doc analyzer tasks on migration.
    This ensures all decorated tasks are available in the database.
    """
    try:
        from opencontractserver.analyzer.utils import auto_create_doc_analyzers
        
        Analyzer = apps.get_model("analyzer", "Analyzer")
        # Use the historical model from migrations instead of get_user_model()
        UserModel = apps.get_model("users", "User")
        
        auto_create_doc_analyzers(
            AnalyzerModel=Analyzer,
            UserModel=UserModel
        )
    except (ImportError, LookupError):
        # Skip if running in a context where celery tasks aren't available
        # or if models aren't ready yet
        pass


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


class Migration(migrations.Migration):

    dependencies = [
        ("analyzer", "0011_analysis_error_message_analysis_error_traceback_and_more"),
        ("users", "0015_user_slug"),  # Ensure user slug field exists
    ]

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