# Generated by Django 5.2.15 on 2026-08-05 05:22

from django.db import migrations, models


def mark_existing_submissions_as_legacy(apps, schema_editor):
    ContactFormSubmission = apps.get_model("base", "ContactFormSubmission")
    ContactFormSubmission.objects.update(processing_status="legacy")


class Migration(migrations.Migration):

    dependencies = [
        ('base', '0013_send_engine_oauth_client_credentials'),
    ]

    operations = [
        migrations.CreateModel(
            name='ContactFormRateLimitBucket',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('ip_address', models.GenericIPAddressField(unique=True, verbose_name='IP Address')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated At')),
            ],
            options={
                'verbose_name': 'Contact Form Rate Limit Bucket',
                'verbose_name_plural': 'Contact Form Rate Limit Buckets',
            },
        ),
        migrations.CreateModel(
            name='ContactFormSettings',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('rate_limit_enabled', models.BooleanField(default=True, verbose_name='Enable IP Rate Limit')),
                ('rate_limit_max_submissions', models.PositiveIntegerField(default=3, verbose_name='Maximum Submissions Per Window')),
                ('rate_limit_window_minutes', models.PositiveIntegerField(default=10, verbose_name='Rate Limit Window Minutes')),
                ('send_user_copy', models.BooleanField(default=False, help_text='Send a copy to the submitter only after spam detection allows the message.', verbose_name='Send User Copy')),
            ],
            options={
                'verbose_name': 'Contact Form Settings',
            },
        ),
        migrations.CreateModel(
            name='ContactSpamDetectionState',
            fields=[
                ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('lock_token', models.CharField(blank=True, max_length=64, verbose_name='Lock Token')),
                ('locked_until', models.DateTimeField(blank=True, null=True, verbose_name='Locked Until')),
                ('cooldown_until', models.DateTimeField(blank=True, null=True, verbose_name='Cooldown Until')),
                ('last_error', models.TextField(blank=True, verbose_name='Last Error')),
                ('updated_at', models.DateTimeField(auto_now=True, verbose_name='Updated At')),
            ],
            options={
                'verbose_name': 'Contact Spam Detection State',
                'verbose_name_plural': 'Contact Spam Detection State',
            },
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='last_error',
            field=models.TextField(blank=True, verbose_name='Last Error'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='next_attempt_at',
            field=models.DateTimeField(blank=True, db_index=True, null=True, verbose_name='Next Attempt At'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='notification_sent_at',
            field=models.DateTimeField(blank=True, null=True, verbose_name='Notification Sent At'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='processing_started_at',
            field=models.DateTimeField(blank=True, null=True, verbose_name='Processing Started At'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='processing_status',
            field=models.CharField(choices=[('queued', 'Queued'), ('scanning', 'Scanning'), ('scan_retry', 'Scan Retry'), ('blocked', 'Blocked as Spam'), ('sending', 'Sending'), ('send_retry', 'Send Retry'), ('sent', 'Sent'), ('failed', 'Failed'), ('rate_limited', 'Rate Limited'), ('legacy', 'Legacy Submission')], db_index=True, default='queued', max_length=20, verbose_name='Processing Status'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='scan_attempt_count',
            field=models.PositiveIntegerField(default=0, verbose_name='Scan Attempt Count'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='send_attempt_count',
            field=models.PositiveIntegerField(default=0, verbose_name='Send Attempt Count'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='spam_category',
            field=models.PositiveSmallIntegerField(blank=True, choices=[(0, 'Allowed'), (1, 'Unsolicited Advertising'), (2, 'Malicious'), (3, 'Meaningless')], null=True, verbose_name='Spam Category'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='spam_checked_at',
            field=models.DateTimeField(blank=True, null=True, verbose_name='Spam Checked At'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='spam_source',
            field=models.CharField(blank=True, max_length=32, verbose_name='Spam Detection Source'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='updated_at',
            field=models.DateTimeField(auto_now=True, verbose_name='Updated At'),
        ),
        migrations.AddField(
            model_name='contactformsubmission',
            name='user_copy_sent_at',
            field=models.DateTimeField(blank=True, null=True, verbose_name='User Copy Sent At'),
        ),
        migrations.AddIndex(
            model_name='contactformsubmission',
            index=models.Index(fields=['ip_address', 'created_at'], name='contact_ip_created_idx'),
        ),
        migrations.AddIndex(
            model_name='contactformsubmission',
            index=models.Index(fields=['processing_status', 'next_attempt_at', 'created_at'], name='contact_process_due_idx'),
        ),
        migrations.RunPython(mark_existing_submissions_as_legacy, migrations.RunPython.noop),
    ]
