#!/usr/bin/env python3
"""
Correctie v6.0 naar PGS 15:2025 norm.
ALLEEN tekstuele correcties — layout onaangeroerd.

Correcties conform PGS 15:2025 (Publicatie 565, januari 2025):
- ADR 5.1 PG II/III: <2.500 kg → Niveau C (Wbdbo 60 min), niet B
- ADR 8 PG II onbrandbaar: <250 kg individueel → basisniveau (voetnoot c)
- ADR 6.1 PG III: <10.000 kg → Niveau C (via ADR-route, niet CMR-route)
- CMR-route alleen leidend voor stoffen ZONDER ADR-classificatie
- Aggregatie ADR 8: 600 kg > 250 kg → wél PGS 15 toepasselijk
"""
from docx import Document
import shutil, os

SRC = '/root/.openclaw/media/inbound/PGS15_Compliance_Toetsing_Phoenix_Metals_v6.0---ccdf2399-85f8-4986-aea5-063eb59275ed.docx'
DST = '/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Compliance_Toetsing_Phoenix_Metals_v6.0.docx'

# Archive current v6.0
archive_dir = '/root/projects/jg/2026-pgs15-phoenix-metals/archive/'
os.makedirs(archive_dir, exist_ok=True)
if os.path.exists(DST):
    shutil.copy2(DST, os.path.join(archive_dir, 'PGS15_Compliance_Toetsing_Phoenix_Metals_v6.0_pre_norm.docx'))

doc = Document(SRC)
changes = []

