# Generated by hand for adding organization ownership to TaskCredit
from django.db import migrations, models
from django.conf import settings
import django.db.models.deletion


def noop_forward(apps, schema_editor):
    # No data migration needed; existing rows remain user-owned
    pass


def noop_reverse(apps, schema_editor):
    pass


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0095_alter_persistentagent_unique_owner_constraints'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.AddField(
            model_name='taskcredit',
            name='organization',
            field=models.ForeignKey(blank=True, help_text='Exactly one of user or organization must be set.', null=True, on_delete=django.db.models.deletion.CASCADE, related_name='task_credits', to='api.organization'),
        ),
        migrations.AlterField(
            model_name='taskcredit',
            name='user',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='task_credits', to=settings.AUTH_USER_MODEL),
        ),
        migrations.AddConstraint(
            model_name='taskcredit',
            constraint=models.UniqueConstraint(
                fields=('organization', 'plan', 'grant_month'),
                condition=models.Q(('plan', 'free'), ('grant_type', 'Plan'), ('additional_task', False), ('voided', False)),
                name='uniq_free_plan_block_per_month_org',
            ),
        ),
        migrations.AddConstraint(
            model_name='taskcredit',
            constraint=models.CheckConstraint(
                condition=(
                    models.Q(('user__isnull', False), ('organization__isnull', True)) |
                    models.Q(('user__isnull', True), ('organization__isnull', False))
                ),
                name='taskcredit_owner_xor_user_org',
            ),
        ),
        migrations.RunPython(noop_forward, noop_reverse),
    ]
