# -*- coding: utf-8 -*-
r"""B42: soi KỸ nơi lưu BÀI HỌC — bảng/cột chứa 'bai_hoc'/'lesson'/'kinh_nghiem'/'nha_may' + thư mục bài học."""
import os, sqlite3, glob

DS = [r'C:\c\deepseek-harness\claw2-db\btchem.db'] + sorted(p for p in glob.glob(r'C:\Claw 2\**\*.db', recursive=True)
                                                        if '_BAN_GIAO' not in p)

for p in DS:
    if not os.path.exists(p):
        print('  (không thấy) %s' % p); continue
    c = sqlite3.connect('file:%s?mode=ro' % p.replace('\\', '/'), uri=True)
    t = [r[0] for r in c.execute("select name from sqlite_master where type='table' order by name")]
    hit = []
    for ten in t:
        cols = [r[1] for r in c.execute('pragma table_info("%s")' % ten)]
        low = (ten + ' ' + ' '.join(cols)).lower()
        if any(k in low for k in ('bai_hoc', 'baihoc', 'lesson', 'kinh_nghiem', 'nha_may', 'khac_phuc', 'su_co', 'kph')):
            n = c.execute('select count(*) from "%s"' % ten).fetchone()[0]
            hit.append('   %-30s %6d dòng | cột: %s' % (ten, n, ', '.join(cols)))
    print('=== %s (%d bảng)' % (p, len(t)))
    print('   tất cả bảng:', ', '.join(t))
    if hit:
        print('   >>> BẢNG LIÊN QUAN BÀI HỌC / NHÀ MÁY / SỰ CỐ:')
        print('\n'.join(hit))
    c.close()
    print()

for d in (r'C:\Claw 2\QC\Bai-hoc-ky-thuat', r'C:\Claw 2\Bai-hoc-ky-thuat', r'C:\Claw 2\Nghien cuu\Bai-hoc'):
    if os.path.isdir(d):
        fs = sorted(os.listdir(d))
        print('=== THƯ MỤC %s (%d mục)' % (d, len(fs)))
        for f in fs[:20]:
            fp = os.path.join(d, f)
            print('   %-60s %s' % (f[:60], ('%d B' % os.path.getsize(fp)) if os.path.isfile(fp) else '[thư mục]'))
        if len(fs) > 20:
            print('   ... và %d mục nữa' % (len(fs) - 20))
