"""
Phase 1 — Step 1.1: melody_drafts  (SQLite → Postgres)

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

import asyncio
import json
import sys

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

from melodies.orm import MelodyDraft
from migration.utils import open_sqlite, AsyncPgSession, parse_dt, parse_json, log_run, pg_count

SCRIPT = "migrate_melody_drafts"


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

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

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

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

        records.append({
            "id":         r["id"],
            "status":     r["status"] or "draft",
            "data":       data,
            "created_at": parse_dt(r["created_at"]),
            "updated_at": parse_dt(r["updated_at"]),
        })

    async with AsyncPgSession() as session:
        async with session.begin():
            stmt = pg_insert(MelodyDraft).values(records)
            stmt = stmt.on_conflict_do_nothing(index_elements=["id"])
            await session.execute(stmt)
            dest_count = await pg_count(session, "melody_drafts")

    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())