for i, p in enumerate(doc.paragraphs):
    text = p.text

    # ============================================================
    # H₂SO₄ 25% (IC-02) — [35] Niveau, [36] Rationale
    # Was: Niveau B. Moet: Basisniveau (individueel <250 kg, onbrandbaar, voetnoot c)
    # Via aggregatie: Niveau C
    # ============================================================
    
    if text == 'Niveau: B' and i == 35:
        for run in p.runs:
            if 'B' in run.text:
                run.text = run.text.replace('Niveau: B', 'Niveau: Basis (individueel), C (geaggregeerd)')
                changes.append(f"[{i}] H₂SO₄ niveau: B → Basis/C (aggregatie)")

    if 'Corrosieve vloeistof, ADR klasse 8 PG II. Afhankelijk van' in text:
        for run in p.runs:
            if 'Af' in run.text:
                run.text = ("Corrosieve vloeistof, ADR klasse 8 PG II, onbrandbaar. "
                           "Individueel: 150 kg < 250 kg ondergrens (Tabel 2) → basisniveau (voetnoot c: onbrandbaar in dedicated opslag). "
                           "Via aggregatie met andere klasse 8 PG II stoffen (totaal 600 kg > 250 kg): Niveau C (Wbdbo 60 min).")
                changes.append(f"[{i}] H₂SO₄ rationale: correctie naar PGS 15:2025 Tabel 2+6")

    # ============================================================
    # H₂O₂ 30% (IC-03) — [40] Niveau, [41] Rationale
    # Was: Niveau B. Moet: Niveau C (ADR 5.1 <2.500 kg → C, Wbdbo 60 min)
    # ============================================================

    if text == 'Niveau: B' and i == 40:
        for run in p.runs:
            if 'B' in run.text:
                run.text = run.text.replace('Niveau: B', 'Niveau: C')
                changes.append(f"[{i}] H₂O₂ niveau: B → C (ADR 5.1 <2.500 kg)")

    if 'Oxiderende vloeistof met bijkomstig corrosief gevaar. ADR klasse 5.1 PG II. Individueel Niveau B.' in text:
        for run in p.runs:
            if 'Oxiderende' in run.text:
                run.text = ("Oxiderende vloeistof met bijkomstig corrosief gevaar. ADR klasse 5.1 PG II. "
                           "PGS 15:2025 Tabel 6: ADR 5.1 <2.500 kg → Niveau C (Wbdbo 60 min). "
                           "Let op: scheiding vereist van brandbare stoffen en reducerende middelen.")
                changes.append(f"[{i}] H₂O₂ rationale: B → C conform Tabel 6")

    # ============================================================
    # Na-persulfate (IC-04) — [45] Niveau, [46] Rationale
    # Was: Niveau B. Moet: Niveau C (ADR 5.1 <2.500 kg → C)
    # ============================================================

    if text == 'Niveau: B' and i == 45:
        for run in p.runs:
            if 'B' in run.text:
                run.text = run.text.replace('Niveau: B', 'Niveau: C')
                changes.append(f"[{i}] Na-persulfate niveau: B → C (ADR 5.1 <2.500 kg)")

    if 'Oxiderende vaste stof, PG III. H334 (sensibiliserend)' in text:
        for run in p.runs:
            if 'Oxiderende' in run.text:
                run.text = ("Oxiderende vaste stof, ADR klasse 5.1 PG III. "
                           "PGS 15:2025 Tabel 6: ADR 5.1 <2.500 kg → Niveau C (Wbdbo 60 min). "
                           "H334 (sensibiliserend) geeft extra aandachtspunt voor luchtkwaliteit.")
                changes.append(f"[{i}] Na-persulfate rationale: B → C conform Tabel 6")

    # ============================================================
    # HCl 20% (IC-05) — [50] Niveau, [51] Rationale
    # Was: Niveau B. Moet: Basisniveau (individueel <250 kg), C (geaggregeerd)
    # ============================================================

    if text == 'Niveau: B' and i == 50:
        for run in p.runs:
            if 'B' in run.text:
                run.text = run.text.replace('Niveau: B', 'Niveau: Basis (individueel), C (geaggregeerd)')
                changes.append(f"[{i}] HCl niveau: B → Basis/C (aggregatie)")

    if 'Corrosieve vloeistof, ADR klasse 8 PG II. H335 (irriterend) vereist ventilatie. Individueel Niveau B.' in text:
        for run in p.runs:
            if 'Corrosieve' in run.text:
                run.text = ("Corrosieve vloeistof, ADR klasse 8 PG II, onbrandbaar. H335 (irriterend) vereist ventilatie. "
                           "Individueel: 150 kg < 250 kg ondergrens (Tabel 2) → basisniveau (voetnoot c). "
                           "Via aggregatie klasse 8 PG II (totaal 600 kg > 250 kg): Niveau C.")
                changes.append(f"[{i}] HCl rationale: correctie naar PGS 15:2025 Tabel 2+6")

    # ============================================================
    # Na-metavanadate (IM-04) — [55] Niveau, [56] Rationale
    # Was: Niveau C (CMR). Moet: Niveau C (ADR 6.1 PG III <10.000 kg)
    # Niveau blijft C, maar route verandert van CMR naar ADR
    # ============================================================

    if 'Niveau: C (CMR)' in text and i == 55:
        for run in p.runs:
            if 'C (CMR)' in run.text:
                run.text = run.text.replace('Niveau: C (CMR)', 'Niveau: C')
                changes.append(f"[{i}] Na-metavanadate niveau: C (CMR) → C (ADR 6.1)")

    if 'De CMR-route leidt tot Niveau C (ondergrens 25 kg voor CMR-stoffen in poedervorm)' in text:
        for run in p.runs:
            if 'CMR-route' in run.text:
                run.text = ("Giftige vaste stof, ADR klasse 6.1 PG III. CMR-classificatie: Muta. 2 (H341) + Repr. 2 (H361fd). "
                           "PGS 15:2025 Tabel 6: ADR 6.1 PG III <10.000 kg → Niveau C (Wbdbo 60 min). "
                           "ADR-route is leidend bij ADR-geclassificeerde stoffen. CMR-etiketten vereisen aanvullende arbo-maatregelen. "
                           "H411: milieugevaarlijk — milieucompliance verplicht.")
                changes.append(f"[{i}] Na-metavanadate rationale: CMR-route → ADR 6.1 route")

    # ============================================================
    # Na-aluminate (EP-06) — [60] Niveau, [61] Rationale
    # Was: Niveau B. Moet: Basisniveau (150 kg < 250 kg, onbrandbaar, voetnoot c)
    # ============================================================

    if text == 'Niveau: B' and i == 60:
        for run in p.runs:
            if 'B' in run.text:
                run.text = run.text.replace('Niveau: B', 'Niveau: Basis (individueel <250 kg)')
                changes.append(f"[{i}] Na-aluminate niveau: B → Basis (<250 kg ondergrens)")

    if 'Corrosieve vloeistof, ADR klasse 8 PG II (SDS: PG II, niet PG III). pH >13. Individueel Niveau B.' in text:
        for run in p.runs:
            if 'Corrosieve' in run.text:
                run.text = ("Corrosieve vloeistof, ADR klasse 8 PG II (SDS: PG II, niet PG III). pH >13, onbrandbaar. "
                           "Individueel: 150 kg < 250 kg ondergrens (Tabel 2) → basisniveau (voetnoot c: onbrandbaar in dedicated opslag). "
                           "Let op: bij aggregatie met andere klasse 8 PG II stoffen kan Niveau C van toepassing worden.")
                changes.append(f"[{i}] Na-aluminate rationale: B → Basis (<250 kg, voetnoot c)")

    # ============================================================
    # V₂O₅ (EP-08) — [65] Niveau, [66] Rationale
    # Was: Niveau C (CMR). Moet: Niveau C (ADR 6.1 PG III <10.000 kg)
    # Niveau blijft C, maar route verandert
    # ============================================================

    if 'Niveau: C (CMR)' in text and i == 65:
        for run in p.runs:
            if 'C (CMR)' in run.text:
                run.text = run.text.replace('Niveau: C (CMR)', 'Niveau: C')
                changes.append(f"[{i}] V₂O₅ niveau: C (CMR) → C (ADR 6.1)")

    if 'Carc. 1B is de zwaarste CMR-categorie → Niveau C (strengste route)' in text:
        for run in p.runs:
            if 'strengste route' in run.text:
                run.text = ("Giftige vaste stof, ADR klasse 6.1 PG III. CMR-classificatie: Carc. 1B (H350) + Muta. 2 (H341) + Repr. 2 (H361fd) + Lact. (H362). "
                           "PGS 15:2025 Tabel 6: ADR 6.1 PG III <10.000 kg → Niveau C (Wbdbo 60 min). "
                           "ADR-route is leidend. Carc. 1B (H350) vereist aanvullende arbo-maatregelen conform Arbobesluit art. 4.1c. "
                           "H411: milieugevaarlijk. H330 (dodelijk bij inademing): extra urgency voor ademhalingsbescherming.")
                changes.append(f"[{i}] V₂O₅ rationale: CMR-route → ADR 6.1 route + arbo-maatregelen")

    # ============================================================
    # V-electrolyte (EP-09) — [70] Niveau, [71] Rationale
    # Was: Niveau C (CMR). Moet: Basis (individueel <250 kg), C (geaggregeerd + arbo CMR)
    # ============================================================

    if 'Niveau: C (CMR)' in text and i == 70:
        for run in p.runs:
            if 'C (CMR)' in run.text:
                run.text = run.text.replace('Niveau: C (CMR)', 'Niveau: Basis (individueel), C (geaggregeerd + arbo)')
                changes.append(f"[{i}] V-electrolyte niveau: C (CMR) → Basis/C (aggregatie)")

    if 'De combinatie klasse 8 PG II + CMR Repr. 2 leidt tot Niveau C.' in text:
        for run in p.runs:
            if 'klasse 8 PG II + CMR' in run.text:
                run.text = ("Corrosieve vloeistof, ADR klasse 8 PG II, onbrandbaar. CMR: Repr. 2 (H361). "
                           "H370 (STOT SE 1) + H411 (milieugevaarlijk). "
                           "Individueel: 150 kg < 250 kg ondergrens (Tabel 2) → basisniveau (voetnoot c). "
                           "Via aggregatie klasse 8 PG II (totaal 600 kg > 250 kg): Niveau C. "
                           "CMR Repr. 2 vereist aanvullende arbo-maatregelen ongeacht PGS 15 niveau.")
                changes.append(f"[{i}] V-electrolyte rationale: CMR → aggregatie + arbo")

    # ============================================================
    # Samenvatting niveaubepaling — [85]
    # ============================================================

    if 'Geaggregeerd niveau: C (bepaald door CMR-stoffen + klasse 8 aggregatie).' in text:
        for run in p.runs:
            if 'Geaggregeerd' in run.text:
                run.text = ("Geaggregeerd niveau: C. Bepaald door ADR 6.1 PG III (<10.000 kg → C), "
                           "ADR 5.1 (<2.500 kg → C) en klasse 8 PG II aggregatie (600 kg > 250 kg → C).")
                changes.append(f"[{i}] Geaggregeerd niveau: correctie route")

    # ============================================================
    # §4.3 V-electrolyte heading — [163]
    # ============================================================

    if '4.3 Vanadium electrolyte — Niveau C (Repr. 2)' in text:
        for run in p.runs:
            if 'Niveau C (Repr. 2)' in run.text:
                run.text = run.text.replace('Niveau C (Repr. 2)', 'Niveau C (geaggregeerd klasse 8 + arbo CMR)')
                changes.append(f"[{i}] §4.3 heading: Niveau C (Repr.2) → Niveau C (geaggregeerd)")

    # ============================================================
    # §4.4 Klasse 8 aggregatie — [188] heading, [189] tekst
    # ============================================================

    if '4.4 Corrosieve stoffen (Klasse 8) — Niveau C (geaggregeerd)' in text:
        # heading is fine, keep it
        pass

    if 'Individueel Niveau B, maar aggregatie' in text and 'klasse 8 PG II' in text:
        for run in p.runs:
            if 'Individueel Niveau B' in run.text:
                run.text = run.text.replace(
                    'Individueel Niveau B, maar aggregatie (totale hoeveelheid > 250 kg ondergrens klasse 8 PG II) leidt tot Niveau C. Let op: Vanadium electrolyte is al Niveau C via CMR-route.',
                    'Individueel basisniveau (150 kg < 250 kg ondergrens, onbrandbaar → voetnoot c), maar aggregatie (4 x 150 kg = 600 kg > 250 kg ondergrens klasse 8 PG II) leidt tot Niveau C. Vanadium electrolyte: aanvullende arbo-maatregelen i.v.m. Repr. 2 (H361).'
                )
                changes.append(f"[{i}] §4.4 tekst: Niveau B → basisniveau + aggregatie C")

    # ============================================================
    # §4.5 Oxiderende stoffen — [203] heading, [204] tekst
    # ============================================================

    if '4.5 Oxiderende stoffen (Klasse 5.1) — Niveau B' in text:
        for run in p.runs:
            if 'Niveau B' in run.text:
                run.text = run.text.replace('Niveau B', 'Niveau C')
                changes.append(f"[{i}] §4.5 heading: Niveau B → Niveau C")

    if 'worden individueel toetst op Niveau B' in text:
        for run in p.runs:
            if 'Niveau B' in run.text:
                run.text = run.text.replace(
                    'worden individueel toetst op Niveau B. Aggregatie binnen klasse 5.1 is beperkt (verschillende PG\'s).',
                    'PGS 15:2025 Tabel 6: ADR 5.1 <2.500 kg → Niveau C (Wbdbo 60 min). Verschillende verpakkingsgroepen: aggregatie beperkt.'
                )
                changes.append(f"[{i}] §4.5 tekst: Niveau B → C conform Tabel 6")

    # ============================================================
    # §5 Samenvatting — [222]
    # ============================================================

    if 'Niveau C is het geaggregeerde niveau' in text and 'CMR-specifieke' in text:
        for run in p.runs:
            if 'CMR-specifieke' in run.text:
                run.text = ("Niveau C is het vereiste niveau voor de Pilot Plant, bepaald via ADR 6.1 (<10.000 kg → C), "
                           "ADR 5.1 (<2.500 kg → C) en klasse 8 PG II aggregatie (600 kg > 250 kg → C). "
                           "Alle Basis- en C-maatregelen zijn verplicht. Aanvullend gelden arbo-maatregelen voor CMR-stoffen "
                           "(Arbobesluit art. 4.1c) en milieu-specifieke maatregelen (H411).")
                changes.append(f"[{i}] §5 samenvatting: correctie route")

    # ============================================================
    # §6.1 Klasse 8 aggregatie — [225]
    # ============================================================

    if 'Totale hoeveelheid: 4 x 150 kg = 600 kg' in text and 'Aggregatie leidt tot Niveau C' in text:
        for run in p.runs:
            if 'Aggregatie leidt tot Niveau C' in run.text:
                run.text = run.text.replace(
                    'Aggregatie leidt tot Niveau C.',
                    'Aggregatie leidt tot Niveau C (Wbdbo 60 min). Individueel <250 kg ondergrens (voetnoot c: onbrandbaar = basisniveau).'
                )
                changes.append(f"[{i}] §6.1 aggregatie: toevoeging voetnoot c")

    # ============================================================
    # §6.2 Klasse 5.1 — [227]
    # ============================================================

    if 'Individueel Niveau B' in text and 'Verschillende verpakkingsgroepen' in text:
        for run in p.runs:
            if 'Individueel Niveau B' in run.text:
                run.text = run.text.replace(
                    'Individueel Niveau B.',
                    'PGS 15:2025 Tabel 6: ADR 5.1 <2.500 kg → Niveau C (Wbdbo 60 min).'
                )
                changes.append(f"[{i}] §6.2 klasse 5.1: Niveau B → C conform Tabel 6")

    # ============================================================
    # §6.3 CMR-route — [229]
    # ============================================================

    if 'De CMR-route is leidend: Niveau C ongeacht hoeveelheden.' in text:
        for run in p.runs:
            if 'CMR-route is leidend' in run.text:
                run.text = ("V₂O₅ (Carc. 1B) → Niveau C via ADR 6.1 PG III (<10.000 kg, Tabel 6). "
                           "NaVO₃ (Muta. 2 + Repr. 2) → Niveau C via ADR 6.1 PG III (<10.000 kg, Tabel 6). "
                           "V-electrolyte (Repr. 2) → Niveau C via klasse 8 PG II aggregatie (>250 kg). "
                           "Let op: CMR-classificatie vereist aanvullende arbo-maatregelen (Arbobesluit art. 4.1c), "
                           "maar PGS 15 niveaubepaling verloopt via ADR-route bij ADR-geclassificeerde stoffen.")
                changes.append(f"[{i}] §6.3 CMR-route: correctie naar ADR-route")

    # ============================================================
    # §4.1 heading — [88]
    # ============================================================

    if '4.1 Vanadium pentoxide — Niveau C (Carc. 1B)' in text:
        for run in p.runs:
            if 'Carc. 1B' in run.text:
                run.text = run.text.replace('Niveau C (Carc. 1B)', 'Niveau C (ADR 6.1 PG III)')
                changes.append(f"[{i}] §4.1 heading: Carc.1B → ADR 6.1 PG III")

    # ============================================================
    # §4.2 heading — [138]
    # ============================================================

    if '4.2 Sodium metavanadate — Niveau C (Muta. 2)' in text:
        for run in p.runs:
            if 'Muta. 2' in run.text:
                run.text = run.text.replace('Niveau C (Muta. 2)', 'Niveau C (ADR 6.1 PG III)')
                changes.append(f"[{i}] §4.2 heading: Muta.2 → ADR 6.1 PG III")

    # ============================================================
    # Vak 3 heading — [277]
    # ============================================================

    if 'Eisen Niveau B + scheidingseisen klasse 5.1' in text:
        for run in p.runs:
            if 'Niveau B' in run.text:
                run.text = run.text.replace('Eisen Niveau B + scheidingseisen klasse 5.1', 'Eisen Niveau C + scheidingseisen klasse 5.1')
                changes.append(f"[{i}] Vak 3: Niveau B → C")

