#!/usr/bin/env python3
"""
Full wipe of product data in astara_pdb_demo_hy before re-import.

Clears ALL rows from con_sety, con_wypos2set, con_wyp2kat2mod, con_wypos,
con_opcje, con_opcje_imp, etc. Keeps shared dictionaries (con_kategorie, con_grupy).
"""
from __future__ import annotations

import pymysql

HOST = "192.168.200.4"
PORT = 3307
USER = "developer"
PASSWORD = "DEVEL!now1"
DB = "astara_pdb_demo_hy"

# Order matters: children before parents where FK-like dependencies exist.
TABLES_TO_TRUNCATE = (
    "con_op2set",
    "con_wypos2set",
    "con_wyp2kat2mod",
    "con_ak2mod",
    "con_sety",
    "con_colors",
    "con_opcje",
    "con_opcje_imp",
    "con_emisje",
    "con_footers",
    "con_modele",
    "con_wersje",
    "con_silniki",
    "con_wypos",
    "con_akces",
    "con_konfiguracje",
)


def connect() -> pymysql.connections.Connection:
    return pymysql.connect(
        host=HOST,
        port=PORT,
        user=USER,
        password=PASSWORD,
        database=DB,
        charset="utf8mb4",
        autocommit=False,
    )


def main() -> None:
    conn = connect()
    cur = conn.cursor()
    try:
        cur.execute("SET FOREIGN_KEY_CHECKS = 0")
        for table in TABLES_TO_TRUNCATE:
            cur.execute(f"DELETE FROM `{table}`")
            print(f"cleared {table}: {cur.rowcount} rows")
        cur.execute("SET FOREIGN_KEY_CHECKS = 1")
        conn.commit()

        cur.execute("SELECT COUNT(*) FROM con_modele")
        print("con_modele left:", cur.fetchone()[0])
        for table in ("con_wypos", "con_wypos2set", "con_wyp2kat2mod", "con_opcje_imp"):
            cur.execute(f"SELECT COUNT(*) FROM `{table}`")
            print(f"{table} left:", cur.fetchone()[0])
    except Exception:
        conn.rollback()
        raise
    finally:
        conn.close()


if __name__ == "__main__":
    main()
