# -*- coding: utf-8 -*-
"""Tao BAN SAO CSDLA de gui chu duan + kiem chung (integrity, so dong, md5 giong nhau)."""
import hashlib, os, shutil, sqlite3, time

D = r'C:\Claw 2\Nghien cuu\Du-an-A-Quy-trinh-son'
SRC = os.path.join(D, 'CSDLA.db')
OUT = os.path.join(os.environ['LOCALAPPDATA'], 'hermes', 'cache', 'gui_duan_a')
DST = os.path.join(OUT, 'CSDLA_05-10-2026.db')
os.makedirs(OUT, exist_ok=True)

c = sqlite3.connect(SRC)
print('integrity_check:', c.execute('pragma integrity_check').fetchone()[0])
print('--- so dong tung bang ---')
tong = 0
for tb in ['buoc_chuan', 'quy_trinh', 'buoc_quy_trinh', 'phong_cach', 'cong_thuc', 'danh_muc_mau', 'nguon_quet']:
    n = c.execute('select count(*) from %s' % tb).fetchone()[0]
    tong += n
    print('   %-16s %d' % (tb, n))
print('   TONG            %d dong' % tong)
print('--- 6 buoc cuoi (25-34) ---')
for r in c.execute('select so, ten_vi from buoc_chuan where so >= 25 order by so'):
    print('   ', r)
c.close()

shutil.copy2(SRC, DST)
h1 = hashlib.md5(open(SRC, 'rb').read()).hexdigest()
h2 = hashlib.md5(open(DST, 'rb').read()).hexdigest()
print()
print('ban goc :', os.path.basename(SRC), os.path.getsize(SRC), 'byte | md5', h1)
print('ban sao :', os.path.basename(DST), os.path.getsize(DST), 'byte | md5', h2)
print('md5 giong nhau:', h1 == h2, '| sua luc:', time.strftime('%d/%m/%Y %H:%M:%S', time.localtime(os.path.getmtime(SRC))))
os.makedirs(os.path.join(OUT, '..', 'gui_duan_a'), exist_ok=True)
