#!/usr/bin/env python3
"""Pastikan jejak MERR: akun Repliz 'RSIA' milik siapa, dan domain 'kendangsari' di job cache milik siapa."""
import json, re, sys

WS = "/home/mantapman/.hermes/profiles/atun/workspace"
sys.path.insert(0, WS)
from repliz_client import get_client, ACC          # noqa: E402

print("=== 1. detail akun Repliz yang berkaitan dengan RSIA/Kendangsari/MERR ===")
client = get_client()
d = client.get("/public/account?page=1&limit=100")
docs = d.get("docs", []) if isinstance(d, dict) else []
NAMA = {getattr(ACC, n): n for n in dir(ACC) if not n.startswith("_")}
for a in docs:
    nama = str(a.get("name") or "")
    if re.search(r"rsia|kendangsari|merr|hospital", nama, re.I):
        lengkap = client.get(f"/public/account/{a['_id']}") if hasattr(client, "get") else {}
        print(f"   {a['_id']} | nama: {nama}")
        print(f"      ACC: {NAMA.get(a['_id'], '?')} | platform: {a.get('platform')} | jenis: {a.get('type')}")
        for k in ("username", "pageId", "businessId", "profileUrl", "url"):
            if isinstance(lengkap, dict) and lengkap.get(k):
                print(f"      {k}: {str(lengkap[k])[:80]}")

print("\n=== 2. domain di job WP Rocket Cache Rebuild ===")
j = json.load(open("/home/mantapman/.hermes/cron/jobs.json"))
job = [x for x in j["jobs"] if x.get("id") == "d0b98dc7afbc"]
if job:
    teks = str(job[0].get("prompt"))
    for m in re.finditer(r".{0,80}kendangsari.{0,120}", teks, re.I):
        print("   »", re.sub(r"\s+", " ", m.group(0))[:200])
    doms = sorted(set(re.findall(r"[a-z0-9\-]+\.(?:id|com|co\.id)", teks.lower())))
    print("   semua domain:", doms)

print("\n=== 3. cek langsung: apakah kedua domain merr hidup dan siapa pemiliknya ===")
import urllib.request
UA = {"User-Agent": "Mozilla/5.0"}
for u in ("https://merr.kendangsari.com/", "https://www.rsiamerr.com/", "https://rsia.kendangsari.com/"):
    try:
        r = urllib.request.urlopen(urllib.request.Request(u, headers=UA), timeout=60)
        h = r.read().decode("utf-8", "replace")
        t = re.search(r"<title[^>]*>(.*?)</title>", h, re.S)
        print(f"   {u:<38} {r.status} | {re.sub('<[^>]+>', '', t.group(1)).strip()[:60] if t else '-'}")
    except Exception as e:
        print(f"   {u:<38} GAGAL {str(e)[:60]}")
