# -*- coding: utf-8 -*-
"""고유(36~42)·즐겨찾는(24~35) 카테고리별 건수 — 페이저 마지막offset+행수로 총계."""
import sys, io, json, re
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
from playwright.sync_api import sync_playwright

JOBS = {
    "고유정보목록": ("https://www.hf.go.kr/ko/sub06/sub06_04_02.do",
        [("전체",""),("주택구입","36"),("주택보증","37"),("주택연금","38"),("유동화증권","39"),("신용회복","40"),("재무","41"),("기타","42")]),
    "즐겨찾는목록": ("https://www.hf.go.kr/ko/sub06/sub06_04_03.do",
        [("전체",""),("감사","24"),("윤리","25"),("기획","26"),("재정","27"),("법무","28"),("노무","29"),("인사","30"),("총무","31"),("회계","32"),("정보화","33"),("홍보","34"),("대외","35")]),
}
CNT_JS = """
() => {
  const t=document.body.innerText;
  let m=t.match(/총\\s*([0-9,]+)\\s*건/)||t.match(/전체\\s*:?\\s*([0-9,]+)/);
  // 페이저 마지막 offset
  let lastOff=0;
  document.querySelectorAll('a').forEach(a=>{const h=a.getAttribute('href')||'';const mm=h.match(/article\\.offset=([0-9]+)/);if(mm)lastOff=Math.max(lastOff,+mm[1]);});
  const rows=document.querySelectorAll('table tbody tr').length;
  return JSON.stringify({txt:m?m[1].replace(/,/g,''):null, lastOff, rows});
}
"""
out={}
with sync_playwright() as p:
    br=p.chromium.launch(headless=True)
    pg=br.new_context(ignore_https_errors=True,viewport={'width':1600,'height':1000}).new_page()
    for bname,(burl,cats) in JOBS.items():
        out[bname]={"url":burl,"cats":[]}
        for cname,cv in cats:
            u=f"{burl}?mode=list&srCategoryId={cv}" if cv else f"{burl}?mode=list"
            try:
                pg.goto(u,wait_until='domcontentloaded',timeout=20000); pg.wait_for_timeout(700)
                r=json.loads(pg.evaluate(CNT_JS))
                # 마지막 페이지로 가서 총계 확정
                tot=r['txt']
                if tot is None and r['rows']>0:
                    if r['lastOff']>0:
                        pg.goto(f"{u}&article.offset={r['lastOff']}&articleLimit=5",wait_until='domcontentloaded',timeout=20000); pg.wait_for_timeout(500)
                        last_rows=pg.evaluate("()=>document.querySelectorAll('table tbody tr').length")
                        tot=str(r['lastOff']+last_rows)
                    else:
                        tot=str(r['rows'])
            except Exception as e:
                tot=None; r={'err':str(e)[:30]}
            out[bname]["cats"].append({"name":cname,"v":cv,"tot":tot})
            print(f"  {bname} {cname}({cv}): {tot}")
    br.close()
json.dump(out,open("_hf_cat2.json","w",encoding="utf-8"),ensure_ascii=False,indent=1)
print("saved")
