# Generated by Django 5.2.11

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("annotations", "0061_add_vector_2048"),
    ]

    operations = [
        # Relationship model constraints
        migrations.RemoveConstraint(
            model_name="relationship",
            name="relationship_created_by_only_one_source",
        ),
        migrations.AddConstraint(
            model_name="relationship",
            constraint=models.CheckConstraint(
                condition=models.Q(
                    ("created_by_analysis__isnull", True),
                    ("created_by_extract__isnull", True),
                    _connector="OR",
                ),
                name="relationship_created_by_only_one_source",
                violation_error_message="A relationship cannot be created by both an analysis and an extract",
            ),
        ),
        migrations.RemoveConstraint(
            model_name="relationship",
            name="relationship_has_single_parent",
        ),
        migrations.AddConstraint(
            model_name="relationship",
            constraint=models.CheckConstraint(
                condition=models.Q(
                    models.Q(
                        ("document__isnull", False), ("structural_set__isnull", True)
                    ),
                    models.Q(
                        ("document__isnull", True), ("structural_set__isnull", False)
                    ),
                    _connector="OR",
                ),
                name="relationship_has_single_parent",
                violation_error_message="A relationship must belong to either a document or a structural set, not both or neither",
            ),
        ),
        migrations.RemoveConstraint(
            model_name="relationship",
            name="rel_structural_set_requires_structural_flag",
        ),
        migrations.AddConstraint(
            model_name="relationship",
            constraint=models.CheckConstraint(
                condition=(
                    models.Q(structural_set__isnull=True)
                    | models.Q(structural=True)
                ),
                name="rel_structural_set_requires_structural_flag",
                violation_error_message="Relationships in a structural_set must have structural=True",
            ),
        ),
        # Annotation model constraints
        migrations.RemoveConstraint(
            model_name="annotation",
            name="annotation_created_by_only_one_source",
        ),
        migrations.AddConstraint(
            model_name="annotation",
            constraint=models.CheckConstraint(
                condition=models.Q(
                    ("created_by_analysis__isnull", True),
                    ("created_by_extract__isnull", True),
                    _connector="OR",
                ),
                name="annotation_created_by_only_one_source",
                violation_error_message="An annotation cannot be created by both an analysis and an extract",
            ),
        ),
        migrations.RemoveConstraint(
            model_name="annotation",
            name="annotation_has_single_parent",
        ),
        migrations.AddConstraint(
            model_name="annotation",
            constraint=models.CheckConstraint(
                condition=models.Q(
                    models.Q(
                        ("document__isnull", False), ("structural_set__isnull", True)
                    ),
                    models.Q(
                        ("document__isnull", True), ("structural_set__isnull", False)
                    ),
                    _connector="OR",
                ),
                name="annotation_has_single_parent",
                violation_error_message="An annotation must belong to either a document or a structural set, not both or neither",
            ),
        ),
        migrations.RemoveConstraint(
            model_name="annotation",
            name="structural_set_requires_structural_flag",
        ),
        migrations.AddConstraint(
            model_name="annotation",
            constraint=models.CheckConstraint(
                condition=(
                    models.Q(structural_set__isnull=True)
                    | models.Q(structural=True)
                ),
                name="structural_set_requires_structural_flag",
                violation_error_message="Annotations in a structural_set must have structural=True",
            ),
        ),
    ]
