# -*- coding: utf-8 -*-
r"""B75: soi loader — WEEK_RE, cột bảng file_loaded, file nào đã nạp cho 'mau' T40, và thử lại với đúng TÊN."""
import importlib.util, os, re, sqlite3

DB = r'C:\c\deepseek-harness\claw2-db\btchem.db'
LO = r'C:\c\deepseek-harness\claw2-db\nap_du_lieu.py'
print('  WEEK_RE trong loader:')
s = open(LO, encoding='utf-8', errors='replace').read()
for l in s.split('\n'):
    if 'WEEK_RE' in l and '=' in l:
        print('   ', l.strip()[:160])

spec = importlib.util.spec_from_file_location('napdu', LO)
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
print()
print('  week_from_name thử với các tên:')
for ten in ('Tuan_41_05.10-11.10_DA-LAM-SACH.xlsx', 'TUẦN 41 (05.10-11.10).xlsx', 'Tuan_40_28.09-04.10_DA-LAM-SACH.xlsx', 'TUẦN 40 (28.09-04.10).xlsx'):
    print('   %-42s -> %s' % (ten, m.week_from_name(ten)))

c = sqlite3.connect('file:%s?mode=ro' % DB.replace('\\', '/'), uri=True)
print()
print('  cột bảng file_loaded:', [r[1] for r in c.execute('pragma table_info(file_loaded)')])
print('  các bản ghi mới nhất:')
cols = [r[1] for r in c.execute('pragma table_info(file_loaded)')]
q = 'select * from file_loaded order by rowid desc limit 8'
for r in c.execute(q):
    d = dict(zip(cols, r))
    print('   %-6s | %-78s | %s' % (str(d.get('loai'))[:6], str(d.get('file') or d.get('ten_file') or d.get('duong_dan'))[:78], str(d.get('ngay') or d.get('thoi_gian'))[:16]))
print()
print('  mau_lam có tuan=T41 chưa:', c.execute("select count(*) from mau_lam where tuan='T41'").fetchone()[0])
c.close()
