import requests, json, io, time
from bs4 import BeautifulSoup
from urllib.parse import urljoin
import urllib3; urllib3.disable_warnings()
H={'User-Agent':'Mozilla/5.0'}
BASE="https://www.kdhc.co.kr"
def menuno(u):
    import re
    m=re.search(r'menuNo=(\d+)',u or '')
    return m.group(1) if m else None
def fetch(u):
    r=requests.get(u,headers=H,timeout=25,verify=False)
    return BeautifulSoup(r.content,'html.parser')

data=json.load(io.open('_dump.json',encoding='utf-8'))
# unique urls (Klink) -> rows
seen={}
for row in data:
    k=row.get('Klink') or row.get('K')
    if not k: continue
    seen.setdefault(k,[]).append(row)

groups=[]  # each: list of (label,href,menuNo,active)
def gettabs(s):
    res=[]
    for ul in s.find_all('ul'):
        lis=ul.find_all('li',class_='tebLi')
        if not lis: continue
        tabs=[]
        for li in lis:
            a=li.find('a')
            if not a: continue
            href=a.get('href') or ''
            cls=a.get('class') or []
            tabs.append((a.get_text(strip=True), href, 'active' in cls))
        if len(tabs)>=2:
            res.append(tabs)
    return res

allgroups={}
errs=[]
for i,(k,rows) in enumerate(sorted(seen.items())):
    try:
        s=fetch(k)
        tg=gettabs(s)
    except Exception as e:
        errs.append((k,str(e))); continue
    for tabs in tg:
        key=tuple(sorted((t[0],menuno(t[1]) or t[1]) for t in tabs))
        if key not in allgroups:
            allgroups[key]=tabs
    time.sleep(0.15)

# sheet menuNos present
sheet_mn={}
for row in data:
    k=row.get('Klink') or row.get('K') or ''
    mn=menuno(k)
    if mn: sheet_mn.setdefault(mn,[]).append(row['r'])

out=[]
for key,tabs in allgroups.items():
    info=[]
    for lbl,href,act in tabs:
        mn=menuno(href)
        absu=urljoin(BASE,href) if href.startswith('/') else href
        in_sheet = mn in sheet_mn if mn else False
        is_board = '/bbs/' in href or '/list.do' in href
        info.append({'label':lbl,'href':href,'url':absu,'menuNo':mn,'active':act,'in_sheet':in_sheet,'sheet_rows':sheet_mn.get(mn,[]),'board':is_board})
    out.append(info)

io.open('_tabgroups.json','w',encoding='utf-8').write(json.dumps(out,ensure_ascii=False,indent=1))
print('total page/board fetched:',len(seen))
print('distinct tab groups:',len(out))
print('errors:',len(errs))
for e in errs[:10]: print('  ERR',e)
print('=== GROUPS ===')
for g in out:
    present=sum(1 for t in g if t['in_sheet'])
    print(f'[{present}/{len(g)} in sheet]')
    for t in g:
        print('   ',('*' if t['active'] else ' '),'SHEET' if t['in_sheet'] else '  NEW',t['menuNo'],('BOARD' if t['board'] else 'page '),t['label'],'rows=',t['sheet_rows'])
