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

import os
import zlib
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()


# ==============================
# 📦 STREAMING EXPORT WITH TRIANGLE CHECK
# ==============================
conn = pymysql.connect(**DB_CONFIG)
cur_meta = conn.cursor(pymysql.cursors.DictCursor)
cur_blob = conn.cursor()

cur_meta.execute("""
    SELECT id, request_id, attachment_id, filename, pacient_jmeno,
           pacient_prijmeni, created_at, downloaded_at
    FROM medevio_downloads
    WHERE file_content IS NOT NULL;
""")

rows = cur_meta.fetchall()
print(f"📋 Found {len(rows)} records to check/export")

skipped, exported = 0, 0

for r in rows:
    try:
        created = r["created_at"] or r["downloaded_at"] or datetime.now()
        date_str = created.strftime("%Y-%m-%d")

        prijmeni = sanitize_name(r["pacient_prijmeni"] or "Unknown")
        jmeno = sanitize_name(r["pacient_jmeno"] or "")

        crc = f"{zlib.crc32(r['request_id'].encode('utf-8')) & 0xFFFFFFFF:08X}"

        # Base (non-triangle) and processed (triangle) folder variants
        base_folder = sanitize_name(f"{date_str} {prijmeni}, {jmeno} {crc}")
        tri_folder = sanitize_name(f"{date_str}▲ {prijmeni}, {jmeno} {crc}")

        base_path = BASE_DIR / base_folder
        tri_path = BASE_DIR / tri_folder

        filename = sanitize_name(r["filename"] or f"unknown_{r['id']}.bin")
        file_path_base = base_path / filename
        file_path_tri = tri_path / filename

        # 🟡 Skip if exists in either version
        if file_path_base.exists() or file_path_tri.exists():
            skipped += 1
            found_in = "▲" if file_path_tri.exists() else ""
            print(f"⏭️ Skipping existing{found_in}: {filename}")
            continue

        # Make sure base folder exists before saving
        base_path.mkdir(parents=True, exist_ok=True)

        # 2️⃣ Fetch blob
        cur_blob.execute("SELECT file_content FROM medevio_downloads WHERE id = %s", (r["id"],))
        blob = cur_blob.fetchone()[0]

        if blob:
            with open(file_path_base, "wb") as f:
                f.write(blob)
            exported += 1
            print(f"✅ Saved: {file_path_base.relative_to(BASE_DIR)}")
        else:
            print(f"⚠️ No content for id={r['id']}")

    except Exception as e:
        print(f"❌ Error for id={r['id']}: {e}")

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

print(f"\n🎯 Export complete — {exported} new files saved, {skipped} skipped.\n")
