# -*- coding: utf-8 -*-
import sys, os, glob
sys.stdout.reconfigure(encoding='utf-8')
import openpyxl

BASE = os.path.join(os.path.dirname(os.path.abspath(__file__)), '광역_사이트맵')
WANT = {'2026-06-01': '월', '2026-06-02': '화', '2026-06-03': '수'}

def norm(v):
    if v is None: return None
    s = str(v)
    return s[:10] if len(s) >= 10 else s

rows = []
for p in glob.glob(os.path.join(BASE, '*', '*', '*.xlsx')):
    bn = os.path.basename(p)
    if bn.startswith('_') or 'backup' in bn or '~$' in bn:
        continue
    try:
        wb = openpyxl.load_workbook(p, read_only=True); ws = wb.active
        z = aa = None; nrows = 0
        for r in ws.iter_rows(min_row=3, min_col=2, max_col=27):
            b = r[0].value  # col B
            if b not in (None, ''):
                nrows += 1
                if z is None and r[24].value not in (None, ''):  # Z=col26 -> index 24 (B=0)
                    z = r[24].value; aa = r[25].value
        wb.close()
        zd = norm(z)
        if zd in WANT:
            prov = p.replace(BASE + os.sep, '').split(os.sep)[0]
            rows.append((zd, str(aa), bn.replace('.xlsx', ''), prov, nrows))
    except Exception:
        pass

rows.sort(key=lambda x: (x[0], x[1]))
print('검수일(Z) 월화수 해당 파일:')
for zd, aa, inst, prov, n in rows:
    print(f'  {zd}({WANT[zd]}) | {aa} | {inst} | {n}행')

# 집계: 프리랜서별 × 날짜별
print('\n===== 날짜별 × 프리랜서별 행 통계 =====')
fls = sorted({r[1] for r in rows})
for zd in ['2026-06-01', '2026-06-02', '2026-06-03']:
    drs = [r for r in rows if r[0] == zd]
    if not drs: continue
    print(f'\n[{zd} ({WANT[zd]})]')
    for fl in fls:
        sub = [r for r in drs if r[1] == fl]
        if sub:
            tot = sum(r[4] for r in sub)
            names = ', '.join(f'{r[2]}({r[4]})' for r in sub)
            print(f'  {fl}: {len(sub)}곳 {tot}행 — {names}')

print('\n===== 프리랜서별 3일 합계 =====')
for fl in fls:
    sub = [r for r in rows if r[1] == fl]
    tot = sum(r[4] for r in sub)
    print(f'  {fl}: {len(sub)}곳, {tot}행')
print(f'  전체: {len(rows)}곳, {sum(r[4] for r in rows)}행')
