"""
Phase 1 — Step 1.10: commands  (SQLite → Postgres)

commands is a raw-SQL table (no ORM model). BIGSERIAL PK — SQLite integer IDs
are NOT preserved; rows are inserted in sent_at order.

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

import asyncio
import sys

from sqlalchemy import text

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

SCRIPT = "migrate_commands"


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

    source_count = len(rows)
    print(f"Source (SQLite): {source_count} commands 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"],
            "command_name":     r["command_name"],
            "command_payload":  r["command_payload"],
            "status":           r["status"] or "pending",
            "response_payload": r["response_payload"],
            "sent_at":          parse_dt(r["sent_at"]),
            "responded_at":     parse_dt(r["responded_at"]),
        }
        for r in rows
    ]

    async with AsyncPgSession() as session:
        async with session.begin():
            await session.execute(
                text("""
                    INSERT INTO commands
                        (device_serial, command_name, command_payload, status,
                         response_payload, sent_at, responded_at)
                    VALUES
                        (:device_serial, :command_name, :command_payload, :status,
                         :response_payload, :sent_at, :responded_at)
                """),
                records,
            )
            dest_count = await pg_count(session, "commands")

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