"""
Phase 1 — Step 1.7: crm_quotation_items  (SQLite → Postgres)

FK to crm_quotations(id) — quotations must be migrated first (step 1.6).
FK enforcement suppressed via session_replication_role for the same reason
as in migrate_crm_quotations (parent crm_customers not yet in PG).

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

import asyncio
import sys
from decimal import Decimal

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

from crm.orm import CrmQuotationItem
from migration.utils import open_sqlite, AsyncPgSession, log_run, pg_count

SCRIPT = "migrate_crm_quotation_items"


def _dec(val, default="0") -> Decimal:
    try:
        return Decimal(str(val)) if val is not None else Decimal(default)
    except Exception:
        return Decimal(default)


async def run() -> None:
    sqlite = await open_sqlite()
    rows = await sqlite.execute_fetchall(
        "SELECT * FROM crm_quotation_items ORDER BY quotation_id, sort_order"
    )
    await sqlite.close()

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

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

    records = []
    for r in rows:
        records.append({
            "id":               r["id"],
            "quotation_id":     r["quotation_id"],
            "product_id":       r["product_id"],
            "description":      r["description"],
            "description_en":   r["description_en"],
            "description_gr":   r["description_gr"],
            "unit_type":        r["unit_type"] or "pcs",
            "unit_cost":        _dec(r["unit_cost"]),
            "discount_percent": _dec(r["discount_percent"]),
            "vat_percent":      _dec(r["vat_percent"], "24"),
            "quantity":         _dec(r["quantity"], "1"),
            "line_total":       _dec(r["line_total"]),
            "sort_order":       int(r["sort_order"]) if r["sort_order"] is not None else 0,
        })

    async with AsyncPgSession() as session:
        async with session.begin():
            await session.execute(text("SET LOCAL session_replication_role = replica"))
            stmt = pg_insert(CrmQuotationItem).values(records)
            stmt = stmt.on_conflict_do_nothing(index_elements=["id"])
            await session.execute(stmt)
            dest_count = await pg_count(session, "crm_quotation_items")

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