# -*- coding: utf-8 -*-
r"""B119: kiểm kê 4 CSDL để thiết kế dashboard: bảng, số dòng, các cột ngày/tháng/tuần/khách hàng/số liệu."""
import os, sqlite3

DB = {
    'btchem.db': r'C:\c\deepseek-harness\claw2-db\btchem.db',
    'CSDLC.db': r'C:\Claw-Zalo\CSDLC\CSDLC.db',
    'CSDLA.db': r'C:\Claw 2\Nghien cuu\Du-an-A-Quy-trinh-son\CSDLA.db',
    'CSDLB.db': r'C:\Claw 2\Nghien cuu\Du-an-B-He-mau-Stain-Glaze\CSDLB.db',
}
for ten, p in DB.items():
    print('=' * 100)
    print('%s  (%s · %d B)' % (ten, p, os.path.getsize(p)))
    c = sqlite3.connect('file:%s?mode=ro' % p.replace('\\', '/'), uri=True)
    for (t,) in c.execute("select name from sqlite_master where type='table' and name not like 'sqlite_%' order by name"):
        n = c.execute('select count(*) from "%s"' % t).fetchone()[0]
        cols = [r[1] for r in c.execute('pragma table_info("%s")' % t)]
        print('  %-22s %6d dòng | %s' % (t, n, ', '.join(cols)))
        # thử lấy khoảng thời gian + vài giá trị phân loại
        for cand in ('tuan', 'thang', 'ngay', 'ngay_bc', 'ngay_nhan', 'date', 'thoi_gian'):
            if cand in cols:
                try:
                    r = c.execute('select min("%s"), max("%s"), count(distinct "%s") from "%s"' % (cand, cand, cand, t)).fetchone()
                    print('        %-10s min=%s max=%s | %d giá trị khác nhau' % (cand, r[0], r[1], r[2]))
                except Exception as e:
                    print('        %s lỗi %s' % (cand, e))
                break
    c.close()
