# -*- coding: utf-8 -*-
r"""B182: chốt bản đồ 10 vụ tuần 3 → id su_co_kph / ho_so_kph / khối portal (dry-run, KHÔNG ghi)."""
import io, re, sqlite3

PORTAL = r'C:\Claw 2\Bao cao - Hanh chinh\BAOCAOTONGHOP\HTML\KPH_TongHop_0508\index.html'
BT = r'C:\c\deepseek-harness\claw2-db\btchem.db'
KPH = r'C:\Claw\Khieu Nai KHach Hang\btchem_kph.db'
h = io.open(PORTAL, encoding='utf-8').read()
c = sqlite3.connect('file:%s?mode=ro' % BT.replace('\\', '/'), uri=True)
k = sqlite3.connect('file:%s?mode=ro' % KPH.replace('\\', '/'), uri=True)

print('  --- 1. MHT 02 (dinh vet giay to ong) trong su_co_kph:')
for tu in ('tổ ong', 'MHT 02', 'MHT02', 'Minh Hưng Tiến'):
    r = c.execute("""select id, substr(coalesce(ten_vu_viec,''),1,100), coalesce(trang_thai,'')
                     from su_co_kph where ten_vu_viec like ? or san_pham_mau like ? or mo_ta_su_co like ?""",
                  ('%' + tu + '%',) * 3).fetchall()
    print('     %-18s %d dong %s' % (tu, len(r), '; '.join('#%d %s (%s)' % x for x in r[:3])))

print('\n  --- 2. Hanh Phuc HP 269 trong ho_so_kph (moi truong):')
for tu in ('Hạnh Phúc', 'HP 269', 'HP269'):
    r = k.execute("""select id, coalesce(ma_hs,''), coalesce(ngay_nhan,''), substr(coalesce(san_pham_raw,''),1,60)
                     from ho_so_kph where san_pham_raw like ? or hien_tuong_raw like ? or mo_ta like ?
                     or cap_nhat like ? or ghi_chu like ? or nguon like ?""", ('%' + tu + '%',) * 6).fetchall()
    print('     %-18s %d ho so %s' % (tu, len(r), '; '.join('#%d %s %s %s' % x for x in r[:3])))

print('\n  --- 3. Portal: khoi Hanh Phuc / Hanh My / Tan Viet Tien / MK 12-17 / MHT / TVIET:')
for tu in ('Hạnh Phúc', 'Hành Mỹ', 'Tân Việt Tiến', 'MK 12', 'MHT 02', 'TVIET 246', 'TV 246'):
    i = h.lower().find(tu.lower())
    print('     %-16s %s' % (tu, ('co trong portal (vi tri %d)' % i) if i > 0 else 'KHONG co trong portal'))

print('\n  --- 4. Trang thai hien tai cac dong su_co_kph lien quan:')
for i in (22, 41, 53, 57, 59, 60, 62, 63, 70, 74, 80):
    r = c.execute('select id, substr(coalesce(ten_vu_viec,""),1,72), coalesce(trang_thai,""), length(coalesce(ket_qua,"")), length(coalesce(mo_ta_su_co,"")) from su_co_kph where id=?', (i,)).fetchone()
    if r:
        print('     #%-3d %-72s | %-24s | ket_qua %4dc | mo_ta %4dc' % r)

print('\n  --- 5. ho_so_kph lien quan (cap_nhat dai bao nhieu):')
for i in (7, 15, 796, 808, 812, 824, 826, 827):
    r = k.execute('select id, coalesce(ma_hs,""), coalesce(ngay_nhan,""), substr(coalesce(san_pham_raw,""),1,58), length(coalesce(cap_nhat,"")) from ho_so_kph where id=?', (i,)).fetchone()
    if r:
        print('     #%-4d %-12s %-11s %-58s | cap_nhat %3dc' % r)
