"""MySQL connection helper that reads the same credentials as db_login.php."""
from __future__ import annotations
import os
import re
from pathlib import Path

import mysql.connector


def _parse_db_login_php() -> dict:
    """Parse db_login.php for $db_host/$db_username/$db_password/$db_database."""
    p = Path(__file__).resolve().parent.parent.parent / "db_login.php"
    text = p.read_text()
    out = {}
    for key in ("db_host", "db_username", "db_password", "db_database"):
        m = re.search(rf"\${key}\s*=\s*['\"]([^'\"]+)['\"]", text)
        if not m:
            raise RuntimeError(f"db_login.php missing ${key}")
        out[key] = m.group(1)
    return out


def connect():
    cfg = _parse_db_login_php()
    return mysql.connector.connect(
        host=cfg["db_host"],
        user=cfg["db_username"],
        password=cfg["db_password"],
        database=cfg["db_database"],
        charset="utf8mb4",
        autocommit=False,
    )


def dedup_descriptions(descriptions: list[str]) -> list[str]:
    """Normalize whitespace + case for dedup. Preserves original casing of
    the first occurrence by returning the first-seen normalized key."""
    seen = {}
    for d in descriptions:
        norm = re.sub(r"\s+", " ", d).strip().lower()
        if not norm:
            continue
        if norm not in seen:
            seen[norm] = d.strip()
    return list(seen.values())


def batched(iterable, size: int):
    batch = []
    for item in iterable:
        batch.append(item)
        if len(batch) >= size:
            yield batch
            batch = []
    if batch:
        yield batch
