# -*- coding: utf-8 -*-
"""Kiem chung cuoi: DOCX 26 buoc + CSDLA sau khi ap buoc 25/26."""
import os, re, shutil, sqlite3
from docx import Document

D = r'C:\Claw 2\Nghien cuu\Du-an-A-Quy-trinh-son'
P = os.path.join(D, 'Bo-buoc-chuan-26-buoc_05-10-2026.docx')
print('=== DOCX:', os.path.basename(P), os.path.getsize(P), 'byte ===')
doc = Document(P)
txt = '\n'.join(p.text for p in doc.paragraphs)
for t in doc.tables:
    for r in t.rows:
        for c in r.cells:
            txt += '\n' + c.text
print('   doan:', len(doc.paragraphs), '| bang:', len(doc.tables), '| anh:', len(doc.inline_shapes))
print('   dong bang dau:', [len(t.rows) for t in doc.tables][:3])
print('   co buoc 25/26:', 'Ngăn dầu' in txt, '/', 'Kiểm tra hoàn thiện & đóng gói' in txt)
print('   so muc 1..26 du:', all(('### %d. ' % i) in txt for i in range(1, 27)))
print('   ten rieng:', sum(txt.count(k) for k in ['Trendworld', 'Nguyễn Văn Tươi', 'Bình Thạnh chem']))
print('   tieng Viet co dau:', all(w in txt for w in ['Ngăn dầu', 'BỘ ĐỊNH NGHĨA', 'chống ố vàng']))

print()
print('=== CSDLA.db sau khi ap ===')
c = sqlite3.connect(os.path.join(D, 'CSDLA.db'))
for tb in ['buoc_chuan', 'quy_trinh', 'buoc_quy_trinh', 'danh_muc_mau']:
    print('   %-16s %d' % (tb, c.execute('select count(*) from %s' % tb).fetchone()[0]))
print('   buoc_chuan 24-26:')
for r in c.execute('select so, ten_vi, ten_en from buoc_chuan where so >= 23 order by so'):
    print('      ', r)
print('   dong buoc_quy_trinh = 25:', c.execute('select count(*) from buoc_quy_trinh where buoc_chuan_so=25').fetchone()[0])
print('   dong buoc_quy_trinh = 26 (chua co, dung):', c.execute('select count(*) from buoc_quy_trinh where buoc_chuan_so=26').fetchone()[0])
c.close()

out = os.path.join(os.environ['LOCALAPPDATA'], 'hermes', 'cache', 'gui_duan_a')
os.makedirs(out, exist_ok=True)
shutil.copy2(P, out)
print()
print('   ban gui:', os.path.join(out, os.path.basename(P)), os.path.getsize(os.path.join(out, os.path.basename(P))), 'byte')
print('   file 24 buoc cu con?', os.path.exists(os.path.join(D, '01-Bo-dinh-nghia-24-buoc.md')))
print('   file 26 buoc:', os.path.exists(os.path.join(D, '01-Bo-dinh-nghia-34-buoc.md')))
