# Generated by Django 5.2.13 on 2026-04-25 01:05

import apps.core.storage.paths
import django.db.models.deletion
import django.utils.timezone
import django_fsm
import uuid
from decimal import Decimal
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        # The REP LMS (rep_courses) was retired for Moodle; the ProgrammeCourseLink
        # FK into rep_courses.course is neutralised below (and repointed to a
        # Moodle course id by migration 0013), so the cross-app dependency is dropped.
        ("smehub_onboarding", "0001_initial"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Cohort",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("name", models.CharField(max_length=255)),
                ("start_date", models.DateField()),
                ("end_date", models.DateField()),
                ("venue", models.CharField(blank=True, max_length=255)),
                (
                    "schedule",
                    models.JSONField(
                        blank=True, default=dict, help_text="Free-form schedule payload."
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
            ],
            options={
                "ordering": ["-start_date"],
            },
        ),
        migrations.CreateModel(
            name="RubricSection",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("instructions", models.TextField(blank=True)),
                (
                    "weight",
                    models.DecimalField(
                        decimal_places=4,
                        default=Decimal("0.25"),
                        help_text="Fractional weight (sections must sum to 1.0).",
                        max_digits=5,
                    ),
                ),
                ("max_marks", models.PositiveSmallIntegerField(default=10)),
                ("order", models.PositiveSmallIntegerField(default=0)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
            ],
            options={
                "ordering": ["order", "id"],
            },
        ),
        migrations.CreateModel(
            name="Application",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "application_id",
                    models.CharField(
                        editable=False,
                        help_text="Auto-generated public identifier.",
                        max_length=20,
                        unique=True,
                    ),
                ),
                (
                    "status",
                    django_fsm.FSMField(
                        choices=[
                            ("draft", "Draft (auto-saved)"),
                            ("submitted", "Submitted"),
                            ("under_review", "Under review"),
                            ("returned_for_info", "Returned for more info"),
                            ("accepted", "Accepted"),
                            ("rejected", "Not selected"),
                            ("withdrawn", "Withdrawn"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=24,
                        protected=True,
                    ),
                ),
                ("business_description", models.TextField(blank=True)),
                ("problem_statement", models.TextField(blank=True)),
                ("traction", models.TextField(blank=True)),
                ("funding_needs", models.TextField(blank=True)),
                ("support_sought", models.TextField(blank=True)),
                (
                    "auto_saved_payload",
                    models.JSONField(blank=True, default=dict, help_text="HTMX auto-save buffer."),
                ),
                ("submitted_at", models.DateTimeField(blank=True, null=True)),
                ("decided_at", models.DateTimeField(blank=True, null=True)),
                ("return_message", models.TextField(blank=True)),
                (
                    "weighted_score",
                    models.DecimalField(blank=True, decimal_places=2, max_digits=6, null=True),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "business",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="smehub_applications",
                        to="smehub_onboarding.business",
                    ),
                ),
                (
                    "decision_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="smehub_applications_decided",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "entrepreneur",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="smehub_applications",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="ApplicationDocument",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("label", models.CharField(max_length=255)),
                (
                    "file",
                    models.FileField(upload_to=apps.core.storage.paths.upload_to_smehub_incubation),
                ),
                ("uploaded_at", models.DateTimeField(auto_now_add=True)),
                (
                    "application",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="documents",
                        to="smehub_incubation.application",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="CohortMember",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "status",
                    django_fsm.FSMField(
                        choices=[
                            ("active", "Active"),
                            ("withdrawn", "Withdrawn"),
                            ("programme_complete", "Programme complete"),
                        ],
                        db_index=True,
                        default="active",
                        max_length=24,
                        protected=True,
                    ),
                ),
                ("withdrawn_reason", models.TextField(blank=True)),
                ("completed_at", models.DateTimeField(blank=True, null=True)),
                ("completed_manually", models.BooleanField(default=False)),
                ("completion_notes", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "application",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="cohort_membership",
                        to="smehub_incubation.application",
                    ),
                ),
                (
                    "business",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="cohort_memberships",
                        to="smehub_onboarding.business",
                    ),
                ),
                (
                    "cohort",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="members",
                        to="smehub_incubation.cohort",
                    ),
                ),
                (
                    "entrepreneur",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="cohort_memberships",
                        to="smehub_onboarding.entrepreneurprofile",
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="Certificate",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "certificate_number",
                    models.UUIDField(default=uuid.uuid4, editable=False, unique=True),
                ),
                (
                    "pdf_file",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_smehub_certificates,
                    ),
                ),
                ("generated_at", models.DateTimeField(default=django.utils.timezone.now)),
                (
                    "issued_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="smehub_certificates_issued",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "cohort_member",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="certificate",
                        to="smehub_incubation.cohortmember",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="JudgeAssignment",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("notified_at", models.DateTimeField(blank=True, null=True)),
                ("last_reminded_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "application",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="judge_assignments",
                        to="smehub_incubation.application",
                    ),
                ),
                (
                    "judge",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="smehub_judge_assignments",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="MentorAssignment",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("assigned_at", models.DateTimeField(default=django.utils.timezone.now)),
                ("ended_at", models.DateTimeField(blank=True, null=True)),
                ("notes", models.TextField(blank=True)),
                (
                    "cohort_member",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="mentor_assignments",
                        to="smehub_incubation.cohortmember",
                    ),
                ),
                (
                    "mentor",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="smehub_mentor_assignments",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "reassigned_from",
                    models.ForeignKey(
                        blank=True,
                        help_text="Set when this assignment supersedes a previous one (FRSME-INC027).",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="reassignments",
                        to="smehub_incubation.mentorassignment",
                    ),
                ),
            ],
            options={
                "ordering": ["-assigned_at"],
            },
        ),
        migrations.CreateModel(
            name="MentorSession",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "type",
                    models.CharField(
                        choices=[("virtual", "Virtual"), ("physical", "Physical")],
                        default="virtual",
                        max_length=16,
                    ),
                ),
                ("starts_at", models.DateTimeField()),
                ("duration_minutes", models.PositiveSmallIntegerField(default=60)),
                ("location", models.CharField(blank=True, max_length=255)),
                ("agenda", models.TextField(blank=True)),
                ("notes", models.TextField(blank=True)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("scheduled", "Scheduled"),
                            ("completed", "Completed"),
                            ("cancelled", "Cancelled"),
                        ],
                        db_index=True,
                        default="scheduled",
                        max_length=16,
                    ),
                ),
                ("completed_at", models.DateTimeField(blank=True, null=True)),
                ("cancellation_reason", models.CharField(blank=True, max_length=255)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "assignment",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="sessions",
                        to="smehub_incubation.mentorassignment",
                    ),
                ),
            ],
            options={
                "ordering": ["starts_at"],
            },
        ),
        migrations.CreateModel(
            name="Programme",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("slug", models.SlugField(max_length=120, unique=True)),
                ("objectives", models.TextField(blank=True)),
                (
                    "target_sectors",
                    models.JSONField(
                        blank=True, default=list, help_text="Sector tags eligible for application."
                    ),
                ),
                (
                    "target_stages",
                    models.JSONField(
                        blank=True, default=list, help_text="Business stage codes eligible."
                    ),
                ),
                (
                    "eligibility_countries",
                    models.JSONField(
                        blank=True, default=list, help_text="ISO-2 codes; empty = all."
                    ),
                ),
                ("cohort_size", models.PositiveIntegerField(default=20)),
                ("duration_weeks", models.PositiveIntegerField(default=12)),
                (
                    "aih_locations",
                    models.JSONField(
                        blank=True, default=list, help_text="Free-form list of AIH / venue names."
                    ),
                ),
                ("application_opens_at", models.DateTimeField(blank=True, null=True)),
                ("application_deadline", models.DateTimeField(blank=True, null=True)),
                (
                    "selection_method",
                    models.CharField(
                        choices=[
                            ("weighted_rubric", "Weighted rubric scoring"),
                            ("officer_decision", "Programme officer decision"),
                        ],
                        default="weighted_rubric",
                        max_length=32,
                    ),
                ),
                (
                    "status",
                    django_fsm.FSMField(
                        choices=[
                            ("draft", "Draft"),
                            ("published", "Published"),
                            ("closed", "Closed"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=16,
                        protected=True,
                    ),
                ),
                ("published_at", models.DateTimeField(blank=True, null=True)),
                ("closed_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "created_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="smehub_programmes_created",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.AddField(
            model_name="cohort",
            name="programme",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="cohorts",
                to="smehub_incubation.programme",
            ),
        ),
        migrations.AddField(
            model_name="application",
            name="programme",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="applications",
                to="smehub_incubation.programme",
            ),
        ),
        migrations.CreateModel(
            name="ProgrammeCourseLink",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("is_required", models.BooleanField(default=True)),
                ("notes", models.CharField(blank=True, max_length=255)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    # Originally a PROTECT FK to rep_courses.course; the REP LMS
                    # was retired for Moodle. Kept as a plain integer column (same
                    # db_column) so migration history stays consistent; repointed
                    # to moodle_course_id by migration 0013.
                    "course",
                    models.IntegerField(null=True, db_column="course_id"),
                ),
                (
                    "programme",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="course_links",
                        to="smehub_incubation.programme",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="ProgressRecord",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "course_completions",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Map of course_id → {completed_at, source}.",
                    ),
                ),
                (
                    "manual_trainings",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text="List of {title, completed_at, recorded_by} entries.",
                    ),
                ),
                ("sessions_completed", models.PositiveIntegerField(default=0)),
                ("last_milestone_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "cohort_member",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="progress",
                        to="smehub_incubation.cohortmember",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="JudgeScore",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("score", models.DecimalField(decimal_places=2, max_digits=5)),
                ("comments", models.TextField(blank=True)),
                ("submitted_at", models.DateTimeField(default=django.utils.timezone.now)),
                (
                    "application",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="scores",
                        to="smehub_incubation.application",
                    ),
                ),
                (
                    "judge",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="smehub_judge_scores",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "section",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.PROTECT,
                        related_name="scores",
                        to="smehub_incubation.rubricsection",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="ScoringRubric",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("description", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "programme",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="rubric",
                        to="smehub_incubation.programme",
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="rubricsection",
            name="rubric",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="sections",
                to="smehub_incubation.scoringrubric",
            ),
        ),
        migrations.AddIndex(
            model_name="cohortmember",
            index=models.Index(fields=["cohort", "status"], name="smehub_incu_cohort__3a55fb_idx"),
        ),
        migrations.AddConstraint(
            model_name="judgeassignment",
            constraint=models.UniqueConstraint(
                fields=("application", "judge"), name="uniq_smehub_judge_per_application"
            ),
        ),
        migrations.AddConstraint(
            model_name="mentorassignment",
            constraint=models.UniqueConstraint(
                condition=models.Q(("ended_at__isnull", True)),
                fields=("cohort_member", "mentor"),
                name="uniq_smehub_active_mentor_assignment",
            ),
        ),
        migrations.AddIndex(
            model_name="programme",
            index=models.Index(
                fields=["status", "application_deadline"], name="smehub_incu_status_f11e25_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="application",
            index=models.Index(
                fields=["programme", "status"], name="smehub_incu_program_7e59da_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="application",
            index=models.Index(
                fields=["entrepreneur", "status"], name="smehub_incu_entrepr_826a85_idx"
            ),
        ),
        migrations.AddConstraint(
            model_name="application",
            constraint=models.UniqueConstraint(
                condition=models.Q(
                    ("status__in", ["draft", "submitted", "under_review", "returned_for_info"])
                ),
                fields=("programme", "business"),
                name="uniq_smehub_active_application_per_business",
            ),
        ),
        migrations.AddConstraint(
            model_name="programmecourselink",
            constraint=models.UniqueConstraint(
                fields=("programme", "course"), name="uniq_smehub_programme_course"
            ),
        ),
        migrations.AddConstraint(
            model_name="judgescore",
            constraint=models.UniqueConstraint(
                fields=("application", "judge", "section"),
                name="uniq_smehub_score_per_judge_section",
            ),
        ),
    ]
