import os, subprocess, json, shutil
from pathlib import Path

root=Path(__file__).resolve().parent
out=root/'output'
out.mkdir(parents=True,exist_ok=True)
exe=root/'officecli.exe'
env=os.environ.copy(); env['OFFICECLI_SKIP_UPDATE']='1'
log=[]
def run(*args):
    r=subprocess.run([str(exe),*args],cwd=out,env=env,capture_output=True,encoding='utf-8',errors='replace')
    log.append({'args':list(args),'exit_code':r.returncode,'stdout':r.stdout,'stderr':r.stderr})
    (out/'run-log.json').write_text(json.dumps(log,ensure_ascii=False,indent=2),encoding='utf-8')
    if r.returncode: raise RuntimeError(r.stderr or r.stdout)
    return r.stdout
run('--version')
run('create','weekly-report.pptx')
run('add','weekly-report.pptx','/','--type','slide','--prop','title=주간 문의 자동화 실험','--prop','background=F8FAFC')
run('add','weekly-report.pptx','/slide[1]','--type','shape','--prop','text=가상 업무 자료 · 실제 고객 데이터 아님','--prop','x=2cm','--prop','y=4cm','--prop','width=26cm','--prop','height=1.4cm','--prop','size=18','--prop','color=475569','--prop','font=Malgun Gothic')
run('add','weekly-report.pptx','/slide[1]','--type','shape','--prop','text=검토 대상 40건 / 자동 분류 28건 / 사람 확인 12건','--prop','x=2cm','--prop','y=6cm','--prop','width=26cm','--prop','height=2cm','--prop','size=24','--prop','color=0F172A','--prop','font=Malgun Gothic')
run('add','weekly-report.pptx','/slide[1]','--type','shape','--prop','text=자동 분류율 80%','--prop','x=2cm','--prop','y=9cm','--prop','width=26cm','--prop','height=2cm','--prop','size=30','--prop','color=1D4ED8','--prop','font=Malgun Gothic')
run('view','weekly-report.pptx','html','--out','before.html')
shutil.copy2(out/'weekly-report.pptx',out/'before.pptx')
print(run('view','weekly-report.pptx','outline'))
print(run('get','weekly-report.pptx','/slide[1]','--json'))
# Finish the controlled correction example. The wrong percentage is intentional,
# not an observed OfficeCLI arithmetic bug or a real business metric.
run('set','weekly-report.pptx','/slide[1]/shape[@id=100002]','--prop','text=자동 분류율 70% = 28 ÷ 40 × 100')
run('view','weekly-report.pptx','html','--out','after.html')
run('validate','weekly-report.pptx')
run('view','weekly-report.pptx','text')
(out/'input.json').write_text(json.dumps({'synthetic':True,'total':40,'automatically_classified':28,'human_review':12,'expected_percent':70,'intentional_wrong_percent':80},ensure_ascii=False,indent=2),encoding='utf-8')
