# Generated by Django 5.2.13 on 2026-05-15 19:25

from django.db import migrations, models


def backfill_expired_listings(apps, schema_editor):
    """Convert historical rows that diverged between is_active and listing_status.

    Before this migration, expired listings only flipped ``is_active=False`` —
    ``listing_status`` was left as PUBLISHED, so dashboards counting by status
    showed stale listings. Backfill those rows to the new EXPIRED status.
    """
    from datetime import date

    JobListing = apps.get_model("rep_career", "JobListing")
    today = date.today()
    JobListing.objects.filter(
        is_active=False,
        listing_status="published",
        closes_at__lt=today,
    ).update(listing_status="expired")


class Migration(migrations.Migration):

    dependencies = [
        ("rep_career", "0007_alumnihandoff"),
    ]

    operations = [
        migrations.AlterField(
            model_name="joblisting",
            name="listing_status",
            field=models.CharField(
                choices=[
                    ("draft", "Draft"),
                    ("pending", "Pending approval"),
                    ("published", "Published"),
                    ("rejected", "Rejected"),
                    ("expired", "Expired"),
                ],
                db_index=True,
                default="published",
                max_length=16,
            ),
        ),
        migrations.RunPython(backfill_expired_listings, migrations.RunPython.noop),
    ]
