"""
Phase 1 — Step 1.9: crm_comms_log  (SQLite → Postgres)

FK to crm_customers(id) (nullable, ON DELETE SET NULL) — FK enforcement
suppressed until Phase 2 populates crm_customers.

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

import asyncio
import sys

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

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

SCRIPT = "migrate_crm_comms_log"


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

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

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

    records = []
    for r in rows:
        # attachments stored as JSON text in SQLite
        attachments = parse_json(r["attachments"], default=[])

        # is_important / is_read stored as INTEGER (0/1) in SQLite
        is_important = bool(r["is_important"]) if r["is_important"] is not None else False
        is_read      = bool(r["is_read"])      if r["is_read"]      is not None else True

        records.append({
            "id":             r["id"],
            "customer_id":    r["customer_id"],
            "type":           r["type"],
            "mail_account":   r["mail_account"],
            "direction":      r["direction"],
            "subject":        r["subject"],
            "body":           r["body"],
            "body_html":      r["body_html"],
            "attachments":    attachments,
            "ext_message_id": r["ext_message_id"],
            "from_addr":      r["from_addr"],
            "to_addrs":       r["to_addrs"],
            "logged_by":      r["logged_by"],
            "is_important":   is_important,
            "is_read":        is_read,
            "occurred_at":    parse_dt(r["occurred_at"]),
            "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(CrmCommsLog).values(records)
            stmt = stmt.on_conflict_do_nothing(index_elements=["id"])
            await session.execute(stmt)
            dest_count = await pg_count(session, "crm_comms_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())
