import os, re, json
from playwright.sync_api import sync_playwright
import psycopg2

MA_LIST_URL = os.getenv("MA_LIST_URL", "https://www.metal-archives.com/lists/FR")
DB_DSN = os.getenv("DB_DSN")
STATE_PATH = os.getenv("MA_STATE_PATH", "/app/ma_state.json")

def db_conn():
    return psycopg2.connect(DB_DSN)

def upsert_band(cur, b):
    cur.execute(
        """
        INSERT INTO bands (ma_id, name, country, location_text, status, genre, data)
        VALUES (%s,%s,%s,%s,%s,%s,%s::jsonb)
        ON CONFLICT (ma_id) DO UPDATE SET
          name=EXCLUDED.name,
          country=EXCLUDED.country,
          location_text=EXCLUDED.location_text,
          status=EXCLUDED.status,
          genre=EXCLUDED.genre,
          data=EXCLUDED.data
        """,
        (b["ma_id"], b["name"], b.get("country"), b.get("location_text"),
         b.get("status"), b.get("genre"), b.get("data_json","{}"))
    )

def parse_ma_id(url: str):
    m = re.search(r"/bands/[^/]+/(\d+)", (url or ""))
    return int(m.group(1)) if m else None

def strip_html(s: str) -> str:
    return re.sub(r"<[^>]+>", "", (s or "")).strip()

def parse_row(row):
    # row = [ '<a href=".../bands/Name/123">Name</a>', 'Genre', 'Location', 'Status' ]
    if not isinstance(row, list) or len(row) < 2:
        return None
    name_html = str(row[0])
    m = re.search(r'href="([^"]+)"', name_html)
    url = m.group(1) if m else None
    name = strip_html(name_html)
    genre = strip_html(str(row[1])) if len(row) > 1 else ""
    loc = strip_html(str(row[2])) if len(row) > 2 else ""
    status = strip_html(str(row[3])) if len(row) > 3 else ""
    return name, url, genre, loc, status

def main():
    if not DB_DSN:
        raise SystemExit("DB_DSN not set")

    print(f"[worker] MA list: {MA_LIST_URL}")
    print(f"[worker] using storage_state: {STATE_PATH}")

    with sync_playwright() as p:
        browser = p.chromium.launch(headless=True)

        # charge cookies / storage state Cloudflare
        context = browser.new_context(storage_state=STATE_PATH)
        page = context.new_page()

        # Debug minimal des XHR
        xhr = []
        def on_response(resp):
            if resp.request.resource_type == "xhr":
                u = resp.url
                if "/browse/ajax-country/" in u or "/user/session" in u or "/cdn-cgi/" in u:
                    xhr.append(f"{resp.status} {u}")
        page.on("response", on_response)

        page.goto(MA_LIST_URL, wait_until="domcontentloaded")
        page.wait_for_selector("table.display", timeout=60_000)

        # Attendre la réponse DataTables (doit passer en 200 si cookie OK)
        def ok_ajax(resp):
            return ("/browse/ajax-country/" in resp.url) and resp.status == 200

        try:
            with page.expect_response(ok_ajax, timeout=60_000) as resp_info:
                page.wait_for_timeout(1000)
            dt = resp_info.value
        except Exception:
            print("[worker] ERROR: did not get 200 ajax-country within 60s")
            print("[worker] xhr seen:", xhr[:20])
            raise

        j = dt.json()
        rows = j.get("aaData") or j.get("data") or []
        print(f"[worker] ajax ok. rows={len(rows)}")

        bands = []
        for row in rows[:5]:
            parsed = parse_row(row)
            if not parsed:
                continue
            name, url, genre, location, status = parsed
            ma_id = parse_ma_id(url)
            if not ma_id:
                continue
            bands.append({
                "ma_id": ma_id,
                "name": name,
                "country": "France",
                "genre": genre,
                "location_text": location,
                "status": status or None,
                "data_json": json.dumps({"source":"ma_list_fr","url":url}),
            })

        browser.close()

    with db_conn() as conn:
        with conn.cursor() as cur:
            for b in bands:
                upsert_band(cur, b)
        conn.commit()

    print(f"[worker] inserted/updated: {len(bands)}")

if __name__ == "__main__":
    main()
