# Generated by Django 5.2.12 on 2026-03-28 08:25

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


class Migration(migrations.Migration):

    initial = True

    dependencies = [
        ("core", "0002_rename_core_auditlo_actor_i_idx_core_auditl_actor_i_19c60f_idx_and_more"),
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
    ]

    operations = [
        migrations.CreateModel(
            name="Application",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "anonymized_code",
                    models.CharField(
                        blank=True, db_index=True, help_text="Blind review reference", max_length=16
                    ),
                ),
                (
                    "status",
                    django_fsm.FSMField(
                        choices=[
                            ("draft", "Draft"),
                            ("submitted", "Submitted"),
                            ("ineligible", "Ineligible"),
                            ("under_review", "Under review"),
                            ("shortlisted", "Shortlisted"),
                            ("awarded", "Awarded"),
                            ("rejected", "Rejected"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=32,
                        protected=True,
                    ),
                ),
                ("eligibility_notes", models.TextField(blank=True)),
                ("submitted_at", models.DateTimeField(blank=True, null=True)),
                ("created_at", models.DateTimeField(auto_now_add=True)),
                ("updated_at", models.DateTimeField(auto_now=True)),
                (
                    "applicant",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="grant_applications",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "institution",
                    models.ForeignKey(
                        blank=True,
                        null=True,
                        on_delete=django.db.models.deletion.SET_NULL,
                        related_name="grant_applications",
                        to="core.institution",
                    ),
                ),
            ],
            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=128)),
                ("file", models.FileField(upload_to=apps.core.storage.paths.upload_to_grants)),
                ("uploaded_at", models.DateTimeField(auto_now_add=True)),
                (
                    "application",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="documents",
                        to="rims_grants.application",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="Award",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("amount", models.DecimalField(decimal_places=2, max_digits=14)),
                ("currency", models.CharField(default="USD", max_length=8)),
                ("awarded_at", models.DateTimeField(auto_now_add=True)),
                ("project_end_date", models.DateField(help_text="No claims after this date")),
                ("narrative", models.TextField(blank=True)),
                (
                    "application",
                    models.OneToOneField(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="award",
                        to="rims_grants.application",
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="ConflictOfInterest",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("has_conflict", models.BooleanField(default=False)),
                ("notes", models.TextField(blank=True)),
                (
                    "application",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="conflicts",
                        to="rims_grants.application",
                    ),
                ),
                (
                    "reviewer",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL
                    ),
                ),
            ],
        ),
        migrations.CreateModel(
            name="GrantCall",
            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)),
                (
                    "call_type",
                    models.CharField(
                        choices=[
                            ("grant", "Grant"),
                            ("scholarship", "Scholarship"),
                            ("fellowship", "Fellowship"),
                            ("challenge", "Challenge"),
                        ],
                        default="grant",
                        max_length=32,
                    ),
                ),
                ("description", models.TextField(blank=True)),
                ("guidelines", models.TextField(blank=True)),
                ("opens_at", models.DateTimeField(default=django.utils.timezone.now)),
                ("closes_at", models.DateTimeField()),
                ("interview_required", models.BooleanField(default=False)),
                (
                    "budget_ceiling",
                    models.DecimalField(blank=True, decimal_places=2, max_digits=14, null=True),
                ),
                (
                    "blind_review",
                    models.BooleanField(
                        default=False, help_text="Hide applicant identity from reviewers"
                    ),
                ),
                (
                    "scoring_rubric",
                    models.JSONField(
                        blank=True,
                        default=list,
                        help_text='List of {"criterion": str, "max_points": number}',
                    ),
                ),
                (
                    "status",
                    django_fsm.FSMField(
                        choices=[
                            ("draft", "Draft"),
                            ("published", "Published"),
                            ("closed", "Closed"),
                            ("awarding", "Awarding"),
                            ("completed", "Completed"),
                        ],
                        db_index=True,
                        default="draft",
                        max_length=32,
                        protected=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="grant_calls_created",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
                (
                    "institutions_scope",
                    models.ManyToManyField(
                        blank=True,
                        help_text="Empty = all institutions eligible",
                        related_name="scoped_calls",
                        to="core.institution",
                    ),
                ),
            ],
            options={
                "ordering": ["-opens_at"],
            },
        ),
        migrations.CreateModel(
            name="EligibilityRule",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                (
                    "rule_type",
                    models.CharField(
                        choices=[
                            ("nationality", "Nationality ISO list"),
                            ("degree", "Degree level"),
                            ("institution", "Institution allow-list"),
                        ],
                        max_length=32,
                    ),
                ),
                ("params", models.JSONField(default=dict)),
                (
                    "is_blocking",
                    models.BooleanField(
                        default=True, help_text="If true, failed rule prevents review queue"
                    ),
                ),
                (
                    "call",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="eligibility_rules",
                        to="rims_grants.grantcall",
                    ),
                ),
            ],
            options={
                "ordering": ["pk"],
            },
        ),
        migrations.AddField(
            model_name="application",
            name="call",
            field=models.ForeignKey(
                on_delete=django.db.models.deletion.CASCADE,
                related_name="applications",
                to="rims_grants.grantcall",
            ),
        ),
        migrations.CreateModel(
            name="GrantCallDocument",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("label", models.CharField(max_length=128)),
                ("file", models.FileField(upload_to=apps.core.storage.paths.upload_to_grants)),
                ("sort_order", models.PositiveSmallIntegerField(default=0)),
                (
                    "call",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="documents",
                        to="rims_grants.grantcall",
                    ),
                ),
            ],
            options={
                "ordering": ["sort_order", "pk"],
            },
        ),
        migrations.CreateModel(
            name="Review",
            fields=[
                (
                    "id",
                    models.BigAutoField(
                        auto_created=True, primary_key=True, serialize=False, verbose_name="ID"
                    ),
                ),
                ("scores", models.JSONField(default=dict, help_text="Criterion key → points")),
                (
                    "total_score",
                    models.DecimalField(decimal_places=2, default=Decimal("0"), max_digits=8),
                ),
                ("recommendation", models.TextField(blank=True)),
                ("submitted_at", models.DateTimeField(blank=True, null=True)),
                (
                    "application",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="reviews",
                        to="rims_grants.application",
                    ),
                ),
                (
                    "reviewer",
                    models.ForeignKey(
                        on_delete=django.db.models.deletion.CASCADE,
                        related_name="grant_reviews",
                        to=settings.AUTH_USER_MODEL,
                    ),
                ),
            ],
        ),
        migrations.AddConstraint(
            model_name="application",
            constraint=models.UniqueConstraint(
                fields=("call", "applicant"), name="uniq_application_per_call_user"
            ),
        ),
        migrations.AddConstraint(
            model_name="review",
            constraint=models.UniqueConstraint(
                fields=("application", "reviewer"), name="uniq_review_per_reviewer"
            ),
        ),
    ]
