#!/usr/bin/env python3
"""Chèn ảnh panel test vào báo cáo của Trí"""

from docx import Document
from docx.shared import Pt, Cm, Inches, RGBColor
from docx.enum.text import WD_ALIGN_PARAGRAPH
from docx.oxml.ns import qn
import os

FILE = r"C:\antigra-DS\CLAW_LAM_VIEC\QC\Bao cao\Bao_cao_dinh_foam_Thinh_Viet_TV246_TV299A_27-07-2026.docx"
IMG = r"C:\antigra-DS\CLAW_LAM_VIEC\QC\Bao cao\panel_test_dinh_foam_TV246_30-07-2026.jpg"

doc = Document(FILE)

# ─── Tìm section IV. HÌNH ẢNH KÈM THEO và thêm ảnh vào đó ───
# Cách đơn giản: append ảnh vào cuối tài liệu sau phần V
# Nhưng tốt hơn: thêm ảnh vào section IV ngay sau placeholder
# Cách dễ nhất: tìm paragraph có chứa "HÌNH ẢNH KÈM THEO" và thêm sau đó

# Tìm section heading và thêm ảnh
found_section = False
for i, p in enumerate(doc.paragraphs):
    if "HÌNH ẢNH KÈM THEO" in p.text and p.style.name.startswith('Heading'):
        # Insert image after this paragraph
        # Find the last paragraph that contains "Báo cáo TV 246 — Trang 2" or similar
        # Let's add the image at the end of section IV
        found_section = True

# Add image to a new paragraph at the end
p = doc.add_paragraph()
p.alignment = WD_ALIGN_PARAGRAPH.CENTER
run = p.add_run()
run.add_picture(IMG, width=Inches(5.5))

# Add caption
p2 = doc.add_paragraph()
p2.alignment = WD_ALIGN_PARAGRAPH.CENTER
run2 = p2.add_run("📸 Kết quả dán dính mặt — Màu TV.246 (30/07/2026): 0,1-0,5 kg/cm², 17H-24H → ❌ Không đạt (dính foam)")
run2.font.name = "Times New Roman"
run2.font.size = Pt(9)
run2.italic = True

doc.save(FILE)
print(f"✅ Đã chèn ảnh vào báo cáo: {FILE}")
