# Generated by Django 5.0.7 on 2024-07-10 05:03

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

import jemail.models
import jemail.settings


class Migration(migrations.Migration):
    initial = True

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="EmailAttachment",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now=True, verbose_name="created at"),
                ),
                ("filename", models.CharField(max_length=256, verbose_name="name")),
                (
                    "file",
                    models.FileField(
                        max_length=512,
                        upload_to=jemail.settings.ATTACHMENT_UPLOAD_TO,
                        verbose_name="file",
                    ),
                ),
                (
                    "mimetype",
                    models.CharField(max_length=128, verbose_name="MIME type"),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="created by",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="EmailMessage",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "created_at",
                    models.DateTimeField(auto_now=True, verbose_name="created at"),
                ),
                (
                    "from_email",
                    jemail.models.MailboxField(
                        max_length=256, verbose_name="from email"
                    ),
                ),
                (
                    "reply_to",
                    models.JSONField(
                        blank=True, null=True, verbose_name="reply-to email"
                    ),
                ),
                ("subject", models.TextField(verbose_name="email subject")),
                ("body", models.TextField(blank=True, verbose_name="email text")),
                (
                    "html_message",
                    models.FileField(
                        blank=True,
                        upload_to=jemail.settings.HTML_MESSAGE_UPLOAD_TO,
                        verbose_name="html message",
                    ),
                ),
                (
                    "created_by",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                        verbose_name="created by",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="EmailMessageAttachment",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "attachment",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="+",
                        to="jemail.emailattachment",
                        verbose_name="attachment",
                    ),
                ),
                (
                    "message",
                    models.ForeignKey(
                        db_index=False,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="+",
                        to="jemail.emailmessage",
                        verbose_name="message",
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="emailmessage",
            name="attachments",
            field=models.ManyToManyField(
                related_name="messages",
                through="jemail.EmailMessageAttachment",
                to="jemail.emailattachment",
                verbose_name="attachments",
            ),
        ),
        migrations.CreateModel(
            name="EmailRecipient",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "address",
                    models.EmailField(max_length=254, verbose_name="email address"),
                ),
                (
                    "provider_id",
                    models.CharField(
                        max_length=128,
                        verbose_name="mail service provider recipient's id",
                    ),
                ),
                (
                    "kind",
                    models.PositiveSmallIntegerField(
                        choices=[(0, "to"), (1, "cc"), (2, "bcc")],
                        verbose_name="recipient kind",
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        max_length=32, verbose_name="mail delivery status"
                    ),
                ),
                (
                    "timestamp",
                    models.DateTimeField(
                        null=True, verbose_name="latest delivery event time"
                    ),
                ),
                (
                    "clicks_count",
                    models.PositiveSmallIntegerField(
                        default=0, verbose_name="clicks count"
                    ),
                ),
                (
                    "opens_count",
                    models.PositiveSmallIntegerField(
                        default=0, verbose_name="opens count"
                    ),
                ),
                (
                    "message",
                    models.ForeignKey(
                        db_index=False,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="recipients",
                        to="jemail.emailmessage",
                        verbose_name="message",
                    ),
                ),
            ],
        ),
        migrations.AddConstraint(
            model_name="emailmessageattachment",
            constraint=models.UniqueConstraint(
                fields=("message", "attachment"),
                name="jemail_unique_message_attachment",
            ),
        ),
        migrations.AddConstraint(
            model_name="emailrecipient",
            constraint=models.UniqueConstraint(
                fields=("message", "address"),
                name="jemail_message_has_unique_recipients",
            ),
        ),
        migrations.AddConstraint(
            model_name="emailrecipient",
            constraint=models.CheckConstraint(
                check=models.Q(
                    ("address", django.db.models.functions.text.Lower("address"))
                ),
                name="jemail_address_in_lowercase",
            ),
        ),
    ]
