# Generated manually for PRD alignment (funding record, close-out, publish-related fields)

import datetime

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


class Migration(migrations.Migration):

    dependencies = [
        ("rims_grants", "0008_storage_validators_and_career_files"),
        ("rims_operations", "0002_add_partner_type"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="FundingRecord",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "public_grant_id",
                    models.CharField(
                        blank=True,
                        db_index=True,
                        help_text="Unique Grant ID (assigned when submitted for approval / on approval).",
                        max_length=40,
                        unique=True,
                    ),
                ),
                ("title", models.CharField(max_length=255)),
                ("amount", models.DecimalField(decimal_places=2, max_digits=14)),
                ("currency", models.CharField(default="USD", max_length=8)),
                ("start_date", models.DateField(default=datetime.date(2026, 1, 1))),
                ("end_date", models.DateField(default=datetime.date(2026, 12, 31))),
                ("strategic_objectives", models.TextField(blank=True)),
                (
                    "reporting_financial_frequency",
                    models.CharField(
                        choices=[
                            ("monthly", "Monthly"),
                            ("quarterly", "Quarterly"),
                            ("semi_annual", "Semi-annual"),
                            ("annual", "Annual"),
                            ("ad_hoc", "Ad hoc"),
                        ],
                        default="quarterly",
                        max_length=24,
                    ),
                ),
                (
                    "reporting_narrative_frequency",
                    models.CharField(
                        choices=[
                            ("monthly", "Monthly"),
                            ("quarterly", "Quarterly"),
                            ("semi_annual", "Semi-annual"),
                            ("annual", "Annual"),
                            ("ad_hoc", "Ad hoc"),
                        ],
                        default="quarterly",
                        max_length=24,
                    ),
                ),
                (
                    "status",
                    models.CharField(
                        choices=[
                            ("draft", "Draft"),
                            ("pending_approval", "Pending approval"),
                            ("draft_revision_required", "Draft revision required"),
                            ("approved", "Approved"),
                            ("closed", "Closed"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=32,
                    ),
                ),
                ("rejection_comment", models.TextField(blank=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="funding_records_created",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "finance_officer",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="funding_records_as_finance_officer",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "grant_manager",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="funding_records_as_grant_manager",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "partner",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="funding_records",
                        to="rims_operations.partner",
                    ),
                ),
                (
                    "program_lead",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="funding_records_as_program_lead",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
            options={
                "ordering": ["-created_at"],
            },
        ),
        migrations.CreateModel(
            name="GrantBudgetLine",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                ("category", models.CharField(blank=True, max_length=128)),
                ("description", models.CharField(max_length=255)),
                ("unit_of_measure", models.CharField(blank=True, max_length=64)),
                ("quantity", models.DecimalField(decimal_places=4, default=1, max_digits=14)),
                ("unit_cost", models.DecimalField(decimal_places=2, max_digits=14)),
                ("sort_order", models.PositiveSmallIntegerField(default=0)),
                (
                    "funding_record",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="budget_lines",
                        to="rims_grants.fundingrecord",
                    ),
                ),
            ],
            options={
                "ordering": ["sort_order", "pk"],
            },
        ),
        migrations.CreateModel(
            name="AwardCloseoutRecord",
            fields=[
                ("id", models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name="ID")),
                (
                    "financial_snapshot",
                    models.JSONField(
                        blank=True,
                        default=dict,
                        help_text="Captured disbursement vs budget totals at last save.",
                    ),
                ),
                (
                    "checklist",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text='List of {"key": str, "label": str, "required": bool, "done": bool, "evidence_note": str}',
                    ),
                ),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "award",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="closeout_record",
                        to="rims_grants.award",
                    ),
                ),
            ],
            options={
                "ordering": ["-updated_at"],
            },
        ),
        migrations.AddField(
            model_name="grantcalldocument",
            name="is_guideline",
            field=models.BooleanField(
                default=True,
                help_text="When true, counts toward NFRFA008 guideline document requirement.",
            ),
        ),
        migrations.AddField(
            model_name="application",
            name="revision_note",
            field=models.TextField(blank=True, help_text="Instructions from grants manager to the applicant."),
        ),
        migrations.AddField(
            model_name="application",
            name="revision_requested_at",
            field=models.DateTimeField(
                blank=True,
                help_text="When set, applicant may edit a submitted application (NFRFA012).",
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="grantcall",
            name="funding_record",
            field=models.ForeignKey(
                blank=True,
                help_text="When set, call allocation is drawn from this approved funding record (PRD call setup).",
                null=True,
                on_delete=django.db.models.deletion.PROTECT,
                related_name="calls",
                to="rims_grants.fundingrecord",
            ),
        ),
        migrations.AddField(
            model_name="grantcall",
            name="max_awards",
            field=models.PositiveIntegerField(
                blank=True,
                help_text="Maximum number of awards for this call (PRD general information).",
                null=True,
            ),
        ),
        migrations.AddField(
            model_name="grantcall",
            name="review_ends_at",
            field=models.DateTimeField(blank=True, help_text="Review period end.", null=True),
        ),
        migrations.AddField(
            model_name="grantcall",
            name="review_starts_at",
            field=models.DateTimeField(
                blank=True,
                help_text="Review period start — must be after submission deadline (PRD FRFA-CS008).",
                null=True,
            ),
        ),
    ]
