# -*- coding: utf-8 -*-
"""보은 재수집: www.boeun.go.kr(차단)→boeun.go.kr(apex) URL교정 + L빈칸 Phase2~4 채움.
사용: python -X utf8 _보은_refill.py [--write]
"""
import sys, os, shutil, importlib.util
from concurrent.futures import ThreadPoolExecutor, as_completed
import openpyxl

HERE=os.path.dirname(os.path.abspath(__file__))
sp=importlib.util.spec_from_file_location('cb',os.path.join(HERE,'_chungbuk_phase234_all.py'))
M=importlib.util.module_from_spec(sp); sp.loader.exec_module(M)
XLSX=r'D:\01.프로젝트\DB수집\작업파일\광역_사이트맵\충청북도\3.보은군\충청북도_보은군.xlsx'
BODY=['#contents','#txt','main']
sess=M.make_session()

def apexfix(u):
    return u.replace('://www.boeun.go.kr','://boeun.go.kr')

def working(u):
    s=M.fetch(sess,u)
    if s is not None: return u,s
    v=apexfix(u)
    if v!=u:
        s=M.fetch(sess,v)
        if s is not None: return v,s
    return None,None

def do_row(r,K):
    wu,soup=working(K)
    if soup is None: return r,None,K
    return r,M.process_row(sess,wu,BODY),wu

def main():
    write='--write' in sys.argv
    wb=openpyxl.load_workbook(XLSX); ws=wb.active
    # 1) www→apex URL 교정 (모든 행, 죽은링크 복구)
    urlfix=0
    for r in range(3,ws.max_row+1):
        K=ws.cell(r,11).value
        if isinstance(K,str) and '://www.boeun.go.kr' in K:
            nk=apexfix(K); urlfix+=1
            if write:
                ws.cell(r,11).value=nk
                if ws.cell(r,11).hyperlink: ws.cell(r,11).hyperlink.target=nk
    # 2) L빈칸 http행 Phase2~4
    jobs=[(r,ws.cell(r,11).value) for r in range(3,ws.max_row+1)
          if ws.cell(r,12).value in (None,'') and isinstance(ws.cell(r,11).value,str) and ws.cell(r,11).value.startswith('http')]
    res={}
    with ThreadPoolExecutor(max_workers=6) as ex:
        futs={ex.submit(do_row,r,K):r for r,K in jobs}
        for f in as_completed(futs):
            r,out,wu=f.result(); res[r]=(out,wu)
    filled=fail=0
    for r,(out,wu) in sorted(res.items()):
        if out is None: fail+=1; continue
        if write:
            if wu!=ws.cell(r,11).value:
                ws.cell(r,11).value=wu
                if ws.cell(r,11).hyperlink: ws.cell(r,11).hyperlink.target=wu
            ws.cell(r,12).value=out['L']; ws.cell(r,13).value=out['M']; ws.cell(r,14).value=out['N']
            ws.cell(r,15).value=out['O']; ws.cell(r,16).value=out['P']; ws.cell(r,17).value=out['Q']
        filled+=1
    print(f'www→apex 교정 {urlfix}행 · L빈칸채움 {filled} · 실패 {fail} · {"적용" if write else "DRY"}')
    from collections import Counter
    print('채운 L분포:',dict(Counter(v[0]['L'] for v in res.values() if v[0])))
    if write and (urlfix or filled):
        bak=XLSX.replace('.xlsx','_backup_보은재수집전.xlsx')
        if not os.path.exists(bak): shutil.copy(XLSX,bak)
        wb.save(XLSX); print('저장+백업')

if __name__=='__main__': main()
