# -*- coding: utf-8 -*-
import openpyxl, io
from collections import defaultdict
wb=openpyxl.load_workbook("한국지역난방공사.xlsx")
ws=wb.active
out=io.open("_dupscan2_out.txt","w",encoding="utf-8")
def w(*a): out.write(" ".join(str(x) for x in a)+"\n")
rows=[]
for r in range(3, ws.max_row+1):
    B=ws.cell(r,2).value
    d=[ws.cell(r,c).value for c in range(4,11)]
    K=ws.cell(r,11).value
    L=ws.cell(r,12).value; M=ws.cell(r,13).value; N=ws.cell(r,14).value
    leaf=""
    for v in d:
        if v not in (None,""): leaf=str(v).strip()
    rows.append(dict(r=r,B=B,d=d,K=K,L=L,M=M,N=N,leaf=leaf,
                     path=" > ".join(str(x).strip() for x in d if x not in (None,""))))

# duplicate by full path (labels only, ignore K)
byPath=defaultdict(list)
for x in rows: byPath[x["path"]].append(x)
w("=== same full label path (ignore URL) ===")
for p,xs in byPath.items():
    if len(xs)>1:
        for x in xs: w("  row",x["r"],"B="+str(x["B"]),"L="+str(x["L"]),"M="+str(x["M"]),"K="+str(x["K"]))
        w("  PATH:",p,"\n")

# duplicate by leaf label + same L + same M (likely true dup)
byLeaf=defaultdict(list)
for x in rows: byLeaf[(x["leaf"],str(x["L"]),str(x["M"]))].append(x)
w("=== same (leaf + L + M) ===")
for k,xs in byLeaf.items():
    if len(xs)>1:
        for x in xs: w("  row",x["r"],"B="+str(x["B"]),"path="+x["path"],"K="+str(x["K"]))
        w("  ---",k,"\n")

# parent-child URL equal (1-5): consecutive rows, parent path is prefix and same K
w("=== parent row whose K equals a child's K (1-5 candidate) ===")
byK=defaultdict(list)
for x in rows:
    if x["K"]: byK[str(x["K"]).strip()].append(x)
# already none. check normalized (strip trailing params after menuNo?)
w("(identical K groups:", sum(1 for v in byK.values() if len(v)>1),")")
out.close()
print("done")
