# Generated by Django 5.2.13 on 2026-05-04 21:49

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


class Migration(migrations.Migration):

    dependencies = [
        ("repository_documents", "0008_collection_deprecation"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="EmailIngestMailbox",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("name", models.CharField(max_length=120, unique=True)),
                ("imap_host", models.CharField(max_length=255)),
                ("imap_port", models.PositiveIntegerField(default=993)),
                ("use_ssl", models.BooleanField(default=True)),
                ("username", models.CharField(max_length=255)),
                (
                    "password_signed",
                    models.BinaryField(
                        blank=True,
                        help_text="Set/read via the `password` property — never assign directly.",
                        null=True,
                    ),
                ),
                ("inbox_folder", models.CharField(default="INBOX", max_length=120)),
                ("processed_folder", models.CharField(default="Processed", max_length=120)),
                ("failed_folder", models.CharField(default="Failed", max_length=120)),
                (
                    "default_license",
                    models.CharField(
                        default="cc_by",
                        help_text="Default license assigned to documents ingested through this mailbox.",
                        max_length=32,
                    ),
                ),
                (
                    "parsing_rules",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text='e.g. {"title_from": "subject", "abstract_from": "body"}',
                    ),
                ),
                ("is_enabled", models.BooleanField(db_index=True, default=True)),
                ("last_polled_at", models.DateTimeField(blank=True, null=True)),
                ("last_error", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "default_collection",
                    models.ForeignKey(
                        help_text="Mailbox-wide default when no Collection.email_ingest_address matches the recipient.",
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="email_ingest_mailboxes",
                        to="repository_documents.collection",
                    ),
                ),
                (
                    "submitter",
                    models.ForeignKey(
                        blank=True,
                        help_text="User credited as submitter when the sender's email doesn't match a CustomUser.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="email_ingest_mailboxes",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["name"],
            },
        ),
        migrations.CreateModel(
            name="EmailIngestEvent",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("message_uid", models.CharField(max_length=120)),
                ("subject", models.CharField(blank=True, max_length=500)),
                ("sender", models.CharField(blank=True, max_length=255)),
                ("processed_at", models.DateTimeField(auto_now_add=True)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("ingested", "Ingested"),
                            ("skipped_dup", "Skipped (duplicate)"),
                            ("rejected_sender", "Rejected (sender)"),
                            ("rejected_recipient", "Rejected (no collection)"),
                            ("rejected_validation", "Rejected (validation)"),
                            ("error", "Error"),
                        ],
                        db_index=True,
                        max_length=24,
                    ),
                ),
                ("error_message", models.TextField(blank=True)),
                (
                    "document",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="email_ingest_events",
                        to="repository_documents.document",
                    ),
                ),
                (
                    "mailbox",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="events",
                        to="repository_documents.emailingestmailbox",
                    ),
                ),
            ],
            options={
                "ordering": ["-processed_at"],
                "indexes": [
                    models.Index(
                        fields=["mailbox", "status"], name="repository__mailbox_fc212a_idx"
                    )
                ],
                "constraints": [
                    models.UniqueConstraint(
                        fields=("mailbox", "message_uid"), name="uniq_email_ingest_msg"
                    )
                ],
            },
        ),
    ]
