"""
Phase 1 — Step 1.3: mfg_audit_log  (SQLite → Postgres)

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

import asyncio
import sys

from sqlalchemy import text

from migration.utils import open_sqlite, AsyncPgSession, parse_dt, log_run, pg_count

SCRIPT = "migrate_mfg_audit_log"


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

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

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

    # mfg_audit_log uses a BIGSERIAL PK — we don't preserve SQLite integer IDs
    # because the Postgres sequence will assign new ones. We insert in the same
    # timestamp order so the audit trail remains coherent.
    records = [
        {
            "timestamp":     parse_dt(r["timestamp"]),
            "admin_user":    r["admin_user"],
            "action":        r["action"],
            "serial_number": r["serial_number"],
            "detail":        r["detail"],
        }
        for r in rows
    ]

    async with AsyncPgSession() as session:
        async with session.begin():
            await session.execute(
                text("""
                    INSERT INTO mfg_audit_log (timestamp, admin_user, action, serial_number, detail)
                    VALUES (:timestamp, :admin_user, :action, :serial_number, :detail)
                """),
                records,
            )
            dest_count = await pg_count(session, "mfg_audit_log")

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