"""
Phase 1 — Step 1.8: crm_media  (SQLite → Postgres)

FK to crm_customers(id) (nullable) — FK enforcement suppressed until Phase 2
populates crm_customers.

Run on VPS:
    docker compose exec backend python -m migration.migrate_crm_media
"""

import asyncio
import sys

from sqlalchemy import text
from sqlalchemy.dialects.postgresql import insert as pg_insert

from crm.orm import CrmMedia
from migration.utils import open_sqlite, AsyncPgSession, parse_dt, parse_json, log_run, pg_count

SCRIPT = "migrate_crm_media"


async def run() -> None:
    sqlite = await open_sqlite()
    rows = await sqlite.execute_fetchall("SELECT * FROM crm_media ORDER BY created_at")
    await sqlite.close()

    source_count = len(rows)
    print(f"Source (SQLite): {source_count} crm_media rows")

    if source_count == 0:
        print("Nothing to migrate.")
        await log_run(SCRIPT, 0, 0, notes="source empty")
        return

    records = []
    for r in rows:
        # SQLite stores tags as JSON text; Postgres column is JSONB
        tags_raw = r["tags"]
        tags = parse_json(tags_raw, default=[])

        records.append({
            "id":             r["id"],
            "customer_id":    r["customer_id"],
            "order_id":       r["order_id"],
            "filename":       r["filename"],
            "nextcloud_path": r["nextcloud_path"],
            "thumbnail_path": r["thumbnail_path"],
            "mime_type":      r["mime_type"],
            "direction":      r["direction"],
            "tags":           tags,
            "uploaded_by":    r["uploaded_by"],
            "created_at":     parse_dt(r["created_at"]),
        })

    async with AsyncPgSession() as session:
        async with session.begin():
            await session.execute(text("SET LOCAL session_replication_role = replica"))
            stmt = pg_insert(CrmMedia).values(records)
            stmt = stmt.on_conflict_do_nothing(index_elements=["id"])
            await session.execute(stmt)
            dest_count = await pg_count(session, "crm_media")

    if dest_count < source_count:
        msg = f"Count mismatch: source={source_count}  postgres={dest_count}"
        print(f"ERROR: {msg}", file=sys.stderr)
        await log_run(SCRIPT, source_count, dest_count, success=False, notes=msg)
        sys.exit(1)

    print(f"Postgres: {dest_count} rows ✓")
    await log_run(SCRIPT, source_count, dest_count)


if __name__ == "__main__":
    asyncio.run(run())
