# Generated by Django 5.2.13 on 2026-05-14 15:39

import apps.mel.tracking.models
import django.db.models.deletion
import simple_history.models
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("mel_tracking", "0008_melconfiguration"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="ActivityAttendanceChannel",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "token",
                    models.CharField(
                        default=apps.mel.tracking.models._generate_attendance_token,
                        help_text="Opaque token used in the public scan URL.",
                        max_length=48,
                        unique=True,
                    ),
                ),
                ("is_active", models.BooleanField(db_index=True, default=True)),
                (
                    "opens_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="Optional gate — channel returns 410 before this time.",
                        null=True,
                    ),
                ),
                (
                    "closes_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="Optional gate — channel returns 410 after this time.",
                        null=True,
                    ),
                ),
                (
                    "require_email",
                    models.BooleanField(
                        default=False,
                        help_text="When True, the public form requires an email for self-identification.",
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "activity",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="attendance_channel",
                        to="mel_tracking.activity",
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="created_attendance_channels",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="HistoricalActivityAttendanceChannel",
            fields=[
                (
                    "id",
                    models.BigIntegerField(
                        auto_created=True, blank=True, db_index=True, verbose_name="ID"
                    ),
                ),
                (
                    "token",
                    models.CharField(
                        db_index=True,
                        default=apps.mel.tracking.models._generate_attendance_token,
                        help_text="Opaque token used in the public scan URL.",
                        max_length=48,
                    ),
                ),
                ("is_active", models.BooleanField(db_index=True, default=True)),
                (
                    "opens_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="Optional gate — channel returns 410 before this time.",
                        null=True,
                    ),
                ),
                (
                    "closes_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="Optional gate — channel returns 410 after this time.",
                        null=True,
                    ),
                ),
                (
                    "require_email",
                    models.BooleanField(
                        default=False,
                        help_text="When True, the public form requires an email for self-identification.",
                    ),
                ),
                ("created_at", models.DateTimeField(blank=True, editable=False)),
                ("updated_at", models.DateTimeField(blank=True, editable=False)),
                ("history_id", models.AutoField(primary_key=True, serialize=False)),
                ("history_date", models.DateTimeField(db_index=True)),
                ("history_change_reason", models.CharField(max_length=100, null=True)),
                (
                    "history_type",
                    models.CharField(
                        choices=[("+", "Created"), ("~", "Changed"), ("-", "Deleted")], max_length=1
                    ),
                ),
                (
                    "activity",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to="mel_tracking.activity",
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "history_user",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "historical activity attendance channel",
                "verbose_name_plural": "historical activity attendance channels",
                "ordering": ("-history_date", "-history_id"),
                "get_latest_by": ("history_date", "history_id"),
            },
            bases=(simple_history.models.HistoricalChanges, models.Model),
        ),
        migrations.CreateModel(
            name="HistoricalBeneficiaryAttendance",
            fields=[
                (
                    "id",
                    models.BigIntegerField(
                        auto_created=True, blank=True, db_index=True, verbose_name="ID"
                    ),
                ),
                ("attendee_name", models.CharField(blank=True, max_length=255)),
                ("attendee_email", models.EmailField(blank=True, max_length=254)),
                ("attendee_phone", models.CharField(blank=True, max_length=40)),
                ("organisation", models.CharField(blank=True, max_length=255)),
                ("notes", models.TextField(blank=True)),
                ("dedupe_key", models.CharField(db_index=True, max_length=64)),
                ("attended_at", models.DateTimeField(blank=True, db_index=True, editable=False)),
                ("history_id", models.AutoField(primary_key=True, serialize=False)),
                ("history_date", models.DateTimeField(db_index=True)),
                ("history_change_reason", models.CharField(max_length=100, null=True)),
                (
                    "history_type",
                    models.CharField(
                        choices=[("+", "Created"), ("~", "Changed"), ("-", "Deleted")], max_length=1
                    ),
                ),
                (
                    "attendee_user",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "channel",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to="mel_tracking.activityattendancechannel",
                    ),
                ),
                (
                    "history_user",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "historical beneficiary attendance",
                "verbose_name_plural": "historical beneficiary attendances",
                "ordering": ("-history_date", "-history_id"),
                "get_latest_by": ("history_date", "history_id"),
            },
            bases=(simple_history.models.HistoricalChanges, models.Model),
        ),
        migrations.CreateModel(
            name="BeneficiaryAttendance",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("attendee_name", models.CharField(blank=True, max_length=255)),
                ("attendee_email", models.EmailField(blank=True, max_length=254)),
                ("attendee_phone", models.CharField(blank=True, max_length=40)),
                ("organisation", models.CharField(blank=True, max_length=255)),
                ("notes", models.TextField(blank=True)),
                ("dedupe_key", models.CharField(db_index=True, max_length=64)),
                ("attended_at", models.DateTimeField(auto_now_add=True, db_index=True)),
                (
                    "attendee_user",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="mel_event_attendances",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "channel",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="attendances",
                        to="mel_tracking.activityattendancechannel",
                    ),
                ),
            ],
            options={
                "ordering": ["-attended_at"],
                "indexes": [
                    models.Index(
                        fields=["channel", "attended_at"], name="mel_trackin_channel_160ccc_idx"
                    )
                ],
                "constraints": [
                    models.UniqueConstraint(
                        fields=("channel", "dedupe_key"), name="uniq_attendance_per_channel"
                    )
                ],
            },
        ),
    ]
