"""
Phase 1 — Step 1.4: device_alerts  (SQLite → Postgres)

device_alerts is a "current state" table — one row per (device_serial, subsystem).
The SQLite PK is (device_serial, subsystem); Postgres adds a BIGSERIAL surrogate PK
with a unique constraint on the pair.

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

import asyncio
import sys

from sqlalchemy import text

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

SCRIPT = "migrate_device_alerts"


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

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

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

    records = [
        {
            "device_serial": r["device_serial"],
            "subsystem":     r["subsystem"],
            "state":         r["state"],
            "message":       r["message"],
            "updated_at":    parse_dt(r["updated_at"]),
        }
        for r in rows
    ]

    async with AsyncPgSession() as session:
        async with session.begin():
            await session.execute(
                text("""
                    INSERT INTO device_alerts (device_serial, subsystem, state, message, updated_at)
                    VALUES (:device_serial, :subsystem, :state, :message, :updated_at)
                    ON CONFLICT (device_serial, subsystem) DO NOTHING
                """),
                records,
            )
            dest_count = await pg_count(session, "device_alerts")

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