# -*- coding: utf-8 -*-
"""몽타주 candidate 행의 (행번호, 현재N, 라벨) 출력. 사용: python _n_cands.py <num>"""
import sys, io, os, glob, json
sys.stdout = io.TextIOWrapper(sys.stdout.buffer, encoding='utf-8')
import openpyxl

BASE = r"D:\01.프로젝트\DB수집\작업파일\공공기관2"
TEMP = r"D:\01.프로젝트\DB수집\_temp"

num = sys.argv[1]
fp = [x for x in glob.glob(os.path.join(BASE, f"{num}.*", "*.xlsx")) if "_backup" not in x and "~$" not in x][0]
name = os.path.basename(os.path.dirname(fp)).split('.', 1)[1]
ws = openpyxl.load_workbook(fp).active

last = {4: '', 5: '', 6: ''}
eff = {}
for r in range(3, ws.max_row + 1):
    for c in (4, 5, 6):
        v = ws.cell(r, c).value
        if v not in (None, ''):
            last[c] = v
    eff[r] = (last[4], last[5], last[6])

plan_path = os.path.join(TEMP, f"nshot_{name}", f"plan_{name}.json")
p = json.load(open(plan_path, encoding='utf-8'))
print(f"# {name} candidates (몽타주 {fp})")
for it in p:
    if it.get('auto') == 'candidate':
        r = it['row']
        d, e, f = eff.get(r, ('', '', ''))
        lab = f or e or d
        print(f"{r} | {it['N']} | imgs{it['n_imgs']} | {lab}")
