# Generated by Django 5.2.14 on 2026-07-07 07:44

import apps.core.storage.paths
import apps.core.storage.validators
import django.core.validators
import django.db.models.deletion
import django_fsm
from django.conf import settings
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ("mel_indicators", "0011_historicallogframe_close_reason_and_more"),
        ("rims_grants", "0053_applicantcommunityservice_applicantemployment_and_more"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="ImplementationPlan",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "objective",
                    models.TextField(
                        help_text="Overall project/implementation objective — maps to the Impact row."
                    ),
                ),
                (
                    "assumptions",
                    models.TextField(
                        blank=True,
                        help_text="Key assumptions necessary for successful implementation.",
                    ),
                ),
                (
                    "status",
                    django_fsm.FSMField(
                        choices=[
                            ("draft", "Draft"),
                            ("submitted", "Submitted"),
                            ("approved", "Approved"),
                            ("revision_required", "Revision required"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=24,
                        protected=True,
                    ),
                ),
                ("submitted_at", models.DateTimeField(blank=True, null=True)),
                ("approved_at", models.DateTimeField(blank=True, null=True)),
                ("revision_comment", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "approved_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="implementation_plans_approved",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "award",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="implementation_plan",
                        to="rims_grants.award",
                    ),
                ),
                (
                    "results_framework",
                    models.ForeignKey(
                        blank=True,
                        help_text="Populated by generate_results_framework() once this plan is approved.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to="mel_indicators.logframe",
                    ),
                ),
                (
                    "submitted_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="implementation_plans_submitted",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="ImplementationPlanActivity",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("planned_start", models.DateField(blank=True, null=True)),
                ("planned_end", models.DateField(blank=True, null=True)),
                ("risks", models.TextField(blank=True)),
                ("mitigation_measures", models.TextField(blank=True)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("not_started", "Not started"),
                            ("in_progress", "In progress"),
                            ("completed", "Completed"),
                        ],
                        default="not_started",
                        max_length=16,
                    ),
                ),
                (
                    "percent_complete",
                    models.PositiveSmallIntegerField(
                        default=0,
                        validators=[
                            django.core.validators.MinValueValidator(0),
                            django.core.validators.MaxValueValidator(100),
                        ],
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "depends_on",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Activities that must complete before this one can start.",
                        related_name="blocks",
                        to="rims_grants.implementationplanactivity",
                    ),
                ),
                (
                    "mel_logframe_row",
                    models.ForeignKey(
                        blank=True,
                        help_text="Populated by generate_results_framework() once the plan is approved.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to="mel_indicators.logframerow",
                    ),
                ),
                (
                    "parent",
                    models.ForeignKey(
                        blank=True,
                        help_text="Set for sub-activities.",
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="sub_activities",
                        to="rims_grants.implementationplanactivity",
                    ),
                ),
                (
                    "responsible_person",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Implementation plan activities",
            },
        ),
        migrations.CreateModel(
            name="ImplementationPlanEvidence",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "file",
                    models.FileField(
                        upload_to=apps.core.storage.paths.upload_to_grants,
                        validators=[
                            apps.core.storage.validators.FileValidator(
                                allowed_categories=["document", "data", "archive"], module="rims"
                            )
                        ],
                    ),
                ),
                ("description", models.CharField(blank=True, max_length=255)),
                ("uploaded_at", models.DateTimeField(auto_now_add=True)),
                (
                    "activity",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="evidence",
                        to="rims_grants.implementationplanactivity",
                    ),
                ),
                (
                    "uploaded_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name_plural": "Implementation plan evidence",
            },
        ),
        migrations.CreateModel(
            name="ImplementationPlanOutcome",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("description", models.TextField(blank=True)),
                ("order", models.PositiveSmallIntegerField(default=0)),
                (
                    "plan",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="outcomes",
                        to="rims_grants.implementationplan",
                    ),
                ),
            ],
            options={
                "ordering": ["order", "pk"],
            },
        ),
        migrations.CreateModel(
            name="ImplementationPlanOutput",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("description", models.TextField(blank=True)),
                ("order", models.PositiveSmallIntegerField(default=0)),
                (
                    "outcome",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="outputs",
                        to="rims_grants.implementationplanoutcome",
                    ),
                ),
            ],
            options={
                "ordering": ["order", "pk"],
            },
        ),
        migrations.CreateModel(
            name="ImplementationPlanIndicator",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("name", models.CharField(max_length=255)),
                (
                    "baseline_value",
                    models.DecimalField(blank=True, decimal_places=4, max_digits=18, null=True),
                ),
                (
                    "target_value",
                    models.DecimalField(blank=True, decimal_places=4, max_digits=18, null=True),
                ),
                (
                    "means_of_verification",
                    models.CharField(
                        choices=[
                            ("report", "Report"),
                            ("publication", "Publication"),
                            ("certificate", "Certificate"),
                            ("dataset", "Dataset"),
                            ("attendance_register", "Attendance register"),
                            ("monitoring_report", "Monitoring report"),
                            ("other", "Other"),
                        ],
                        max_length=24,
                    ),
                ),
                (
                    "reporting_frequency",
                    models.CharField(
                        choices=[
                            ("daily", "Daily"),
                            ("weekly", "Weekly"),
                            ("monthly", "Monthly"),
                            ("quarterly", "Quarterly"),
                            ("semi_annual", "Semi-annual"),
                            ("annual", "Annual"),
                            ("ad_hoc", "Ad hoc"),
                        ],
                        max_length=16,
                    ),
                ),
                (
                    "mel_indicator",
                    models.ForeignKey(
                        blank=True,
                        help_text="Populated by generate_results_framework() once the plan is approved.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to="mel_indicators.indicator",
                    ),
                ),
                (
                    "outcome",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="indicators",
                        to="rims_grants.implementationplanoutcome",
                    ),
                ),
                (
                    "output",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="indicators",
                        to="rims_grants.implementationplanoutput",
                    ),
                ),
            ],
        ),
        migrations.AddField(
            model_name="implementationplanactivity",
            name="output",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="activities",
                to="rims_grants.implementationplanoutput",
            ),
        ),
        migrations.CreateModel(
            name="MonitoringVisit",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "visit_type",
                    models.CharField(
                        choices=[("physical", "Physical"), ("virtual", "Virtual")],
                        default="physical",
                        max_length=16,
                    ),
                ),
                ("scheduled_for", models.DateTimeField()),
                (
                    "participants",
                    models.TextField(blank=True, help_text="Names/roles of expected participants."),
                ),
                ("agenda", models.TextField(blank=True)),
                ("findings", models.TextField(blank=True)),
                ("strengths", models.TextField(blank=True)),
                ("gaps", models.TextField(blank=True)),
                ("risks_identified", models.TextField(blank=True)),
                ("recommendations", models.TextField(blank=True)),
                (
                    "latitude",
                    models.DecimalField(blank=True, decimal_places=6, max_digits=9, null=True),
                ),
                (
                    "longitude",
                    models.DecimalField(blank=True, decimal_places=6, max_digits=9, null=True),
                ),
                ("conducted_at", models.DateTimeField(blank=True, null=True)),
                (
                    "locked_at",
                    models.DateTimeField(
                        blank=True,
                        help_text="Set once findings are submitted — further edits are refused by the service layer.",
                        null=True,
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "activity",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="monitoring_visits",
                        to="rims_grants.implementationplanactivity",
                    ),
                ),
                (
                    "award",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="monitoring_visits",
                        to="rims_grants.award",
                    ),
                ),
                (
                    "initiated_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-scheduled_for"],
            },
        ),
        migrations.CreateModel(
            name="MonitoringVisitEvidence",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "file",
                    models.FileField(
                        blank=True, null=True, upload_to=apps.core.storage.paths.upload_to_grants
                    ),
                ),
                ("caption", models.CharField(blank=True, max_length=255)),
                (
                    "visit",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="evidence",
                        to="rims_grants.monitoringvisit",
                    ),
                ),
            ],
        ),
    ]
