"""Create job_briefs collection with indexes."""
from app.migrations.migrator import Migration


class Migration(Migration):
    version = "2026-04-29-000001"
    description = "Create job_briefs collection and indexes"

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

        # Ensure collection exists (insert + delete a dummy doc)
        try:
            await db.create_collection("job_briefs")
        except Exception:
            pass  # already exists

        await db.job_briefs.create_index(
            [("organization_id", 1), ("status", 1), ("created_at", -1)],
            name="idx_briefs_org_status_created",
            background=True,
        )
        await db.job_briefs.create_index(
            [("created_by", 1)],
            name="idx_briefs_created_by",
            background=True,
        )
        await db.job_briefs.create_index(
            [("project_id", 1)],
            name="idx_briefs_project_id",
            background=True,
            sparse=True,
        )
        await db.job_briefs.create_index(
            [("job_id", 1)],
            name="idx_briefs_job_id",
            background=True,
            sparse=True,
        )

    async def down(self) -> None:
        db = self.db
        await db.job_briefs.drop_index("idx_briefs_org_status_created")
        await db.job_briefs.drop_index("idx_briefs_created_by")
        await db.job_briefs.drop_index("idx_briefs_project_id")
        await db.job_briefs.drop_index("idx_briefs_job_id")
