# -*- coding: utf-8 -*-
import sys,io,json,copy
sys.stdout=io.TextIOWrapper(sys.stdout.buffer,encoding='utf-8')
import openpyxl
from openpyxl.styles import PatternFill
XL=r'10.한국장애인고용공단\한국장애인고용공단.xlsx'
plan=json.load(open(r'D:\01.프로젝트\DB수집\_temp\nshot_한국장애인고용공단\plan_한국장애인고용공단.json',encoding='utf-8'))
KEEP={3,23,26,72,75,78,132,148,150,155,156,157,158,159,179,186,187}
cand=[p['row'] for p in plan if p['auto'] in ('candidate','어문')]
wb=openpyxl.load_workbook(XL); ws=wb.active
BLUE=PatternFill(start_color='FFBDD7EE',end_color='FFBDD7EE',fill_type='solid')
def order_parts(parts):
    seq=['어문','이미지','영상','오디오','글꼴','3D','기타']
    return [x for x in seq if x in parts]
changed=0
for r in cand:
    cur=ws.cell(r,14).value or ''
    parts=set(p for p in cur.replace(' ','').split(',') if p)
    if r in KEEP:
        continue  # keep as-is (has image)
    # demote: remove 이미지
    if '이미지' in parts:
        parts.discard('이미지')
        if not parts: parts={'어문'}
        new=','.join(order_parts(parts))
        if new!=cur:
            ws.cell(r,14).value=new
            ws.cell(r,14).fill=BLUE
            changed+=1
# also mark KEEP rows whose N changed? they didn't. But blue-mark verified keeps? per SOP only changed cells blue.
wb.save(XL)
print('N demoted(이미지 제거):',changed)
# final N dist
from collections import Counter
nc=Counter()
for r in range(3,ws.max_row+1):
    if ws.cell(r,2).value is None and not any(ws.cell(r,c).value for c in range(4,11)): continue
    L=ws.cell(r,12).value
    if L=='사이트': continue
    nc[ws.cell(r,14).value]+=1
print('N dist (non-site):',dict(nc))
