# -*- coding: utf-8 -*-
import sys,io,re
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
import openpyxl
XL=r'10.한국장애인고용공단\한국장애인고용공단.xlsx'
wb=openpyxl.load_workbook(XL); ws=wb.active
# data extent
last=2
for r in range(3,ws.max_row+1):
    if any(ws.cell(r,c).value not in (None,'') for c in range(2,12)): last=r
issues=[]
# B sequence
bs=[ws.cell(r,2).value for r in range(3,last+1)]
expected=list(range(1,len(bs)+1))
if bs!=expected: issues.append(f'B not 1..N (first mismatch at idx {next((i for i,(a,b) in enumerate(zip(bs,expected)) if a!=b),None)})')
# C name
cname=set(ws.cell(r,3).value for r in range(3,last+1) if ws.cell(r,3).value)
# merges overlap & parent boundary
from openpyxl.utils import range_boundaries
merges=list(ws.merged_cells.ranges)
cells_seen={}
ovl=0
for mr in merges:
    c0,r0,c1,r1=range_boundaries(str(mr))
    for rr in range(r0,r1+1):
        for cc in range(c0,c1+1):
            if (rr,cc) in cells_seen: ovl+=1
            cells_seen[(rr,cc)]=1
if ovl: issues.append(f'merge overlaps {ovl}')
# K hyperlink consistency
kbad=0
for r in range(3,last+1):
    v=ws.cell(r,11).value; hl=ws.cell(r,11).hyperlink
    if v and isinstance(v,str) and v.startswith('http'):
        if hl and hl.target and hl.target!=v: kbad+=1
if kbad: issues.append(f'K hyperlink mismatch {kbad}')
# page M=1, site blank
mbad=0; sbad=0
for r in range(3,last+1):
    L=ws.cell(r,12).value;M=ws.cell(r,13).value
    if L=='페이지' and M!=1: mbad+=1
    if L=='사이트' and any(ws.cell(r,c).value not in (None,'') for c in [13,14,15,16,17]): sbad+=1
if mbad: issues.append(f'page M!=1: {mbad}')
if sbad: issues.append(f'site MNOPQ not blank: {sbad}')
# L blanks
lblank=sum(1 for r in range(3,last+1) if not ws.cell(r,12).value)
if lblank: issues.append(f'L blank: {lblank}')
print('data rows:',last-2,'| C names:',cname)
from collections import Counter
print('L:',dict(Counter(ws.cell(r,12).value for r in range(3,last+1))))
print('ISSUES:',issues if issues else 'NONE - all checks passed')
