"""Replace status enum in $jsonSchema validator with the full current list."""
from app.migrations.migrator import Migration

ALL_STATUSES = [
    "created", "ingesting", "ai_processing",
    "pending_qc", "approved_english", "approved_source",
    "rejected", "qc_feedback",
    "translating", "tts_generating", "tts_failed",
    "rendering_video", "render_failed", "rendering_qc",
    "pending_final_review", "completed",
    "processing_failed", "cancelled",
]


class Migration(Migration):
    version = "2026-04-30-000002"
    description = "Fix status enum in jobs $jsonSchema validator (add processing_failed + cancelled)"

    async def up(self) -> None:
        db = self.db

        result = await db.command("listCollections", filter={"name": "jobs"})
        batch = result.get("cursor", {}).get("firstBatch", [])
        if not batch:
            return

        existing_validator = batch[0].get("options", {}).get("validator")
        if not existing_validator:
            return

        schema = existing_validator.get("$jsonSchema", {})
        status_prop = schema.get("properties", {}).get("status")
        if not status_prop:
            return

        status_prop["enum"] = ALL_STATUSES

        await db.command(
            "collMod",
            "jobs",
            validator=existing_validator,
            validationLevel="moderate",
            validationAction="error",
        )

    async def down(self) -> None:
        pass
