#!/usr/bin/env python3
# -*- coding: utf-8 -*-

import os
import shutil
import pymysql
import re
from pathlib import Path
from datetime import datetime

# ==============================
# ⚙️ CONFIGURATION
# ==============================
DB_CONFIG = {
    "host": "192.168.1.76",
    "port": 3307,
    "user": "root",
    "password": "Vlado9674+",
    "database": "medevio",
    "charset": "utf8mb4",
}

BASE_DIR = Path(r"u:\Dropbox\Ordinace\Dokumentace_ke_zpracování\MP")
BASE_DIR.mkdir(parents=True, exist_ok=True)


def sanitize_name(name: str) -> str:
    """Replace invalid filename characters with underscore."""
    return re.sub(r'[<>:"/\\|?*\x00-\x1F]', "_", name).strip()


def make_abbrev(title: str) -> str:
    """Create abbreviation from displayTitle."""
    if not title:
        return ""
    words = re.findall(r"[A-Za-zÁ-Žá-ž0-9]+", title)
    abbr = ""
    for w in words:
        abbr += w if w.isdigit() else w[0]
    return abbr.upper()


# ==============================
# 🧹 DELETE UNEXPECTED FILES
# ==============================
def clean_folder(folder: Path, valid_files: set):
    if not folder.exists():
        return

    for f in folder.iterdir():
        if f.is_file():
            if f.name.startswith("▲"):
                continue
            sanitized = sanitize_name(f.name)
            if sanitized not in valid_files:
                print(f"🗑️ Removing unexpected file: {f.name}")
                try:
                    f.unlink()
                except Exception as e:
                    print(f"⚠️ Could not delete {f}: {e}")


# ==============================
# 📦 DB CONNECTION
# ==============================
conn = pymysql.connect(**DB_CONFIG)
cur_meta = conn.cursor(pymysql.cursors.DictCursor)
cur_blob = conn.cursor()

print("🔍 Loading ALL metadata without file_content…")

# ⭐ Load ALL metadata once (NO BLOBs)
cur_meta.execute("""
    SELECT 
        d.request_id,
        d.filename,
        d.created_at,
        p.updatedAt AS req_updated_at,
        p.pacient_jmeno AS jmeno,
        p.pacient_prijmeni AS prijmeni,
        p.displayTitle
    FROM medevio_downloads d
    JOIN pozadavky p ON d.request_id = p.id
    ORDER BY p.updatedAt DESC;
""")

rows = cur_meta.fetchall()
print(f"📋 Found {len(rows)} metadata rows.\n")

# ==============================
# 🧠 PRE-GROUP METADATA
# ==============================

# Build dictionary: request_id → all metadata rows for that request
grouped = {}
for row in rows:
    grouped.setdefault(row["request_id"], []).append(row)

unique_request_ids = list(grouped.keys())
total_requests = len(unique_request_ids)

print(f"🔄 Processing {total_requests} unique requests…\n")

# ==============================
# 🧠 MAIN LOOP
# ==============================

for idx, req_id in enumerate(unique_request_ids, start=1):
    pct = (idx / total_requests) * 100
    req_rows = grouped[req_id]
    first = req_rows[0]

    print(f"\n[ {pct:5.1f}% ] Processing request {idx}/{total_requests} → {req_id}")

    # ======================
    # Build folder name
    # ======================
    updated_at = first["req_updated_at"] or datetime.now()
    date_str = updated_at.strftime("%Y-%m-%d")
    prijmeni = sanitize_name(first["prijmeni"] or "Unknown")
    jmeno   = sanitize_name(first["jmeno"] or "")
    abbr    = make_abbrev(first["displayTitle"] or "")

    clean_folder_name = sanitize_name(f"{date_str} {prijmeni}, {jmeno} [{abbr}] {req_id}")

    # Detect existing folder
    existing_folder = None
    for f in BASE_DIR.iterdir():
        if f.is_dir() and req_id in f.name:
            existing_folder = f
            break

    main_folder = existing_folder if existing_folder else BASE_DIR / clean_folder_name
    main_folder.mkdir(parents=True, exist_ok=True)

    # ======================
    # Valid files for this request
    # ======================
    valid_files = {sanitize_name(r["filename"]) for r in req_rows}

    # Clean unexpected files
    clean_folder(main_folder, valid_files)

    # ======================
    # DOWNLOAD MISSING FILES → only now load BLOBs
    # ======================
    added_new_file = False

    for r in req_rows:
        filename = sanitize_name(r["filename"])
        dest_plain = main_folder / filename
        dest_marked = main_folder / ("▲" + filename)

        if dest_plain.exists() or dest_marked.exists():
            continue

        added_new_file = True

        # ⭐ Load BLOB only when needed
        cur_blob.execute("""
            SELECT file_content
            FROM medevio_downloads
            WHERE request_id=%s AND filename=%s
        """, (req_id, r["filename"]))

        row = cur_blob.fetchone()
        if not row or not row[0]:
            continue

        with open(dest_plain, "wb") as f:
            f.write(row[0])

        print(f"💾 Wrote: {dest_plain.relative_to(BASE_DIR)}")

    # ======================
    # Folder-level ▲ logic
    # ======================
    if added_new_file and "▲" in main_folder.name:
        new_name = main_folder.name.replace("▲", "").strip()
        new_path = main_folder.parent / new_name

        try:
            main_folder.rename(new_path)
            main_folder = new_path
            print(f"🔄 Folder flag ▲ removed → {new_name}")
        except Exception as e:
            print(f"⚠️ Could not rename folder: {e}")

cur_blob.close()
cur_meta.close()
conn.close()

print("\n🎯 Export complete.\n")
