# Generated by Django 5.2 on 2026-01-29 17:46

import django.core.validators
import django.db.models.deletion
import uuid
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models


def create_default_referral_incentive_config(apps, schema_editor):
    ReferralIncentiveConfig = apps.get_model('api', 'ReferralIncentiveConfig')
    ReferralIncentiveConfig.objects.get_or_create(singleton_id=1)


class Migration(migrations.Migration):

    dependencies = [
        ('api', '0274_add_sandbox_compute_flag'),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name='ReferralIncentiveConfig',
            fields=[
                ('singleton_id', models.PositiveSmallIntegerField(default=1, editable=False, primary_key=True, serialize=False)),
                ('referrer_direct_credits', models.DecimalField(decimal_places=3, default=Decimal('100'), help_text='Credits granted to the referrer for direct account referrals.', max_digits=12, validators=[django.core.validators.MinValueValidator(Decimal('0'))])),
                ('referred_direct_credits', models.DecimalField(decimal_places=3, default=Decimal('100'), help_text='Credits granted to the referred user for direct account referrals.', max_digits=12, validators=[django.core.validators.MinValueValidator(Decimal('0'))])),
                ('referrer_template_credits', models.DecimalField(decimal_places=3, default=Decimal('100'), help_text='Credits granted to the referrer for shared template referrals.', max_digits=12, validators=[django.core.validators.MinValueValidator(Decimal('0'))])),
                ('referred_template_credits', models.DecimalField(decimal_places=3, default=Decimal('100'), help_text='Credits granted to the referred user for shared template referrals.', max_digits=12, validators=[django.core.validators.MinValueValidator(Decimal('0'))])),
                ('direct_referral_cap', models.PositiveIntegerField(default=25, help_text='Lifetime cap on the number of direct referral grants per referrer.')),
                ('template_referral_cap', models.PositiveIntegerField(default=25, help_text='Lifetime cap on the number of template referral grants per referrer.')),
                ('expiration_days', models.PositiveIntegerField(default=30, help_text='Number of days before referral credits expire.')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('updated_at', models.DateTimeField(auto_now=True)),
            ],
            options={
                'verbose_name': 'Referral incentive configuration',
                'verbose_name_plural': 'Referral incentive configuration',
            },
        ),
        migrations.RunPython(create_default_referral_incentive_config, migrations.RunPython.noop),
        migrations.AlterField(
            model_name='taskcredit',
            name='grant_type',
            field=models.CharField(choices=[('Plan', 'Plan'), ('Compensation', 'Compensation'), ('Promo', 'Promo'), ('task_pack', 'Task Pack'), ('referral', 'Referral'), ('referral_shared', 'Referral (Shared Agent)'), ('referral_redeemed', 'Referral (Referred)'), ('referral_shared_redeemed', 'Referral (Shared Agent - Referred)')], default='Plan', help_text='Type of grant for these credits (e.g., PLAN, COMPENSATION, PROMO, TASK_PACK)', max_length=32),
        ),
        migrations.CreateModel(
            name='ReferralGrant',
            fields=[
                ('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
                ('referral_type', models.CharField(choices=[('direct', 'Direct'), ('template', 'Template')], help_text='Referral source type (direct or template).', max_length=16)),
                ('template_code', models.CharField(blank=True, default='', help_text='Template code if this was a shared-template referral.', max_length=64)),
                ('granted_at', models.DateTimeField(help_text='When the referral grant was processed.')),
                ('config_snapshot', models.JSONField(blank=True, default=dict, help_text='Referral incentive configuration snapshot used for this grant.')),
                ('created_at', models.DateTimeField(auto_now_add=True)),
                ('referred', models.OneToOneField(help_text='User who received the referral incentive.', on_delete=django.db.models.deletion.CASCADE, related_name='referral_grant', to=settings.AUTH_USER_MODEL)),
                ('referred_task_credit', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referred_referral_grants', to='api.taskcredit')),
                ('referrer', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referral_grants_made', to=settings.AUTH_USER_MODEL)),
                ('referrer_task_credit', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='referrer_referral_grants', to='api.taskcredit')),
            ],
            options={
                'verbose_name': 'Referral grant',
                'verbose_name_plural': 'Referral grants',
                'ordering': ['-granted_at'],
            },
        ),
    ]
