# Generated by Django 4.2.28 on 2026-04-09 06:39

import django.db.models.constraints
import django.db.models.deletion
from django.conf import settings
from django.db import migrations, models

import debusine.artifacts.models


class Migration(migrations.Migration):
    dependencies = [
        ('db', '0050_alter_token_token_type_and_more'),
    ]

    operations = [
        migrations.CreateModel(
            name='CollectionRelation',
            fields=[
                (
                    'id',
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name='ID',
                    ),
                ),
                ('source_category', models.CharField(max_length=255)),
                ('target_category', models.CharField(max_length=255)),
                (
                    'type',
                    models.CharField(
                        choices=[
                            ('suite-forked-from', 'Forked from'),
                            ('suite-based-on', 'Based on'),
                            ('suite-requires', 'Requires'),
                            ('suite-targeting', 'Targeting'),
                            ('suite-default-qa-results', 'Default QA Results'),
                        ],
                        max_length=32,
                    ),
                ),
                (
                    'position',
                    models.PositiveIntegerField(blank=True, null=True),
                ),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(blank=True, null=True)),
                (
                    'created_by_user',
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name='created_collection_relations',
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    'source',
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name='relations',
                        to='db.collection',
                    ),
                ),
                (
                    'target',
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name='targeted_by',
                        to='db.collection',
                    ),
                ),
                (
                    'updated_by_user',
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name='updated_collection_relations',
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                'base_manager_name': 'objects',
            },
        ),
        migrations.AddConstraint(
            model_name='collectionrelation',
            constraint=models.CheckConstraint(
                check=models.Q(
                    models.Q(
                        ('position__isnull', False), ('type', 'suite-requires')
                    ),
                    models.Q(
                        ('position__isnull', True),
                        (
                            'type__in',
                            [
                                'suite-forked-from',
                                'suite-based-on',
                                'suite-targeting',
                                'suite-default-qa-results',
                            ],
                        ),
                    ),
                    _connector='OR',
                ),
                name='db_collectionrelation_position',
            ),
        ),
        migrations.AddConstraint(
            model_name='collectionrelation',
            constraint=models.UniqueConstraint(
                deferrable=django.db.models.constraints.Deferrable['DEFERRED'],
                fields=('source', 'type', 'position'),
                name='db_collectionrelation_suite_requires_no_duplicate_position',
            ),
        ),
        migrations.AddConstraint(
            model_name='collectionrelation',
            constraint=models.UniqueConstraint(
                condition=models.Q(
                    ('type', 'suite-forked-from'),
                    ('type', 'suite-based-on'),
                    ('type', 'suite-targeting'),
                    ('type', 'suite-default-qa-results'),
                    _connector='OR',
                ),
                fields=('source', 'type'),
                name='db_collectionrelation_cardinality_checks_max_1',
            ),
        ),
        migrations.AddConstraint(
            model_name='collectionrelation',
            constraint=models.UniqueConstraint(
                condition=models.Q(('type', 'suite-requires')),
                fields=('source', 'type', 'target'),
                name='db_collectionrelation_suite_requires_no_duplicate_target',
            ),
        ),
        migrations.AddConstraint(
            model_name='collectionrelation',
            constraint=models.CheckConstraint(
                check=models.Q(
                    models.Q(
                        (
                            'source_category',
                            debusine.artifacts.models.CollectionCategory[
                                'SUITE'
                            ],
                        ),
                        (
                            'target_category',
                            debusine.artifacts.models.CollectionCategory[
                                'SUITE'
                            ],
                        ),
                        ('type', 'suite-forked-from'),
                    ),
                    models.Q(
                        (
                            'source_category',
                            debusine.artifacts.models.CollectionCategory[
                                'SUITE'
                            ],
                        ),
                        (
                            'target_category',
                            debusine.artifacts.models.CollectionCategory[
                                'SUITE'
                            ],
                        ),
                        ('type', 'suite-based-on'),
                    ),
                    models.Q(
                        (
                            'source_category',
                            debusine.artifacts.models.CollectionCategory[
                                'SUITE'
                            ],
                        ),
                        (
                            'target_category',
                            debusine.artifacts.models.CollectionCategory[
                                'SUITE'
                            ],
                        ),
                        ('type', 'suite-requires'),
                    ),
                    models.Q(
                        (
                            'source_category',
                            debusine.artifacts.models.CollectionCategory[
                                'SUITE'
                            ],
                        ),
                        (
                            'target_category',
                            debusine.artifacts.models.CollectionCategory[
                                'SUITE'
                            ],
                        ),
                        ('type', 'suite-targeting'),
                    ),
                    models.Q(
                        (
                            'source_category',
                            debusine.artifacts.models.CollectionCategory[
                                'SUITE'
                            ],
                        ),
                        (
                            'target_category',
                            debusine.artifacts.models.CollectionCategory[
                                'QA_RESULTS'
                            ],
                        ),
                        ('type', 'suite-default-qa-results'),
                    ),
                    _connector='OR',
                ),
                name='db_collectionrelation_categories_correct',
            ),
        ),
    ]
