# -*- coding: utf-8 -*-
import os, json, openpyxl, docx

print('=== KIEM TRA TOAN DIEN CAC FILE OUTPUT ===')

# 1. File lam sach
f1 = os.path.join('File-lam-sach', 'MUA_HANG_03T10-26_DA-LAM-SACH.xlsx')
assert os.path.exists(f1), f'{f1} missing!'
wb1 = openpyxl.load_workbook(f1, data_only=True)
assert wb1.sheetnames == ['FILE GỐC'], f'Sheets: {wb1.sheetnames}'
ws1 = wb1['FILE GỐC']
assert ws1.cell(4, 13).value == 'Từ 1->3 Tháng 10 2026', f'Col 13 header: {ws1.cell(4, 13).value}'
assert ws1.cell(4, 12).value == 'Tháng 09 2026 (Khối lượng Kg)', f'Col 12 header: {ws1.cell(4, 12).value}'
assert ws1.cell(195, 13).value == 116584, f'Row 195 Col 13: {ws1.cell(195, 13).value}'
print(f'1. File lam sach: OK ({os.path.getsize(f1)} bytes, max_row={ws1.max_row})')

# 2. File DOCX
f2 = os.path.join('Bao_cao', 'BAO_CAO_SO_SANH_MUA_HANG_T10_ky03.docx')
assert os.path.exists(f2), f'{f2} missing!'
doc = docx.Document(f2)
assert len(doc.tables) >= 2, f'Tables count: {len(doc.tables)}'
t0, t1 = doc.tables[0], doc.tables[1]
print(f'2. File DOCX: OK ({os.path.getsize(f2)} bytes, Table 0 rows={len(t0.rows)}, Table 1 rows={len(t1.rows)})')

# 3. File HTML
f3 = os.path.join('Bao_cao', 'Baocaomuahang_thang10_ky01-03-10-2026.html')
assert os.path.exists(f3), f'{f3} missing!'
with open(f3, 'r', encoding='utf-8') as f:
    html_content = f.read()
assert 'Tháng 10/2026' in html_content
assert '116,584' in html_content
assert '38,861' in html_content
print(f'3. File HTML Portal: OK ({len(html_content)} chars, {os.path.getsize(f3)} bytes)')

# 4. File JSON
f4 = os.path.join('Bao_cao', 'capnhat_csdl_T10_ky03.json')
assert os.path.exists(f4), f'{f4} missing!'
with open(f4, 'r', encoding='utf-8') as f:
    j4 = json.load(f)
assert j4['ky'] == 'T10'
assert j4['so_ngay'] == 3
assert j4['tong_kg'] == 116584
assert j4['tb_ngay'] == 38861
assert j4['so_sanh'] == '+6,935 (+21.7%)'
assert len(j4['khach_hang']) == 190
print(f'4. File JSON CSDL: OK ({len(j4["khach_hang"])} KH, {os.path.getsize(f4)} bytes)')

# 5. File Nhat ky
f5 = 'NHAT_KY_agy_T10_ky03.md'
assert os.path.exists(f5), f'{f5} missing!'
with open(f5, 'r', encoding='utf-8') as f:
    lines = [l.strip() for l in f if l.strip()]
assert lines[-1] == 'HOAN TAT', f'Last line: {lines[-1]}'
print(f'5. File Nhat ky: OK ({len(lines)} lines, last line: {lines[-1]})')

print('=== TAT CA 5 BUOC DA HOAN TAT CHINH XAC 100% ===')
