# Generated by Django 6.0.2 on 2026-02-27 07:44

import django.core.validators
import django.db.models.deletion
import django.utils.timezone
import django_email_learning.models
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):
    initial = True

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

    operations = [
        migrations.CreateModel(
            name="BlockedEmail",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("email", models.EmailField(max_length=254, unique=True)),
            ],
        ),
        migrations.CreateModel(
            name="Course",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("title", models.CharField(max_length=200)),
                (
                    "slug",
                    models.SlugField(
                        help_text="A short label for the course, used in URLs or email interactive actions. You can not edit it later."
                    ),
                ),
                ("description", models.TextField(blank=True, null=True)),
                ("enabled", models.BooleanField(default=False)),
                (
                    "image",
                    models.ImageField(
                        blank=True, null=True, upload_to="course_images/"
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="ImapConnection",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("salt", models.CharField(editable=False, max_length=32)),
                (
                    "server",
                    models.CharField(
                        max_length=200,
                        validators=[django_email_learning.models.is_domain_or_ip],
                    ),
                ),
                ("port", models.IntegerField(db_default=993)),
                ("email", models.EmailField(max_length=200, unique=True)),
                ("password", models.CharField(max_length=200)),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="JobExecution",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "job_name",
                    models.CharField(
                        choices=[("DELIVER_CONTENTS", "deliver_contents")],
                        max_length=200,
                    ),
                ),
                ("started_at", models.DateTimeField(auto_now_add=True)),
                ("finished_at", models.DateTimeField(blank=True, null=True)),
                (
                    "status",
                    models.CharField(
                        choices=[("RUNNING", "running"), ("COMPLETED", "completed")],
                        max_length=50,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Learner",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("email", models.EmailField(max_length=254)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
            ],
        ),
        migrations.CreateModel(
            name="Lesson",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("title", models.CharField(max_length=200)),
                ("content", models.TextField()),
            ],
        ),
        migrations.CreateModel(
            name="Organization",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("name", models.CharField(max_length=200, unique=True)),
                (
                    "logo",
                    models.ImageField(
                        blank=True, null=True, upload_to="organization_logos/"
                    ),
                ),
                ("description", models.TextField(blank=True, null=True)),
            ],
        ),
        migrations.CreateModel(
            name="Question",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("text", models.CharField(max_length=500)),
                ("priority", models.IntegerField()),
            ],
        ),
        migrations.CreateModel(
            name="Quiz",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("title", models.CharField(max_length=500)),
                (
                    "required_score",
                    models.IntegerField(
                        validators=[django.core.validators.MaxValueValidator(100)]
                    ),
                ),
                (
                    "selection_strategy",
                    models.CharField(
                        choices=[
                            ("all", "All Questions"),
                            ("random", "Random Questions"),
                        ],
                        max_length=50,
                    ),
                ),
                (
                    "deadline_days",
                    models.IntegerField(
                        help_text="Time limit to complete the quiz in days. Minimum is 1 day and maximum is 30 days.",
                        validators=[
                            django.core.validators.MinValueValidator(1),
                            django.core.validators.MaxValueValidator(30),
                        ],
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Quizzes",
            },
        ),
        migrations.CreateModel(
            name="ApiKey",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("salt", models.CharField(editable=False, max_length=32)),
                (
                    "key",
                    models.CharField(
                        max_length=256,
                        unique=True,
                        validators=[django.core.validators.MinLengthValidator(50)],
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "abstract": False,
            },
        ),
        migrations.CreateModel(
            name="CourseContent",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("priority", models.IntegerField()),
                (
                    "type",
                    models.CharField(
                        choices=[("lesson", "Lesson"), ("quiz", "Quiz")], max_length=50
                    ),
                ),
                (
                    "waiting_period",
                    models.IntegerField(
                        help_text="Waiting period in seconds after previous content is sent or submited."
                    ),
                ),
                ("is_published", models.BooleanField(default=False)),
                (
                    "course",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="django_email_learning.course",
                    ),
                ),
                (
                    "lesson",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="django_email_learning.lesson",
                    ),
                ),
                (
                    "quiz",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        to="django_email_learning.quiz",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="ContentDelivery",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("hash_value", models.CharField(blank=True, max_length=64, null=True)),
                ("valid_until", models.DateTimeField(blank=True, null=True)),
                (
                    "course_content",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="django_email_learning.coursecontent",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="DeliverySchedule",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "time",
                    models.DateTimeField(
                        db_index=True, default=django.utils.timezone.now
                    ),
                ),
                ("link", models.URLField(blank=True, max_length=500, null=True)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            (
                                django_email_learning.models.DeliveryStatus[
                                    "SCHEDULED"
                                ],
                                "Scheduled",
                            ),
                            (
                                django_email_learning.models.DeliveryStatus[
                                    "PROCESSING"
                                ],
                                "Processing",
                            ),
                            (
                                django_email_learning.models.DeliveryStatus[
                                    "DELIVERED"
                                ],
                                "Delivered",
                            ),
                            (
                                django_email_learning.models.DeliveryStatus["CANCELED"],
                                "Canceled",
                            ),
                            (
                                django_email_learning.models.DeliveryStatus["BLOCKED"],
                                "Blocked",
                            ),
                        ],
                        db_index=True,
                        default=django_email_learning.models.DeliveryStatus[
                            "SCHEDULED"
                        ],
                        max_length=50,
                    ),
                ),
                ("failed_attempts", models.IntegerField(default=0)),
                ("delivered_at", models.DateTimeField(blank=True, null=True)),
                (
                    "delivery",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="delivery_schedules",
                        to="django_email_learning.contentdelivery",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Enrollment",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("enrolled_at", models.DateTimeField(auto_now_add=True)),
                ("activated_at", models.DateTimeField(blank=True, null=True)),
                ("final_state_at", models.DateTimeField(blank=True, null=True)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            (
                                django_email_learning.models.EnrollmentStatus[
                                    "UNVERIFIED"
                                ],
                                "Unverified",
                            ),
                            (
                                django_email_learning.models.EnrollmentStatus["ACTIVE"],
                                "Active",
                            ),
                            (
                                django_email_learning.models.EnrollmentStatus[
                                    "COMPLETED"
                                ],
                                "Completed",
                            ),
                            (
                                django_email_learning.models.EnrollmentStatus[
                                    "DEACTIVATED"
                                ],
                                "Deactivated",
                            ),
                        ],
                        default=django_email_learning.models.EnrollmentStatus[
                            "UNVERIFIED"
                        ],
                        max_length=50,
                    ),
                ),
                (
                    "deactivation_reason",
                    models.CharField(
                        blank=True,
                        choices=[
                            (
                                django_email_learning.models.DeactivationReason[
                                    "CANCELED"
                                ],
                                "Canceled",
                            ),
                            (
                                django_email_learning.models.DeactivationReason[
                                    "BLOCKED"
                                ],
                                "Blocked",
                            ),
                            (
                                django_email_learning.models.DeactivationReason[
                                    "FAILED"
                                ],
                                "Failed",
                            ),
                            (
                                django_email_learning.models.DeactivationReason[
                                    "INACTIVE"
                                ],
                                "Inactive",
                            ),
                        ],
                        max_length=50,
                        null=True,
                    ),
                ),
                (
                    "activation_code",
                    models.CharField(blank=True, max_length=6, null=True),
                ),
                (
                    "course",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="django_email_learning.course",
                    ),
                ),
                (
                    "learner",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="django_email_learning.learner",
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="contentdelivery",
            name="enrollment",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="content_deliveries",
                to="django_email_learning.enrollment",
            ),
        ),
        migrations.CreateModel(
            name="Certificate",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("issued_at", models.DateTimeField(auto_now_add=True)),
                ("name_on_certificate", models.CharField(max_length=200)),
                ("random_suffix", models.IntegerField()),
                (
                    "enrollment",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="certificate",
                        to="django_email_learning.enrollment",
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="course",
            name="imap_connection",
            field=models.ForeignKey(
                blank=True,
                null=True,
                on_delete=django.db.models.deletion.SET_NULL,
                to="django_email_learning.imapconnection",
            ),
        ),
        migrations.AddField(
            model_name="learner",
            name="organization",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                to="django_email_learning.organization",
            ),
        ),
        migrations.AddField(
            model_name="imapconnection",
            name="organization",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                to="django_email_learning.organization",
            ),
        ),
        migrations.AddField(
            model_name="course",
            name="organization",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                to="django_email_learning.organization",
            ),
        ),
        migrations.CreateModel(
            name="OrganizationUser",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                (
                    "role",
                    models.CharField(
                        choices=[
                            ("admin", "Admin"),
                            ("editor", "Editor"),
                            ("viewer", "Viewer"),
                        ],
                        db_index=True,
                        max_length=50,
                    ),
                ),
                (
                    "organization",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        to="django_email_learning.organization",
                    ),
                ),
                (
                    "user",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="memberships",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Answer",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("text", models.CharField(max_length=500)),
                ("is_correct", models.BooleanField(default=False)),
                (
                    "question",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="answers",
                        to="django_email_learning.question",
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="question",
            name="quiz",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="questions",
                to="django_email_learning.quiz",
            ),
        ),
        migrations.CreateModel(
            name="QuizSubmission",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True,
                        primary_key=True,
                        serialize=False,
                        verbose_name="ID",
                    ),
                ),
                ("score", models.IntegerField()),
                ("is_passed", models.BooleanField()),
                ("submitted_at", models.DateTimeField(auto_now_add=True)),
                (
                    "delivery",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="quiz_submissions",
                        to="django_email_learning.contentdelivery",
                    ),
                ),
            ],
        ),
        migrations.AlterUniqueTogether(
            name="contentdelivery",
            unique_together={("enrollment", "course_content")},
        ),
        migrations.AddConstraint(
            model_name="enrollment",
            constraint=models.UniqueConstraint(
                condition=models.Q(
                    ("status__in", ["unverified", "active", "completed"])
                ),
                fields=("learner", "course"),
                name="unique_active_enrollment",
            ),
        ),
        migrations.AlterUniqueTogether(
            name="learner",
            unique_together={("organization", "email")},
        ),
        migrations.AlterUniqueTogether(
            name="course",
            unique_together={("slug", "organization"), ("title", "organization")},
        ),
        migrations.AlterUniqueTogether(
            name="organizationuser",
            unique_together={("user", "organization")},
        ),
        migrations.AddConstraint(
            model_name="coursecontent",
            constraint=models.UniqueConstraint(
                condition=models.Q(("quiz__isnull", False)),
                fields=("course", "quiz"),
                name="unique_quiz_per_course",
            ),
        ),
        migrations.AddConstraint(
            model_name="coursecontent",
            constraint=models.UniqueConstraint(
                condition=models.Q(("lesson__isnull", False)),
                fields=("course", "lesson"),
                name="unique_lesson_per_course",
            ),
        ),
        migrations.AddConstraint(
            model_name="coursecontent",
            constraint=models.UniqueConstraint(
                fields=("course", "priority"), name="unique_priority_per_course"
            ),
        ),
    ]
