# -*- coding: utf-8 -*-
"""공주시 전용 규칙 B: F 하나에 G가 여러 개이고 그 G들이 전부 L=페이지면
→ 1행으로 합치고(G/H/I/J 비움), K=첫 G의 URL, 수량(M)=G들의 M 합.
G 중 하나라도 페이지가 아니면(게시판/사이트 등) 그 그룹은 합치지 않음.

평탄화/재병합/스타일은 _dedup_all 재사용.
사용: python -X utf8 _gongju_collapse.py [--write]
"""
import sys, shutil, importlib.util, warnings
import openpyxl
warnings.filterwarnings('ignore')

spec = importlib.util.spec_from_file_location('dd', __file__.replace('_gongju_collapse.py', '_dedup_all.py'))
dd = importlib.util.module_from_spec(spec); spec.loader.exec_module(dd)

XLSX = r'D:\01.프로젝트\DB수집\작업파일\광역_사이트맵\충청남도\2.공주시\충청남도_공주시.xlsx'


def mval(x):
    try:
        return int(x)
    except Exception:
        return 1


def collapse(rows):
    out, groups = [], []
    i, n = 0, len(rows)
    while i < n:
        v = rows[i]['vals']
        D, E, F, G = v.get(4), v.get(5), v.get(6), v.get(7)
        if F not in (None, '') and G not in (None, ''):
            j = i
            while (j < n and rows[j]['vals'].get(4) == D and rows[j]['vals'].get(5) == E
                   and rows[j]['vals'].get(6) == F and rows[j]['vals'].get(7) not in (None, '')):
                j += 1
            grp = rows[i:j]
            all_page = all(g['vals'].get(12) == '페이지' for g in grp)
            leaf = all(all(g['vals'].get(c) in (None, '') for c in (8, 9, 10)) for g in grp)
            if len(grp) >= 2 and all_page and leaf:
                msum = sum(mval(g['vals'].get(13)) for g in grp)
                first = dict(grp[0]); nv = dict(first['vals'])
                for c in (7, 8, 9, 10):
                    nv[c] = None
                nv[13] = msum
                first['vals'] = nv
                out.append(first)
                groups.append({'D': D, 'E': E, 'F': F, 'n': len(grp),
                               'ms': [mval(g['vals'].get(13)) for g in grp],
                               'gs': [g['vals'].get(7) for g in grp],
                               'sum': msum, 'k': nv.get(11)})
                i = j
                continue
            out.extend(grp); i = j; continue
        out.append(rows[i]); i += 1
    return out, groups


def main():
    write = '--write' in sys.argv
    wb = openpyxl.load_workbook(XLSX)
    ws = wb.active
    rows = dd.load_flat(ws)
    out, groups = collapse(rows)
    removed = len(rows) - len(out)
    print(f'합칠 F그룹: {len(groups)}개  /  제거행 {removed}  ({len(rows)}→{len(out)})\n')
    for g in groups:
        ms = '+'.join(str(m) for m in g['ms'])
        print(f"  [{g['E']} > {g['F']}]  G {g['n']}개({'/'.join(str(x) for x in g['gs'])[:50]})")
        print(f"      → 1행, M={ms}={g['sum']}, K={g['k']}")
    if write and groups:
        bak = XLSX.replace('.xlsx', '_backup_collapse전.xlsx')
        shutil.copy(XLSX, bak)
        dd.write_back(ws, out)
        wb.save(XLSX)
        print(f'\n저장 완료. 백업: {bak}')
    elif not write:
        print('\n(DRY — 실제 적용하려면 --write)')


if __name__ == '__main__':
    main()
