# -*- coding: utf-8 -*-
"""Chuẩn bị việc QC 07/10 (Trí): copy ảnh chủ nhân gửi, phóng 2x ảnh tài liệu, dựng thư mục agy + prompt đọc phiếu."""
import io, os, shutil
from PIL import Image

SRC = os.path.expandvars(r'%LOCALAPPDATA%\hermes\cache\images')
QC = r'C:\Claw 2\QC'
WS = os.path.join(QC, '_agy_tri_0710')
ANH = os.path.join(WS, 'anh')
os.makedirs(ANH, exist_ok=True)

# 3 ảnh TÀI LIỆU (agy đọc) + 4 ảnh HIỆN TRƯỜNG
TAILIEU = [
    ('img_68f010b63649.jpg', 'phieu-in_MK-12-17-H_MDF_0710.jpg'),
    ('img_11aea4af8704.jpg', 'phieu-viet-tay_NC_MDF-B1-B4_0710.jpg'),
    ('img_5423a54ec48f.jpg', 'phieu-RD_kiem-tra-san-pham-moi_0710.jpg'),
]
HIENTRUONG = [
    ('img_89d4e408f161.jpg', 'hientruong_ban-son-tam-trang-MDF_0710.jpg'),
    ('img_e6daefe0f96f.jpg', 'hientruong_tam-den-vat-tu-son_0710.jpg'),
    ('img_7a6c8aec820d.jpg', 'hientruong_11-tam-den-dung-tuong_0710.jpg'),
]
print('=== ảnh tài liệu → vừa copy vào QC\\images, vừa phóng 2x cho agy ===')
for f, ten in TAILIEU:
    p = os.path.join(SRC, f)
    shutil.copy2(p, os.path.join(QC, 'images', 'tri_0710_' + ten))
    im = Image.open(p).convert('RGB')
    w, h = im.size
    if max(w, h) < 2400:
        im2 = im.resize((w * 2, h * 2), Image.LANCZOS)
    else:
        im2 = im
    out = os.path.join(ANH, 'tl_' + ten)
    im2.save(out, quality=95)
    print('   %-42s %dx%d → %dx%d | %d byte' % (ten, w, h, im2.size[0], im2.size[1], os.path.getsize(out)))

print('=== ảnh hiện trường → QC\\images ===')
for f, ten in HIENTRUONG:
    p = os.path.join(SRC, f)
    shutil.copy2(p, os.path.join(QC, 'images', 'tri_0710_' + ten))
    im = Image.open(p)
    print('   %-42s %dx%d | %d byte' % (ten, im.size[0], im.size[1], os.path.getsize(p)))

# nguyên văn tin Zalo
tin = io.open(os.path.join(os.path.expandvars(r'%LOCALAPPDATA%\Temp\zalo_0710'), 'tin_zalo_tri_0710.txt'), encoding='utf-8').read()
io.open(os.path.join(WS, 'tin_07-10.txt'), 'w', encoding='utf-8').write(
    'NGUYÊN VĂN TIN NHẮN ZALO — HỘI THOẠI "Trí QC" — KHỐI NGÀY "Hôm nay" (07/10/2026)\n' + '=' * 70 + '\n' + tin)

# mẫu văn phong file QC đang dùng
src_md = os.path.join(QC, 'Tri qc.md')
tail = io.open(src_md, encoding='utf-8').read()[-9000:]
io.open(os.path.join(WS, 'mau_dinh_dang.md'), 'w', encoding='utf-8').write(tail)

print('\nthư mục agy:', WS)
for f in sorted(os.listdir(WS)):
    print('   ', f)
for f in sorted(os.listdir(ANH)):
    print('    anh/' + f)
