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 = [
        ("alumni_recognition", "0001_initial"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Broadcast",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("subject", models.CharField(max_length=200)),
                (
                    "body_html",
                    models.TextField(
                        help_text="Rich-text body. Rendered into the email and the in-app notification preview."
                    ),
                ),
                (
                    "audience",
                    models.CharField(
                        choices=[
                            ("all_opted_in", "All opted-in alumni"),
                            ("mentors", "Mentors accepting mentees"),
                            ("by_group", "Members of selected groups"),
                            ("by_country", "By country"),
                            ("by_graduation_year", "By graduation year"),
                            ("by_programme", "By programme name"),
                        ],
                        default="all_opted_in",
                        max_length=24,
                    ),
                ),
                (
                    "audience_filter",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Free-form filter payload for non-trivial audiences (group_ids, country, year, programme).",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("draft", "Draft"),
                            ("scheduled", "Scheduled"),
                            ("sending", "Sending"),
                            ("sent", "Sent"),
                            ("failed", "Failed"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=16,
                    ),
                ),
                ("scheduled_for", models.DateTimeField(blank=True, null=True)),
                ("sent_at", models.DateTimeField(blank=True, null=True)),
                ("sent_count", models.PositiveIntegerField(default=0)),
                ("last_error", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "created_by",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="alumni_broadcasts_authored",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
                "indexes": [
                    models.Index(
                        fields=["status", "scheduled_for"],
                        name="alumni_reco_status_brdcst_idx",
                    )
                ],
            },
        ),
        migrations.CreateModel(
            name="HistoricalBroadcast",
            fields=[
                (
                    "id",
                    models.BigIntegerField(
                        auto_created=True, blank=True, db_index=True, verbose_name="ID"
                    ),
                ),
                ("subject", models.CharField(max_length=200)),
                ("body_html", models.TextField(help_text="Rich-text body. Rendered into the email and the in-app notification preview.")),
                (
                    "audience",
                    models.CharField(
                        choices=[
                            ("all_opted_in", "All opted-in alumni"),
                            ("mentors", "Mentors accepting mentees"),
                            ("by_group", "Members of selected groups"),
                            ("by_country", "By country"),
                            ("by_graduation_year", "By graduation year"),
                            ("by_programme", "By programme name"),
                        ],
                        default="all_opted_in",
                        max_length=24,
                    ),
                ),
                ("audience_filter", models.JSONField(blank=True, default=dict, help_text="Free-form filter payload for non-trivial audiences (group_ids, country, year, programme).")),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("draft", "Draft"),
                            ("scheduled", "Scheduled"),
                            ("sending", "Sending"),
                            ("sent", "Sent"),
                            ("failed", "Failed"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=16,
                    ),
                ),
                ("scheduled_for", models.DateTimeField(blank=True, null=True)),
                ("sent_at", models.DateTimeField(blank=True, null=True)),
                ("sent_count", models.PositiveIntegerField(default=0)),
                ("last_error", models.TextField(blank=True)),
                ("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
                    ),
                ),
                (
                    "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 broadcast",
                "verbose_name_plural": "historical broadcasts",
                "ordering": ("-history_date", "-history_id"),
                "get_latest_by": ("history_date", "history_id"),
            },
            bases=(simple_history.models.HistoricalChanges, models.Model),
        ),
    ]
