#!/usr/bin/env python3
"""
Fix creation/modified dates on all migrated documents.
Sets creation = original legacy date (converted from unix timestamp).
Fixes Issues, Sales Invoices, Customers, Communications.
"""
import pymysql
import psycopg2
from datetime import datetime, timezone

LEGACY = {"host": "legacy-db", "user": "facturation", "password": "***LEGACY-DB-PW-REDACTED***",
          "database": "gestionclient", "connect_timeout": 30, "read_timeout": 600}
PG = {"host": "db", "port": 5432, "user": "postgres", "password": "123",
      "dbname": "_eb65bdc0c4b1b2d6"}

def ts_to_dt(unix_ts):
    if not unix_ts or unix_ts <= 0:
        return None
    try:
        return datetime.fromtimestamp(int(unix_ts), tz=timezone.utc).strftime("%Y-%m-%d %H:%M:%S")
    except:
        return None

def log(msg):
    print(msg, flush=True)

def main():
    log("=== Fix Creation/Modified Dates ===")

    mc = pymysql.connect(**LEGACY)
    cur = mc.cursor(pymysql.cursors.DictCursor)

    # 1. Tickets → Issues
    log("Loading ticket dates...")
    cur.execute("SELECT id, date_create, last_update, date_closed FROM ticket ORDER BY id")
    ticket_dates = {r["id"]: r for r in cur.fetchall()}

    # 2. Invoices → Sales Invoices
    log("Loading invoice dates...")
    cur.execute("SELECT id, date_orig FROM invoice WHERE billing_status = 1 ORDER BY id")
    inv_dates = {r["id"]: r["date_orig"] for r in cur.fetchall()}

    # 3. Accounts → Customers
    log("Loading account dates...")
    cur.execute("SELECT id, date_orig, date_last FROM account ORDER BY id")
    acct_dates = {r["id"]: r for r in cur.fetchall()}

    mc.close()
    log("  Loaded {} ticket, {} invoice, {} account dates".format(
        len(ticket_dates), len(inv_dates), len(acct_dates)))

    pg = psycopg2.connect(**PG)
    pgc = pg.cursor()

    # Fix Issues
    log("")
    log("--- Fixing Issue dates ---")
    pgc.execute('SELECT name, legacy_ticket_id FROM "tabIssue" WHERE legacy_ticket_id > 0')
    issues = pgc.fetchall()
    fixed_issues = 0
    for name, tid in issues:
        t = ticket_dates.get(tid)
        if not t:
            continue
        created = ts_to_dt(t.get("date_create"))
        modified = ts_to_dt(t.get("last_update")) or created
        if created:
            pgc.execute('UPDATE "tabIssue" SET creation = %s, modified = %s WHERE name = %s',
                         (created, modified or created, name))
            fixed_issues += 1
    pg.commit()
    log("  {} Issues dates fixed".format(fixed_issues))

    # Fix Sales Invoices
    log("")
    log("--- Fixing Sales Invoice dates ---")
    pgc.execute('SELECT name, legacy_invoice_id FROM "tabSales Invoice" WHERE legacy_invoice_id > 0')
    sinvs = pgc.fetchall()
    fixed_inv = 0
    for name, inv_id in sinvs:
        date_orig = inv_dates.get(inv_id)
        created = ts_to_dt(date_orig)
        if created:
            pgc.execute('UPDATE "tabSales Invoice" SET creation = %s, modified = %s WHERE name = %s',
                         (created, created, name))
            fixed_inv += 1
    pg.commit()
    log("  {} Sales Invoice dates fixed".format(fixed_inv))

    # Fix Customers
    log("")
    log("--- Fixing Customer dates ---")
    pgc.execute('SELECT name, legacy_account_id FROM "tabCustomer" WHERE legacy_account_id > 0')
    custs = pgc.fetchall()
    fixed_cust = 0
    for name, aid in custs:
        a = acct_dates.get(aid)
        if not a:
            continue
        created = ts_to_dt(a.get("date_orig"))
        modified = ts_to_dt(a.get("date_last")) or created
        if created:
            pgc.execute('UPDATE "tabCustomer" SET creation = %s, modified = %s WHERE name = %s',
                         (created, modified or created, name))
            fixed_cust += 1
    pg.commit()
    pg.close()
    log("  {} Customer dates fixed".format(fixed_cust))

    log("")
    log("=" * 60)
    log("Issues:    {} dates fixed".format(fixed_issues))
    log("Invoices:  {} dates fixed".format(fixed_inv))
    log("Customers: {} dates fixed".format(fixed_cust))
    log("=" * 60)

if __name__ == "__main__":
    main()