# ============================================================
# VERSION TABLE — add norm correctie entry
# ============================================================

for table in doc.tables:
    header = [cell.text.strip() for cell in table.rows[0].cells]
    if 'Versie' in header and 'Wijziging' in header:
        # Change last row from v6.0 to updated description
        last_row = table.rows[-1]
        last_cells = last_row.cells
        if '6.0' in last_cells[0].text:
            for run in last_cells[2].paragraphs[0].runs:
                run.text = ('Norm-correctie: niveaus aangepast conform PGS 15:2025 Tabel 6. '
                           'ADR 5.1 → C (was B). ADR 8 → basisniveau individueel + C aggregatie. '
                           'ADR 6.1 → C via ADR-route (was CMR-route). Catch-all: van toepassing (2.700 kg > 1.000 kg).')
            changes.append("Versiehistorie: v6.0 entry geüpdatet met norm-correctie")
        break

# ============================================================
# Document header — update status
# ============================================================

for table in doc.tables:
    for row in table.rows:
        cells = row.cells
        if len(cells) >= 2:
            label = cells[0].text.strip()
            if label == 'Status':
                for run in cells[1].paragraphs[0].runs:
                    if 'Definitief' in run.text:
                        run.text = 'Definitief — 150 kg/ltr per product (PGS 15:2025 norm-gecorrigeerd)'
                        changes.append("Header: Status → norm-gecorrigeerd")

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