#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""Dump tabel link MATI (404) di jadwal pending ke depan -> /tmp/link_mati.json
Read-only. Dipakai untuk perbaikan caption."""
import os, re, sys, json, subprocess
from concurrent.futures import ThreadPoolExecutor
sys.path.insert(0, "/home/mantapman/.hermes/profiles/atun/workspace")
from repliz_client import ReplizClient

c = ReplizClient()
all_docs, page = [], 1
while page < 80:
    res = c.get_schedules(page=page, limit=100)
    docs = res.get("docs", res.get("data", [])) if isinstance(res, dict) else res
    if not docs:
        break
    all_docs += docs
    if len(docs) < 100:
        break
    page += 1

URL_RE = re.compile(r"https?://(?:www\.)?studiosoft\.id[^\s\"'<>)\]]*")
rows = []
for d in all_docs:
    if d.get("status") != "pending":
        continue
    txt = (d.get("description") or "") + " " + json.dumps(d.get("replies") or [])
    for u in set(URL_RE.findall(txt)):
        rows.append((u.rstrip(".,)"), d))

uniq = sorted({u for u, _ in rows})
print("jadwal pending:", sum(1 for d in all_docs if d.get("status") == "pending"),
      "| entri link:", len(rows), "| URL unik:", len(uniq))


def cek(u):
    r = subprocess.run(["curl", "-4", "-s", "-o", "/dev/null", "-w", "%{http_code}",
                        "-L", "--max-time", "35", u], capture_output=True, text=True)
    return u, r.stdout.strip()


with ThreadPoolExecutor(max_workers=8) as ex:
    hasil = dict(ex.map(cek, uniq))

mati = {u: s for u, s in hasil.items() if s not in ("200", "301", "302")}
print("URL 200/301/302:", sum(1 for s in hasil.values() if s in ("200", "301", "302")))
print("URL MATI:", len(mati))

out = {}
for u, s in sorted(mati.items()):
    pakai = [d for uu, d in rows if uu == u]
    out[u] = {
        "kode": s,
        "n": len(pakai),
        "jadwal": [{
            "id": d.get("id"),
            "akun": (d.get("account") or {}).get("name"),
            "acc_id": (d.get("accountId") or (d.get("account") or {}).get("_id")),
            "at": d.get("scheduleAt"),
            "tipe": d.get("type"),
            "title": d.get("title"),
            "media": [m.get("url") for m in (d.get("medias") or [])],
        } for d in pakai],
    }

json.dump(out, open("/tmp/link_mati.json", "w"), indent=1, ensure_ascii=False)
tot = sum(v["n"] for v in out.values())
print(f"-> /tmp/link_mati.json ({len(out)} URL, {tot} jadwal)")
for u, v in out.items():
    print(f"  [{v['kode']}] x{v['n']:3}  {u}")
