#!/usr/bin/env python3
"""
CORRECTIE: v5.0 → v6.0 met BEHOUD van alle formatting.
Neem v5.0 EXACT over, pas ALLEEN de noodzakelijke tekstwijzigingen toe.
"""
from docx import Document
from docx.shared import Pt, Emu
import shutil, os

SRC = '/root/.openclaw/media/inbound/PGS15_Compliance_Toetsing_Phoenix_Metals_v5.0---661ec953-b313-414e-96ed-87265b9f5471.docx'
DST = '/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Compliance_Toetsing_Phoenix_Metals_v6.0.docx'

# Start fresh from v5.0
doc = Document(SRC)
changes = []

# ============================================================
# TARGETED REPLACEMENTS — only change what must change
# ============================================================

for i, p in enumerate(doc.paragraphs):
    text = p.text
    
    # Only target paragraphs that actually need changing
    
    # Aggregatie klasse 8 — geschatte hoeveelheid
    if 'Totale geschatte hoeveelheid: >250 kg' in text:
        for run in p.runs:
            if 'Totale geschatte hoeveelheid: >250 kg' in run.text:
                old = run.text
                run.text = run.text.replace(
                    'Totale geschatte hoeveelheid: >250 kg (ondergrens klasse 8 PG II). Aggregatie leidt tot Niveau C.',
                    'Totale hoeveelheid: 4 x 150 kg = 600 kg (boven ondergrens 250 kg klasse 8 PG II). Aggregatie leidt tot Niveau C.'
                )
                changes.append(f"[{i}] Aggregatie: geschat → 600 kg exact")
    
    # Seveso
    if 'totale opslag < 5.000 kg' in text and 'Seveso' in text:
        for run in p.runs:
            if 'totale opslag < 5.000 kg' in run.text:
                run.text = run.text.replace('totale opslag < 5.000 kg', 'totale opslag 2.700 kg (18 x 150 kg) < 5.000 kg')
                changes.append(f"[{i}] Seveso: exact 2.700 kg")
    
    # Catch-all
    if 'geschat < 2.000 kg' in text:
        for run in p.runs:
            if 'geschat < 2.000 kg' in run.text:
                run.text = run.text.replace(
                    'geschat < 2.000 kg. Catch-all drempel: 1.000 kg. Indien > 1.000 kg: catch-all van toepassing.',
                    '2.700 kg (18 x 150 kg). Catch-all drempel: 1.000 kg. Totaal > 1.000 kg: catch-all van toepassing.'
                )
                changes.append(f"[{i}] Catch-all: exact 2.700 kg")
    
    # Bvi
    if 'Bvi: Geen vergunningplicht (< 1.000 kg individueel per stof)' in text:
        for run in p.runs:
            if '< 1.000 kg individueel per stof' in run.text:
                run.text = run.text.replace(
                    'Bvi: Geen vergunningplicht (< 1.000 kg individueel per stof).',
                    'Bvi: Geen vergunningplicht (150 kg per stof < 1.000 kg drempel).'
                )
                changes.append(f"[{i}] Bvi: 150 kg per stof")

# ============================================================
# DOCUMENT HEADER TABLE — update version/datum/status ONLY
# ============================================================

header_table = doc.tables[0]
for row in header_table.rows:
    cells = row.cells
    label = cells[0].text.strip()
    if label == 'Versie' and len(cells) >= 2:
        # Keep formatting, only change text
        for run in cells[1].paragraphs[0].runs:
            if '5.0' in run.text:
                run.text = run.text.replace('5.0', '6.0')
                changes.append("Header: Versie 5.0 → 6.0")
    elif label == 'Datum' and len(cells) >= 2:
        for run in cells[1].paragraphs[0].runs:
            if '6 mei' in run.text:
                run.text = run.text.replace('6 mei 2026', '7 mei 2026')
                changes.append("Header: Datum → 7 mei 2026")
    elif label == 'Status' and len(cells) >= 2:
        for run in cells[1].paragraphs[0].runs:
            if run.text.strip() == 'Definitief':
                run.text = 'Definitief — 150 kg/ltr per product'
                changes.append("Header: Status → Definitief — 150 kg/ltr per product")

# ============================================================
# VERSION HISTORY TABLE — add v6.0 row WITH MATCHING FORMAT
# ============================================================

ver_table = doc.tables[1]
# Get formatting from last existing row (row 6 = v5.0)
ref_row = ver_table.rows[-1]
ref_cells = ref_row.cells

# Add new row
new_row = ver_table.add_row()
new_cells = new_row.cells
new_cells[0].text = '6.0'
new_cells[1].text = '7 mei 2026'
new_cells[2].text = 'Heranalyse met exact 150 kg/ltr per product (voorheen geschatte hoeveelheden). Alle niveaus herbevestigd: Niveau C blijft vereist (CMR-route). Aggregatie, Seveso en catch-all bijgewerkt naar exacte totalen.'

# Copy formatting from reference row
for ci in range(3):
    ref_para = ref_cells[ci].paragraphs[0]
    new_para = new_cells[ci].paragraphs[0]
    if ref_para.runs and new_para.runs:
        ref_run = ref_para.runs[0]
        new_run = new_para.runs[0]
        # Copy font properties
        if ref_run.font.name:
            new_run.font.name = ref_run.font.name
        if ref_run.font.size:
            new_run.font.size = ref_run.font.size
        new_run.font.bold = ref_run.font.bold
        if ref_run.font.color and ref_run.font.color.rgb:
            new_run.font.color.rgb = ref_run.font.color.rgb
    # Copy paragraph format
    if ref_para.paragraph_format.space_after:
        new_para.paragraph_format.space_after = ref_para.paragraph_format.space_after
    if ref_para.paragraph_format.space_before:
        new_para.paragraph_format.space_before = ref_para.paragraph_format.space_before

changes.append("Versiehistorie: v6.0 entry toegevoegd")

# ============================================================
# SAVE
# ============================================================

doc.save(DST)
print(f"✓ Opgeslagen: {DST}")
print(f"✓ Wijzigingen: {len(changes)}")
for c in changes:
    print(f"  {c}")
