# Generated by Django 5.2.13 on 2026-04-24 06:03

import apps.core.storage.paths
import apps.core.storage.validators
import django.db.models.deletion
import django.utils.timezone
import simple_history.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="HistoricalLogFrame",
            fields=[
                (
                    "id",
                    models.BigIntegerField(
                        auto_created=True, blank=True, db_index=True, verbose_name="ID"
                    ),
                ),
                ("name", models.CharField(db_index=True, max_length=255)),
                ("slug", models.SlugField(max_length=120)),
                ("description", models.TextField(blank=True)),
                ("programme", models.CharField(blank=True, max_length=120)),
                ("active", models.BooleanField(db_index=True, default=True)),
                ("created_at", models.DateTimeField(blank=True, editable=False)),
                ("updated_at", models.DateTimeField(blank=True, editable=False)),
                ("history_id", models.AutoField(primary_key=True, serialize=False)),
                ("history_date", models.DateTimeField(db_index=True)),
                ("history_change_reason", models.CharField(max_length=100, null=True)),
                (
                    "history_type",
                    models.CharField(
                        choices=[("+", "Created"), ("~", "Changed"), ("-", "Deleted")], max_length=1
                    ),
                ),
                (
                    "history_user",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "owner",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "verbose_name": "historical log frame",
                "verbose_name_plural": "historical log frames",
                "ordering": ("-history_date", "-history_id"),
                "get_latest_by": ("history_date", "history_id"),
            },
            bases=(simple_history.models.HistoricalChanges, models.Model),
        ),
        migrations.CreateModel(
            name="Indicator",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("code", models.SlugField(max_length=60, unique=True)),
                ("name", models.CharField(max_length=255)),
                ("definition", models.TextField(blank=True)),
                (
                    "indicator_type",
                    models.CharField(
                        choices=[("quantitative", "Quantitative"), ("qualitative", "Qualitative")],
                        default="quantitative",
                        max_length=16,
                    ),
                ),
                (
                    "unit",
                    models.CharField(blank=True, help_text="e.g. people, %, USD", max_length=60),
                ),
                (
                    "calculation_method",
                    models.TextField(
                        blank=True, help_text="How the value is computed or aggregated."
                    ),
                ),
                (
                    "data_source",
                    models.CharField(
                        help_text="Surveys, upstream module, admin records, etc.", max_length=255
                    ),
                ),
                (
                    "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,
                    ),
                ),
                (
                    "source_module",
                    models.CharField(
                        blank=True,
                        help_text="Upstream module name if this indicator receives automated data points (e.g. 'rims', 'rep').",
                        max_length=32,
                    ),
                ),
                (
                    "auto_event_type",
                    models.CharField(
                        blank=True,
                        help_text="Event type key matched when draining the integration outbox.",
                        max_length=64,
                    ),
                ),
                ("is_active", models.BooleanField(db_index=True, default=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "responsible",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="mel_indicators",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["code"],
            },
        ),
        migrations.CreateModel(
            name="HistoricalIndicatorTarget",
            fields=[
                (
                    "id",
                    models.BigIntegerField(
                        auto_created=True, blank=True, db_index=True, verbose_name="ID"
                    ),
                ),
                (
                    "period_label",
                    models.CharField(
                        help_text="e.g. 'FY2026', '2026-Q1', '2026-03'", max_length=40
                    ),
                ),
                ("period_start", models.DateField()),
                ("period_end", models.DateField()),
                (
                    "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),
                ),
                ("notes", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(blank=True, editable=False)),
                ("updated_at", models.DateTimeField(blank=True, editable=False)),
                ("history_id", models.AutoField(primary_key=True, serialize=False)),
                ("history_date", models.DateTimeField(db_index=True)),
                ("history_change_reason", models.CharField(max_length=100, null=True)),
                (
                    "history_type",
                    models.CharField(
                        choices=[("+", "Created"), ("~", "Changed"), ("-", "Deleted")], max_length=1
                    ),
                ),
                (
                    "history_user",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "indicator",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to="mel_indicators.indicator",
                    ),
                ),
            ],
            options={
                "verbose_name": "historical indicator target",
                "verbose_name_plural": "historical indicator targets",
                "ordering": ("-history_date", "-history_id"),
                "get_latest_by": ("history_date", "history_id"),
            },
            bases=(simple_history.models.HistoricalChanges, models.Model),
        ),
        migrations.CreateModel(
            name="IndicatorTarget",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "period_label",
                    models.CharField(
                        help_text="e.g. 'FY2026', '2026-Q1', '2026-03'", max_length=40
                    ),
                ),
                ("period_start", models.DateField()),
                ("period_end", models.DateField()),
                (
                    "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),
                ),
                ("notes", models.TextField(blank=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "indicator",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="targets",
                        to="mel_indicators.indicator",
                    ),
                ),
            ],
            options={
                "ordering": ["indicator", "period_start"],
            },
        ),
        migrations.CreateModel(
            name="DataPoint",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("period_label", models.CharField(max_length=40)),
                ("reported_at", models.DateTimeField(default=django.utils.timezone.now)),
                ("value", models.DecimalField(decimal_places=4, max_digits=18)),
                ("qualitative_note", models.TextField(blank=True)),
                (
                    "evidence",
                    models.FileField(
                        blank=True,
                        null=True,
                        upload_to=apps.core.storage.paths.upload_to_mel_reports,
                        validators=[
                            apps.core.storage.validators.FileValidator(
                                allowed_categories=["document", "data", "image"], module="mel"
                            )
                        ],
                    ),
                ),
                ("source_module", models.CharField(blank=True, max_length=32)),
                ("source_event", models.CharField(blank=True, max_length=64)),
                ("source_object_id", models.CharField(blank=True, max_length=64)),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("pending", "Pending verification"),
                            ("verified", "Verified"),
                            ("rejected", "Rejected"),
                        ],
                        db_index=True,
                        default="pending",
                        max_length=16,
                    ),
                ),
                ("verified_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                (
                    "reported_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="mel_data_points_reported",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "verified_by",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="mel_data_points_verified",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "indicator",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="data_points",
                        to="mel_indicators.indicator",
                    ),
                ),
                (
                    "target",
                    models.ForeignKey(
                        blank=True,
                        help_text="Optional link to the target period this data point counts toward.",
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="data_points",
                        to="mel_indicators.indicatortarget",
                    ),
                ),
            ],
            options={
                "ordering": ["-reported_at"],
            },
        ),
        migrations.CreateModel(
            name="LogFrame",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("name", models.CharField(max_length=255, unique=True)),
                ("slug", models.SlugField(max_length=120, unique=True)),
                ("description", models.TextField(blank=True)),
                ("programme", models.CharField(blank=True, max_length=120)),
                ("active", models.BooleanField(db_index=True, default=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "owner",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="owned_logframes",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["name"],
            },
        ),
        migrations.CreateModel(
            name="LogFrameRow",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "level",
                    models.CharField(
                        choices=[
                            ("impact", "Impact"),
                            ("outcome", "Outcome"),
                            ("output", "Output"),
                            ("activity", "Activity"),
                            ("input", "Input"),
                        ],
                        max_length=16,
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("description", models.TextField(blank=True)),
                ("assumptions", models.TextField(blank=True)),
                ("order", models.PositiveSmallIntegerField(default=0)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "logframe",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="rows",
                        to="mel_indicators.logframe",
                    ),
                ),
                (
                    "parent",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="children",
                        to="mel_indicators.logframerow",
                    ),
                ),
            ],
            options={
                "ordering": ["logframe", "order", "pk"],
            },
        ),
        migrations.AddField(
            model_name="indicator",
            name="logframe_row",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.PROTECT,
                related_name="indicators",
                to="mel_indicators.logframerow",
            ),
        ),
        migrations.CreateModel(
            name="HistoricalLogFrameRow",
            fields=[
                (
                    "id",
                    models.BigIntegerField(
                        auto_created=True, blank=True, db_index=True, verbose_name="ID"
                    ),
                ),
                (
                    "level",
                    models.CharField(
                        choices=[
                            ("impact", "Impact"),
                            ("outcome", "Outcome"),
                            ("output", "Output"),
                            ("activity", "Activity"),
                            ("input", "Input"),
                        ],
                        max_length=16,
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("description", models.TextField(blank=True)),
                ("assumptions", models.TextField(blank=True)),
                ("order", models.PositiveSmallIntegerField(default=0)),
                ("created_at", models.DateTimeField(blank=True, editable=False)),
                ("updated_at", models.DateTimeField(blank=True, editable=False)),
                ("history_id", models.AutoField(primary_key=True, serialize=False)),
                ("history_date", models.DateTimeField(db_index=True)),
                ("history_change_reason", models.CharField(max_length=100, null=True)),
                (
                    "history_type",
                    models.CharField(
                        choices=[("+", "Created"), ("~", "Changed"), ("-", "Deleted")], max_length=1
                    ),
                ),
                (
                    "history_user",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "logframe",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to="mel_indicators.logframe",
                    ),
                ),
                (
                    "parent",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to="mel_indicators.logframerow",
                    ),
                ),
            ],
            options={
                "verbose_name": "historical log frame row",
                "verbose_name_plural": "historical log frame rows",
                "ordering": ("-history_date", "-history_id"),
                "get_latest_by": ("history_date", "history_id"),
            },
            bases=(simple_history.models.HistoricalChanges, models.Model),
        ),
        migrations.CreateModel(
            name="HistoricalIndicator",
            fields=[
                (
                    "id",
                    models.BigIntegerField(
                        auto_created=True, blank=True, db_index=True, verbose_name="ID"
                    ),
                ),
                ("code", models.SlugField(max_length=60)),
                ("name", models.CharField(max_length=255)),
                ("definition", models.TextField(blank=True)),
                (
                    "indicator_type",
                    models.CharField(
                        choices=[("quantitative", "Quantitative"), ("qualitative", "Qualitative")],
                        default="quantitative",
                        max_length=16,
                    ),
                ),
                (
                    "unit",
                    models.CharField(blank=True, help_text="e.g. people, %, USD", max_length=60),
                ),
                (
                    "calculation_method",
                    models.TextField(
                        blank=True, help_text="How the value is computed or aggregated."
                    ),
                ),
                (
                    "data_source",
                    models.CharField(
                        help_text="Surveys, upstream module, admin records, etc.", max_length=255
                    ),
                ),
                (
                    "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,
                    ),
                ),
                (
                    "source_module",
                    models.CharField(
                        blank=True,
                        help_text="Upstream module name if this indicator receives automated data points (e.g. 'rims', 'rep').",
                        max_length=32,
                    ),
                ),
                (
                    "auto_event_type",
                    models.CharField(
                        blank=True,
                        help_text="Event type key matched when draining the integration outbox.",
                        max_length=64,
                    ),
                ),
                ("is_active", models.BooleanField(db_index=True, default=True)),
                ("created_at", models.DateTimeField(blank=True, editable=False)),
                ("updated_at", models.DateTimeField(blank=True, editable=False)),
                ("history_id", models.AutoField(primary_key=True, serialize=False)),
                ("history_date", models.DateTimeField(db_index=True)),
                ("history_change_reason", models.CharField(max_length=100, null=True)),
                (
                    "history_type",
                    models.CharField(
                        choices=[("+", "Created"), ("~", "Changed"), ("-", "Deleted")], max_length=1
                    ),
                ),
                (
                    "history_user",
                    models.ForeignKey(
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "responsible",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "logframe_row",
                    models.ForeignKey(
                        blank=True,
                        db_constraint=False,
                        null=True,
                        on_delete=django.db.models.deletion.DO_NOTHING,
                        related_name="+",
                        to="mel_indicators.logframerow",
                    ),
                ),
            ],
            options={
                "verbose_name": "historical indicator",
                "verbose_name_plural": "historical indicators",
                "ordering": ("-history_date", "-history_id"),
                "get_latest_by": ("history_date", "history_id"),
            },
            bases=(simple_history.models.HistoricalChanges, models.Model),
        ),
        migrations.AddConstraint(
            model_name="indicatortarget",
            constraint=models.UniqueConstraint(
                fields=("indicator", "period_label"), name="uniq_indicator_target_period"
            ),
        ),
        migrations.AddConstraint(
            model_name="indicatortarget",
            constraint=models.CheckConstraint(
                condition=models.Q(("period_end__gte", models.F("period_start"))),
                name="indicator_target_period_end_after_start",
            ),
        ),
        migrations.AddIndex(
            model_name="datapoint",
            index=models.Index(
                fields=["indicator", "period_label"], name="mel_indicat_indicat_a9a53c_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="datapoint",
            index=models.Index(
                fields=["source_module", "source_event"], name="mel_indicat_source__5263ff_idx"
            ),
        ),
        migrations.AddIndex(
            model_name="datapoint",
            index=models.Index(fields=["status"], name="mel_indicat_status_1e3fd8_idx"),
        ),
        migrations.AddConstraint(
            model_name="datapoint",
            constraint=models.CheckConstraint(
                condition=models.Q(
                    models.Q(("source_module", ""), ("source_object_id", "")),
                    models.Q(("source_module", ""), _negated=True),
                    _connector="OR",
                ),
                name="datapoint_source_module_presence",
            ),
        ),
        migrations.AddIndex(
            model_name="logframerow",
            index=models.Index(fields=["logframe", "level"], name="mel_indicat_logfram_63b0b5_idx"),
        ),
        migrations.AddIndex(
            model_name="logframerow",
            index=models.Index(fields=["parent"], name="mel_indicat_parent__29b98e_idx"),
        ),
        migrations.AddIndex(
            model_name="indicator",
            index=models.Index(
                fields=["source_module", "auto_event_type"], name="mel_indicat_source__858b84_idx"
            ),
        ),
    ]
