# -*- coding: utf-8 -*-
r"""B146: xem nội dung cột canh_tranh trong bao_cao_sx để thiết kế lại biểu đồ/bảng cạnh tranh."""
import sqlite3

b = sqlite3.connect('file:C:/c/deepseek-harness/claw2-db/btchem.db?mode=ro', uri=True)
print('  --- Độ dài / số dòng có nội dung cạnh tranh:')
for r in b.execute("""select tuan, count(*), sum(case when coalesce(trim(canh_tranh),'')<>'' then 1 else 0 end)
                      from bao_cao_sx group by tuan order by cast(substr(tuan,2) as int)"""):
    print('    %-5s tổng %3d dòng | có nội dung cạnh tranh: %d' % r)
print('\n  --- Toàn bộ nội dung cạnh tranh tuần 41 (nguyên văn):')
for r in b.execute("""select coalesce(n.ten,''), trim(bc.canh_tranh) from bao_cao_sx bc
                      left join nha_may n on n.id=bc.nha_may_id
                      where bc.tuan='T41' and coalesce(trim(bc.canh_tranh),'')<>''
                      order by n.ten"""):
    print('    %-26s %s' % (r[0][:26], r[1][:150]))
print('\n  --- 12 nội dung cạnh tranh các tuần khác (mẫu):')
for r in b.execute("""select tuan, coalesce(n.ten,''), trim(bc.canh_tranh) from bao_cao_sx bc
                      left join nha_may n on n.id=bc.nha_may_id
                      where tuan<>'T41' and coalesce(trim(bc.canh_tranh),'')<>''
                      order by bc.id desc limit 12"""):
    print('    %-5s %-24s %s' % (r[0], r[1][:24], r[2][:120]))
