from pathlib import Path
from datetime import datetime
import shutil,re
from docx import Document
from docx.shared import Cm
from docx.enum.text import WD_ALIGN_PARAGRAPH
R=Path(r'C:\Users\Asus\Desktop\TourAppProject\travelin_app\docs\thesis_appendices');stamp=datetime.now().strftime('%Y%m%d_%H%M%S')
cp=R/'checkpoints'/('continuation_'+stamp);cp.mkdir()
d=Document(R/'TourApp_Appendix_A_B.docx')
items={
'การเตรียมฐานข้อมูลสำหรับการติดตั้งใหม่':('database_files_20261003.png','ตำแหน่งไฟล์ SQL และ Migration ของโครงการ','ภาพแสดงรายชื่อไฟล์เท่านั้น ไม่ใช่ผลนำเข้าฐานข้อมูลหรือชุดติดตั้ง schema-only ที่ตรวจแล้ว ต้องแยกไฟล์โครงสร้างออกจาก dump, demo, cleanup และ reset ตามคำอธิบายด้านล่าง','ก.3'),
'การตั้งค่าบริการ Dart และผู้ให้บริการภายนอก':('config_files_20261003.png','ตำแหน่งไฟล์ตั้งค่าท้องถิ่นและไฟล์ตัวอย่าง','ภาพแสดงตำแหน่งไฟล์ตั้งค่าใน config โดยไม่แสดงค่าภายใน การตั้งค่า environment ของบริการและผู้ให้บริการอธิบายด้านล่าง ไม่คัดลอกค่าลับจากไฟล์จริงลงเอกสาร','ก.4'),
'การเปิดระบบด้วย start_tourapp bat':('launcher_file_20261003.png','ตำแหน่งไฟล์ Launcher ของระบบ','ภาพแสดงตำแหน่ง start_tourapp ในโฟลเดอร์โครงการ Windows Explorer ซ่อนนามสกุล .bat ภาพนี้ไม่ใช่หลักฐานว่าตัวเปิดระบบหรือบริการกำลังทำงาน','ก.7'),
}
for p in list(d.paragraphs):
    for title,(fn,cap,note,label) in items.items():
        if p.text=='[รอภาพหน้าจอจริง: '+title+']':
            p.clear();p.alignment=WD_ALIGN_PARAGRAPH.CENTER;p.paragraph_format.first_line_indent=Cm(0);p.paragraph_format.keep_with_next=True
            p.add_run().add_picture(str(R/'screenshots/appendix_a'/fn),width=Cm(9))
            c=p.insert_paragraph_before('ภาพประกอบที่ ก-0 '+cap,style='Caption');p._p.addnext(c._p)
            c.alignment=WD_ALIGN_PARAGRAPH.CENTER;c.paragraph_format.first_line_indent=Cm(0);c.paragraph_format.keep_with_next=True
            n=c.insert_paragraph_before(note,style='Normal');c._p.addnext(n._p);n.paragraph_format.first_line_indent=Cm(1.27)
g=0
for p in d.paragraphs:
    if p.text.startswith('ภาพประกอบที่ ก-'):
        g+=1;txt=re.sub(r'ก-\d+',f'ก-{g}',p.text,count=1);p.clear();p.add_run(txt)
    if p.text.startswith('ใช้ภาพประกอบที่ ก-2 และ ข-1'):
        txt=p.text.replace('ก-2','ก-4');p.clear();p.add_run(txt)
d.save(R/'TourApp_Appendix_A_B.docx');shutil.copy2(R/'TourApp_Appendix_A_B.docx',cp/'03_safe_file_images.docx')
pending=(R/'appendix_pending.md').read_text(encoding='utf-8').replace('เหลือ 38 หัวข้อ','เหลือ 35 หัวข้อ')
pending='\n'.join(x for x in pending.splitlines() if not any(x.startswith('| '+v[3]+' ') for v in items.values()))+'\n';(R/'appendix_pending.md').write_text(pending,encoding='utf-8')
idx=(R/'appendix_image_index.md').read_text(encoding='utf-8').replace('ภาพประกอบที่ ก-2 การเตรียมและเปิดเว็บ Portal','ภาพประกอบที่ ก-4 การเตรียมและเปิดเว็บ Portal').replace('ก-2 และ ข-1','ก-4 และ ข-1')
idx+='\n## ชุดภาพรายชื่อไฟล์ที่เก็บใหม่แบบอ่านอย่างเดียว\n\n| คำบรรยาย | ไฟล์ | ที่มา | UIจริง/Fixture | ใส่ Word | ข้อจำกัด |\n|---|---|---|---|---|---|\n'
for n,(title,(fn,cap,note,label)) in zip([2,3,5],items.items()):
    idx+=f'| ภาพประกอบที่ ก-{n} {cap} | screenshots/appendix_a/{fn} | work/capture_originals_20261003/{fn.replace("_20261003","")} native Windows Explorer | UIจริงใหม่ | แล้ว | รายชื่อไฟล์เท่านั้น ปิดบัง breadcrumb/ส่วนส่วนตัวในสำเนา ไม่เปิดไฟล์ค่าลับ/ไม่ execute |\n'
idx+='\nยอดสุดท้ายชุดนี้: แทรก14ตำแหน่ง unique13 / เดิม10ตำแหน่ง / ใหม่4 / shared5หัวข้อ / Fixture0 / pending35 (B2,C28,D5) เลขภาพ ก-1–ก-5 และ ข-1–ข-9\n'
(R/'appendix_image_index.md').write_text(idx,encoding='utf-8')
with (R/'appendix_progress.md').open('a',encoding='utf-8') as f:f.write('\n## งานต่อเนื่อง '+stamp+' ชุด 3\n\nเติมภาพรายชื่อ SQL, config และ Launcher จาก Explorer ที่เก็บใหม่แบบอ่านอย่างเดียว รวมภาพใหม่4/ภาพเดิม10ตำแหน่ง/unique13 ใช้ภาพร่วม5หัวข้อ เหลือ35 marker (B2/C28/D5). ภาพไม่มีผล execute/import หรือบริการกำลังทำงาน และไม่ถือ cleaninstallerพร้อมใช้ รอ render/ตรวจรุ่นสุดท้าย\n')
print('SAVED',stamp,'PENDING',sum('[รอภาพหน้าจอจริง:' in p.text for p in d.paragraphs),'DRAWINGS',len(d.inline_shapes))
