"""
Import VZP číselníku pracovišť (soubory *.Lh7) do MySQL tabulky vzp_pracoviste.
Spouštět každý týden po stažení nového souboru do složky Import/.
"""

import csv
import glob
import os
import sys
from datetime import date, datetime

# Windows konzole - povol UTF-8 výstup
if sys.stdout.encoding != "utf-8":
    import io
    sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding="utf-8", errors="replace")

import mysql.connector

DB_CONFIG = {
    "host": "192.168.1.76",
    "user": "root",
    "password": "Vlado9674+",
    "database": "medicus",
    "charset": "utf8mb4",
}

IMPORT_DIR = os.path.join(os.path.dirname(__file__), "Import")

CREATE_TABLE_SQL = """
CREATE TABLE IF NOT EXISTS vzp_pracoviste (
    id               INT           NOT NULL AUTO_INCREMENT,
    ico              CHAR(8)       NOT NULL,
    icz              CHAR(8)       NOT NULL,
    icp              CHAR(8)       NOT NULL,
    odbornost        VARCHAR(4)    NOT NULL,
    platnost_od      DATE          NOT NULL,
    platnost_do      DATE          NOT NULL,
    nazev_zarizeni   VARCHAR(200),
    nazev_pracoviste VARCHAR(200),
    ulice            VARCHAR(150),
    mesto            VARCHAR(100),
    psc              CHAR(5),
    PRIMARY KEY (id),
    INDEX idx_icp (icp),
    INDEX idx_icz (icz),
    INDEX idx_odbornost (odbornost),
    INDEX idx_platnost (platnost_od, platnost_do)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
"""

BATCH_SIZE = 1000


def parse_date(s: str) -> date | None:
    """Převede DDMMYYYY na date. Rok 3000 → 9999-12-31."""
    s = s.strip()
    if len(s) != 8:
        return None
    try:
        d, m, y = int(s[0:2]), int(s[2:4]), int(s[4:8])
        if y >= 3000:
            return date(9999, 12, 31)
        return date(y, m, d)
    except ValueError:
        return None


def find_latest_file() -> str:
    files = glob.glob(os.path.join(IMPORT_DIR, "*.Lh7"))
    if not files:
        raise FileNotFoundError(f"Žádný *.Lh7 soubor nenalezen v {IMPORT_DIR}")
    return max(files, key=os.path.getmtime)


def import_file(filepath: str, conn: mysql.connector.MySQLConnection) -> int:
    cursor = conn.cursor()
    cursor.execute("DROP TABLE IF EXISTS vzp_pracoviste")
    cursor.execute(CREATE_TABLE_SQL)
    conn.commit()

    insert_sql = """
        INSERT INTO vzp_pracoviste
            (ico, icz, icp, odbornost, platnost_od, platnost_do,
             nazev_zarizeni, nazev_pracoviste, ulice, mesto, psc)
        VALUES (%s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)
    """

    batch = []
    total = 0
    skipped = 0

    with open(filepath, encoding="cp1250", errors="replace", newline="") as f:
        reader = csv.reader(f, quotechar='"', skipinitialspace=True)
        for row in reader:
            if len(row) < 10:
                skipped += 1
                continue

            platnost_od = parse_date(row[4])
            platnost_do = parse_date(row[5])
            if platnost_od is None or platnost_do is None:
                skipped += 1
                continue

            # ulice = název ulice + číslo popisné + číslo orientační (sloupce 11,12,13)
            ulice_parts = [row[11].strip(), row[12].strip(), row[13].strip()] if len(row) > 13 else []
            ulice = " ".join(p for p in ulice_parts if p) or row[8].strip()

            psc = row[14].strip() if len(row) > 14 else ""
            if len(psc) > 5:
                psc = psc[:5]

            batch.append((
                row[0].strip(),   # ico
                row[1].strip(),   # icz
                row[2].strip(),   # icp
                row[3].strip(),   # odbornost
                platnost_od,
                platnost_do,
                row[6].strip()[:200] if len(row) > 6 else "",   # nazev_zarizeni
                row[7].strip()[:200] if len(row) > 7 else "",   # nazev_pracoviste
                ulice[:150],
                row[9].strip()[:100] if len(row) > 9 else "",   # mesto
                psc,
            ))

            if len(batch) >= BATCH_SIZE:
                cursor.executemany(insert_sql, batch)
                conn.commit()
                total += len(batch)
                batch.clear()

    if batch:
        cursor.executemany(insert_sql, batch)
        conn.commit()
        total += len(batch)

    cursor.close()
    return total, skipped


def main():
    filepath = sys.argv[1] if len(sys.argv) > 1 else find_latest_file()
    filename = os.path.basename(filepath)

    print(f"Soubor:    {filename}")
    print(f"Databáze:  {DB_CONFIG['host']}/{DB_CONFIG['database']}")
    print(f"Začátek:   {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")

    conn = mysql.connector.connect(**DB_CONFIG)
    try:
        total, skipped = import_file(filepath, conn)
    finally:
        conn.close()

    print(f"Importováno: {total} záznamů")
    if skipped:
        print(f"Přeskočeno:  {skipped} řádků (neúplná data)")
    print(f"Hotovo:    {datetime.now().strftime('%Y-%m-%d %H:%M:%S')}")


if __name__ == "__main__":
    main()
