# -*- coding: utf-8 -*-
"""조회 전용(저장 안 함): 부착 행 중 이미지(img_opentype) 없이
링크(licenseType)만 있는 '텍스트 부착' 행의 URL을 시·군별로 나열."""
import sys, os, openpyxl
from concurrent.futures import ThreadPoolExecutor

sys.path.insert(0, r'D:\01.프로젝트\DB수집\_스크립트')
sys.stdout.reconfigure(encoding='utf-8')

import _recheck_kogl_all as RC

O_COL, K_COL = 15, 11


def scan(name, region, weak):
    mod = RC.CN if region == 'cn' else RC.CB
    cfg = RC.get_cfg(name, region)
    xlsx, body_sel = cfg['xlsx'], cfg['body_sel']
    wb = openpyxl.load_workbook(xlsx); ws = wb.active
    targets = []
    for r in range(3, ws.max_row + 1):
        o = ws.cell(r, O_COL).value
        if o and str(o).strip() != '미부착' and ws.cell(r, K_COL).value:
            targets.append((r, str(ws.cell(r, K_COL).value).strip(), o))
    if not targets:
        return name, []
    fetch_fn = RC.make_fetch(region, weak)

    def work(t):
        r, url, oldO = t
        img_t, link_t, st = RC.detect_row(url, body_sel, mod, fetch_fn)
        return (r, url, oldO, img_t, link_t, st)

    out = []
    with ThreadPoolExecutor(max_workers=6) as ex:
        for r, url, oldO, img_t, link_t, st in ex.map(work, targets):
            if st == 'ok' and not img_t and link_t:
                out.append((r, url, str(oldO).strip(), sorted(link_t)))
    return name, sorted(out)


def main():
    sel = [a for a in sys.argv[1:] if not a.startswith('-')]
    sites = [t for t in RC.TARGETS if (not sel or t[0] in sel)]
    grand = 0
    for name, region, weak in sites:
        nm, rows = scan(name, region, weak)
        if rows:
            print(f'\n■ {nm} — 텍스트(링크)만 부착 {len(rows)}건')
            for r, url, oldO, lk in rows:
                print(f'  row{r:>3} | 기존O={oldO:<14} | 링크={lk} | {url}')
            grand += len(rows)
        else:
            print(f'■ {nm} — 없음')
    print(f'\n총 텍스트-only 부착 행: {grand}건')


if __name__ == '__main__':
    main()
