"""Backfill source_has_ad=False on existing jobs and job_briefs."""
from app.migrations.migrator import Migration


class Migration(Migration):
    version = "2026-05-08-000000"
    description = "Add source_has_ad field to jobs.source and job_briefs"

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

        jobs_result = await db.jobs.update_many(
            {"source.source_has_ad": {"$exists": False}},
            {"$set": {"source.source_has_ad": False}},
        )
        briefs_result = await db.job_briefs.update_many(
            {"source_has_ad": {"$exists": False}},
            {"$set": {"source_has_ad": False}},
        )

        print(f"✅ Backfilled source_has_ad on {jobs_result.modified_count} jobs, {briefs_result.modified_count} job_briefs")

    async def down(self) -> None:
        db = self.db
        await db.jobs.update_many({}, {"$unset": {"source.source_has_ad": ""}})
        await db.job_briefs.update_many({}, {"$unset": {"source_has_ad": ""}})
