# -*- coding: utf-8 -*-
r"""B82: (1) mẫu CHƯA dứt điểm T38-T41; (2) mã xuất hiện nhiều tuần mà chưa ký/hoàn thành; (3) mẫu THIẾU KTV (không có báo cáo) theo tuần + danh sách."""
import collections, sqlite3

DB = r'C:\c\deepseek-harness\claw2-db\btchem.db'
c = sqlite3.connect('file:%s?mode=ro' % DB.replace('\\', '/'), uri=True)
c.row_factory = sqlite3.Row

print('=== 1. MẪU CHƯA DỨT ĐIỂM (trạng thái Đang làm / ĐL) trong T38 → T41 ===')
rows = c.execute("""select m.tuan, ifnull(n.ten,'(?)') nm, m.ma_mau, ifnull(m.khach_hang,''), m.ngay_nhan, ifnull(m.ngay_ht,''),
                           ifnull(k.ten,'(thiếu KTV)') ktv, ifnull(m.ghi_chu,'')
                    from mau_lam m left join nha_may n on n.id=m.nha_may_id left join phai_vien_kt k on k.id=m.ky_thuat_id
                    where m.tuan in ('T38','T39','T40','T41') and ifnull(m.trang_thai,'') in ('Đang làm','ĐL')
                    order by m.tuan, nm""").fetchall()
print('   tổng: %d dòng' % len(rows))
theo_tuan = collections.Counter(r['tuan'] for r in rows)
print('   theo tuần:', dict(theo_tuan))
for r in rows:
    print('   %-4s %-18s %-16s %-14s nhận %-11s HT %-11s | %-18s | %s' % (
        r['tuan'], str(r['nm'])[:18], str(r['ma_mau'])[:16], str(r['khach_hang'])[:14], str(r['ngay_nhan'])[:11],
        str(r['ngay_ht'])[:11], str(r['ktv'])[:18], str(r['ghi_chu'])[:44]))

print()
print('=== 2. MÃ XUẤT HIỆN Ở NHIỀU TUẦN TRONG T38–T41 MÀ CHƯA KÝ/HOÀN THÀNH ===')
d = collections.defaultdict(list)
for r in c.execute("""select m.tuan, ifnull(n.ten,'(?)') nm, m.ma_mau, ifnull(m.trang_thai,''), ifnull(m.khach_hang,''), m.ngay_nhan
                      from mau_lam m left join nha_may n on n.id=m.nha_may_id
                      where m.tuan in ('T38','T39','T40','T41') and ifnull(m.ma_mau,'')<>'' """):
    d[(r['nm'], r['ma_mau'])].append((r['tuan'], r['trang_thai'], r['khach_hang'], r['ngay_nhan']))
nhieu = {k: v for k, v in d.items() if len({x[0] for x in v}) >= 2}
print('   số mã xuất hiện ≥2 tuần: %d' % len(nhieu))
chua = {k: v for k, v in nhieu.items() if not any(x[1] in ('Đã ký', 'Đã hoàn thành', 'HT', 'DK') for x in v)}
print('   trong đó CHƯA từng ký/hoàn thành: %d' % len(chua))
for (nm, ma), v in list(chua.items())[:15]:
    print('   %-20s %-16s → %s' % (str(nm)[:20], str(ma)[:16], ['%s:%s' % (x[0], x[1]) for x in v]))

print()
print('=== 3. MẪU THIẾU KTV (không gán người → không có báo cáo cá nhân) theo tuần ===')
for r in c.execute("""select tuan, count(*) tong, sum(case when ifnull(ky_thuat_id,'')='' then 1 else 0 end) thieu
                      from mau_lam where tuan in ('T38','T39','T40','T41') group by tuan order by tuan"""):
    print('   %-4s tổng %3d | thiếu KTV %3d' % (r['tuan'], r['tong'], r['thieu']))

print()
print('=== 4. NHÀ MÁY CÓ MẪU MÀ KHÔNG XUẤT HIỆN TRONG BÁO CÁO KT TUẦN TƯƠNG ỨNG ===')
# lấy các cụm nhà máy nêu trong báo cáo KT tuần, so với nhà máy có mẫu
for t in ('T39', 'T40', 'T41'):
    bc = ' '.join(r[0] or '' for r in c.execute('select noi_dung from bao_cao_kt_tuan where tuan=?', (t,)))
    nm_co_mau = [r[0] for r in c.execute("""select distinct n.ten from mau_lam m join nha_may n on n.id=m.nha_may_id
                                            where m.tuan=? and n.ten is not null""", (t,))]
    thieu = [nm for nm in nm_co_mau if nm and nm.upper() not in bc.upper()]
    print('   %s: %d nhà máy có mẫu | không thấy tên trong báo cáo KT tuần: %d → %s' % (t, len(nm_co_mau), len(thieu), thieu[:12]))
c.close()
