# Generated by Django 4.2.23 on 2025-09-22 04:40

from django.db import migrations


class Migration(migrations.Migration):

    dependencies = [
        ("annotations", "0037_add_relationship_performance_indexes"),
    ]

    operations = [
        # Annotation Indexes - Core query patterns
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_annotation_doc_corpus_structural "
            "ON annotations_annotation(document_id, corpus_id, structural) "
            "WHERE structural = FALSE;",
            reverse_sql="DROP INDEX IF EXISTS idx_annotation_doc_corpus_structural;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_annotation_doc_corpus_page "
            "ON annotations_annotation(document_id, corpus_id, page);",
            reverse_sql="DROP INDEX IF EXISTS idx_annotation_doc_corpus_page;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_annotation_doc_corpus_creator "
            "ON annotations_annotation(document_id, corpus_id, creator_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_annotation_doc_corpus_creator;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_annotation_doc_page "
            "ON annotations_annotation(document_id, page);",
            reverse_sql="DROP INDEX IF EXISTS idx_annotation_doc_page;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_annotation_corpus_creator "
            "ON annotations_annotation(corpus_id, creator_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_annotation_corpus_creator;",
        ),
        # Analysis patterns
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_annotation_analysis_doc "
            "ON annotations_annotation(analysis_id, document_id) "
            "WHERE analysis_id IS NOT NULL;",
            reverse_sql="DROP INDEX IF EXISTS idx_annotation_analysis_doc;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_annotation_analysis_corpus "
            "ON annotations_annotation(analysis_id, corpus_id) "
            "WHERE analysis_id IS NOT NULL;",
            reverse_sql="DROP INDEX IF EXISTS idx_annotation_analysis_corpus;",
        ),
        # Permission patterns
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_annotation_public "
            "ON annotations_annotation(corpus_id, document_id, is_public) "
            "WHERE is_public = TRUE;",
            reverse_sql="DROP INDEX IF EXISTS idx_annotation_public;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_annotation_structural "
            "ON annotations_annotation(document_id, structural) "
            "WHERE structural = TRUE;",
            reverse_sql="DROP INDEX IF EXISTS idx_annotation_structural;",
        ),
        # Label filtering
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_annotation_label "
            "ON annotations_annotation(annotation_label_id, document_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_annotation_label;",
        ),
        # Relationship Indexes - Core patterns
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_relationship_doc_corpus "
            "ON annotations_relationship(document_id, corpus_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_relationship_doc_corpus;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_relationship_doc_corpus_creator "
            "ON annotations_relationship(document_id, corpus_id, creator_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_relationship_doc_corpus_creator;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_relationship_structural "
            "ON annotations_relationship(document_id, structural) "
            "WHERE structural = TRUE;",
            reverse_sql="DROP INDEX IF EXISTS idx_relationship_structural;",
        ),
        # Relationship Analysis patterns
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_relationship_analysis "
            "ON annotations_relationship(analysis_id, document_id) "
            "WHERE analysis_id IS NOT NULL;",
            reverse_sql="DROP INDEX IF EXISTS idx_relationship_analysis;",
        ),
        # Relationship M2M optimization
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_rel_source_annotations "
            "ON annotations_relationship_source_annotations(relationship_id, annotation_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_rel_source_annotations;",
        ),
        migrations.RunSQL(
            "CREATE INDEX IF NOT EXISTS idx_rel_target_annotations "
            "ON annotations_relationship_target_annotations(relationship_id, annotation_id);",
            reverse_sql="DROP INDEX IF EXISTS idx_rel_target_annotations;",
        ),
    ]
