# Generated by Django 5.2.5 on 2025-08-29 14:55

import django.db.models.deletion
import pgtrigger.compiler
import pgtrigger.migrations
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        (
            "accounts",
            "0003_emailverificationevent_passwordresetevent_userevent_and_more",
        ),
        ("pghistory", "0006_delete_aggregateevent"),
    ]

    operations = [
        migrations.CreateModel(
            name="UserDeletionRequest",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "verification_code",
                    models.CharField(
                        help_text="Unique verification code sent to user's email",
                        max_length=32,
                        unique=True,
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "expires_at",
                    models.DateTimeField(help_text="When this deletion request expires"),
                ),
                (
                    "email_sent_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="When the verification email was sent",
                        null=True,
                    ),
                ),
                (
                    "attempts",
                    models.PositiveIntegerField(default=0, help_text="Number of verification attempts made"),
                ),
                (
                    "max_attempts",
                    models.PositiveIntegerField(
                        default=5,
                        help_text="Maximum number of verification attempts allowed",
                    ),
                ),
                (
                    "is_used",
                    models.BooleanField(
                        default=False,
                        help_text="Whether this deletion request has been used",
                    ),
                ),
                (
                    "user",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="deletion_request",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="UserDeletionRequestEvent",
            fields=[
                ("pgh_id", models.AutoField(primary_key=True, serialize=False)),
                ("pgh_created_at", models.DateTimeField(auto_now_add=True)),
                ("pgh_label", models.TextField(help_text="The event label.")),
                ("id", models.BigIntegerField()),
                (
                    "verification_code",
                    models.CharField(
                        help_text="Unique verification code sent to user's email",
                        max_length=32,
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "expires_at",
                    models.DateTimeField(help_text="When this deletion request expires"),
                ),
                (
                    "email_sent_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="When the verification email was sent",
                        null=True,
                    ),
                ),
                (
                    "attempts",
                    models.PositiveIntegerField(default=0, help_text="Number of verification attempts made"),
                ),
                (
                    "max_attempts",
                    models.PositiveIntegerField(
                        default=5,
                        help_text="Maximum number of verification attempts allowed",
                    ),
                ),
                (
                    "is_used",
                    models.BooleanField(
                        default=False,
                        help_text="Whether this deletion request has been used",
                    ),
                ),
                (
                    "pgh_context",
                    models.ForeignKey(
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to="pghistory.context",
                    ),
                ),
                (
                    "pgh_obj",
                    models.ForeignKey(
                        db_constraint=False,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="events",
                        to="accounts.userdeletionrequest",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        db_constraint=False,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        related_query_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.AddIndex(
            model_name="userdeletionrequest",
            index=models.Index(fields=["verification_code"], name="accounts_us_verific_94460d_idx"),
        ),
        migrations.AddIndex(
            model_name="userdeletionrequest",
            index=models.Index(fields=["expires_at"], name="accounts_us_expires_1d1dca_idx"),
        ),
        migrations.AddIndex(
            model_name="userdeletionrequest",
            index=models.Index(fields=["user", "is_used"], name="accounts_us_user_id_1ce18a_idx"),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="userdeletionrequest",
            trigger=pgtrigger.compiler.Trigger(
                name="insert_insert",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    func='INSERT INTO "accounts_userdeletionrequestevent" ("attempts", "created_at", "email_sent_at", "expires_at", "id", "is_used", "max_attempts", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "user_id", "verification_code") VALUES (NEW."attempts", NEW."created_at", NEW."email_sent_at", NEW."expires_at", NEW."id", NEW."is_used", NEW."max_attempts", _pgh_attach_context(), NOW(), \'insert\', NEW."id", NEW."user_id", NEW."verification_code"); RETURN NULL;',
                    hash="c1735fe8eb50247b0afe2bea9d32f83c31da6419",
                    operation="INSERT",
                    pgid="pgtrigger_insert_insert_b982c",
                    table="accounts_userdeletionrequest",
                    when="AFTER",
                ),
            ),
        ),
        pgtrigger.migrations.AddTrigger(
            model_name="userdeletionrequest",
            trigger=pgtrigger.compiler.Trigger(
                name="update_update",
                sql=pgtrigger.compiler.UpsertTriggerSql(
                    condition="WHEN (OLD.* IS DISTINCT FROM NEW.*)",
                    func='INSERT INTO "accounts_userdeletionrequestevent" ("attempts", "created_at", "email_sent_at", "expires_at", "id", "is_used", "max_attempts", "pgh_context_id", "pgh_created_at", "pgh_label", "pgh_obj_id", "user_id", "verification_code") VALUES (NEW."attempts", NEW."created_at", NEW."email_sent_at", NEW."expires_at", NEW."id", NEW."is_used", NEW."max_attempts", _pgh_attach_context(), NOW(), \'update\', NEW."id", NEW."user_id", NEW."verification_code"); RETURN NULL;',
                    hash="6bf807ce3bed069ab30462d3fd7688a7593a7fd0",
                    operation="UPDATE",
                    pgid="pgtrigger_update_update_27723",
                    table="accounts_userdeletionrequest",
                    when="AFTER",
                ),
            ),
        ),
    ]
