#!/usr/bin/env python3
"""
Scanner v6.0 → v6.1: Chirurgische PGS 15:2025 norm-correcties.
Layout en structuur onaangeroerd — alleen niveau/route data bijgewerkt.
"""
from docx import Document
import shutil

SRC = '/root/.openclaw/media/inbound/PGS15_Compliance_Toetsing_Scanner_Phoenix_Metals_v6.0---97b0d9cc-a5fc-4d15-abcc-380be1df5a8e.docx'
DST = '/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Compliance_Toetsing_Scanner_Phoenix_Metals_v6.1.docx'

doc = Document(SRC)
changes = []

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

    # === §3 NIVEAUBEPALING PER STOF ===

    # [43] H₂SO₄: "Niveau: Buiten scope" → "Niveau: Basis (voetnoot c: onbrandbaar)"
    if text.strip() == 'Niveau: Buiten scope' and i == 43:
        for run in p.runs:
            if 'Buiten scope' in run.text:
                run.text = run.text.replace('Niveau: Buiten scope', 'Niveau: Basis (voetnoot c: onbrandbaar, 150 kg < 250 kg ondergrens)')
                changes.append(f"[{i}] H₂SO₄: Buiten scope → Basis (voetnoot c)")

    # [58] HCl: "Niveau: Buiten scope" → "Niveau: Basis (voetnoot c)"
    if text.strip() == 'Niveau: Buiten scope' and i == 58:
        for run in p.runs:
            if 'Buiten scope' in run.text:
                run.text = run.text.replace('Niveau: Buiten scope', 'Niveau: Basis (voetnoot c: onbrandbaar, 150 kg < 250 kg ondergrens)')
                changes.append(f"[{i}] HCl: Buiten scope → Basis (voetnoot c)")

    # [63] Na-metavanadate: "Niveau: C" stays C, but rationale changes
    # [64] Na-metavanadate rationale: CMR-route → ADR 6.1 route
    if 'CMR via CLP-route: Repr. 2 (H361fd)' in text and 'IM-04' not in text and i == 64:
        for run in p.runs:
            if 'CMR via CLP-route' in run.text:
                run.text = ("ADR klasse 6.1 VG III, hoeveelheid 150 kg. "
                           "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: Muta. 2 (H341) + Repr. 2 (H361fd) vereist aanvullende arbo-maatregelen (Arbobesluit art. 4.1c).")
                changes.append(f"[{i}] Na-metavanadate: CMR-route → ADR 6.1 route + arbo")

    # [68] Na-aluminate: "Niveau: Buiten scope" → "Niveau: Basis (voetnoot c)"
    if text.strip() == 'Niveau: Buiten scope' and i == 68:
        for run in p.runs:
            if 'Buiten scope' in run.text:
                run.text = run.text.replace('Niveau: Buiten scope', 'Niveau: Basis (voetnoot c: onbrandbaar, 150 kg < 250 kg ondergrens)')
                changes.append(f"[{i}] Na-aluminate: Buiten scope → Basis (voetnoot c)")

    # [74] V₂O₅ rationale: CMR-route → ADR 6.1 route
    if 'CMR via CLP-route: Carc. 1B (H350)' in text and i == 74:
        for run in p.runs:
            if 'CMR via CLP-route' in run.text:
                run.text = ("ADR klasse 6.1 VG III, hoeveelheid 150 kg. "
                           "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 (Arbobesluit art. 4.1c). "
                           "Muta. 2 (H341) + Repr. 2 (H361fd) + Lact. (H362) aanwezig.")
                changes.append(f"[{i}] V₂O₅: CMR-route → ADR 6.1 route + arbo")

    # [78] V-electrolyte: "Niveau: Basis" → keep but clarify
    # Already says Basis — but rationale is wrong
    # [79] V-electrolyte rationale: fix
    if 'CMR via CLP-route: Repr. 2 (H361fd)' in text and 'EP-09' not in text and i == 79:
        for run in p.runs:
            if 'CMR via CLP-route' in run.text:
                run.text = ("ADR klasse 8 VG II, hoeveelheid 150 kg, onbrandbaar. "
                           "PGS 15:2025 Tabel 2: ondergrens 250 kg → 150 kg < 250 kg → basisniveau (voetnoot c: onbrandbaar). "
                           "Via aggregatie klasse 8 PG II (totaal 600 kg > 250 kg): Niveau C. "
                           "CMR: Repr. 2 (H361) vereist aanvullende arbo-maatregelen.")
                changes.append(f"[{i}] V-electrolyte: rationale → voetnoot c + aggregatie + arbo")

    # [93] Geaggregeerd niveau: "A" → "C"
    if 'Geaggregeerd niveau: A' in text:
        for run in p.runs:
            if 'A (bepaald' in run.text:
                run.text = run.text.replace(
                    'Geaggregeerd niveau: A (bepaald door CMR-stoffen + hoogste individuele niveau).',
                    'Geaggregeerd niveau: C (bepaald door ADR 6.1 <10.000 kg → C, ADR 5.1 <2.500 kg → C, klasse 8 aggregatie >250 kg → C).'
                )
                changes.append(f"[{i}] Geaggregeerd: A → C")

    # [94] Toelichting geaggregeerd
    if 'Vanadium pentoxide (Carc. 1B, H350) domineert' in text:
        for run in p.runs:
            if 'CMR-route verplicht' in run.text:
                run.text = ("Toelichting: Niveau C via drie routes — "
                           "(1) ADR 6.1 PG III <10.000 kg (V₂O₅, Na-metavanadate) → C, "
                           "(2) ADR 5.1 <2.500 kg (H₂O₂, Na-persulfate) → C, "
                           "(3) ADR 8 PG II aggregatie >250 kg (zuren + elektrolyt) → C. "
                           "CMR: arbo-maatregelen Arbobesluit art. 4.1c, niet niveau-bepalend per PGS 15:2025.")
                changes.append(f"[{i}] Toelichting: CMR-route → ADR-routes")

    # === §4 HEADINGS ===

    # [97] IM-04 heading
    if 'IM-04 Sodium metavanadate — Niveau C (Repr. 2 (H361fd))' in text:
        for run in p.runs:
            if 'Repr. 2 (H361fd)' in run.text:
                run.text = run.text.replace('Niveau C (Repr. 2 (H361fd))', 'Niveau C (ADR 6.1 PG III)')
                changes.append(f"[{i}] IM-04 heading: Repr.2 → ADR 6.1")

    # [98] IM-04 text
    if 'bereikt Niveau C via de CMR-route: Repr. 2' in text and 'metavanadate' in text:
        for run in p.runs:
            if 'CMR-route' in run.text:
                run.text = run.text.replace(
                    'bereikt Niveau C via de CMR-route: Repr. 2 (H361fd). ADR klasse 6.1 PG III.',
                    'bereikt Niveau C via ADR 6.1 PG III (<10.000 kg, PGS 15:2025 Tabel 6). CMR: Muta. 2 (H341) + Repr. 2 (H361fd) vereist arbo-maatregelen.'
                )
                changes.append(f"[{i}] IM-04 text: CMR → ADR")

    # [104] EP-08 heading
    if 'EP-08 Vanadium pentoxide — Niveau C (Carc. 1B (H350)' in text:
        for run in p.runs:
            if 'Carc. 1B (H350)' in run.text:
                run.text = run.text.replace('Niveau C (Carc. 1B (H350), Muta. 2 (H341), Repr. 2 (H361fd))', 'Niveau C (ADR 6.1 PG III)')
                changes.append(f"[{i}] EP-08 heading: Carc.1B → ADR 6.1")

    # [105] EP-08 text
    if 'bereikt Niveau C via de CMR-route: Carc. 1B' in text and 'pentoxide' in text:
        for run in p.runs:
            if 'CMR-route' in run.text:
                run.text = run.text.replace(
                    'bereikt Niveau C via de CMR-route: Carc. 1B (H350), Muta. 2 (H341), Repr. 2 (H361fd). ADR klasse 6.1 PG III.',
                    'bereikt Niveau C via ADR 6.1 PG III (<10.000 kg, PGS 15:2025 Tabel 6). Carc. 1B (H350) vereist aanvullende arbo-maatregelen (Arbobesluit art. 4.1c).'
                )
                changes.append(f"[{i}] EP-08 text: CMR → ADR + arbo")

    # [118] EP-09 heading: keep "Niveau Basis" but clarify
    if 'EP-09 Vanadium electrolyte — Niveau Basis (Repr. 2 (H361fd))' in text:
        for run in p.runs:
            if 'Niveau Basis (Repr. 2' in run.text:
                run.text = run.text.replace('Niveau Basis (Repr. 2 (H361fd))', 'Niveau Basis (voetnoot c) / C (aggregatie)')
                changes.append(f"[{i}] EP-09 heading: Basis → Basis/C aggregatie")

    # [119] EP-09 text
    if 'bereikt Niveau Basis via de CMR-route' in text and 'electrolyte' in text:
        for run in p.runs:
            if 'CMR-route' in run.text:
                run.text = run.text.replace(
                    'bereikt Niveau Basis via de CMR-route: Repr. 2 (H361fd). ADR klasse 8 PG II.',
                    'bereikt basisniveau (voetnoot c: onbrandbaar, 150 kg < 250 kg ondergrens). Via aggregatie klasse 8 PG II (totaal 600 kg > 250 kg): Niveau C. CMR Repr. 2 (H361) vereist arbo-maatregelen.'
                )
                changes.append(f"[{i}] EP-09 text: CMR → voetnoot c + aggregatie")

    # §4.x Overige ADR stoffen

    # [126] IC-02 H₂SO₄ heading: "Buiten scope" → "Basis (voetnoot c)"
    if 'IC-02 Sulfuric acid 25% — Niveau Buiten scope' in text:
        for run in p.runs:
            if 'Buiten scope' in run.text:
                run.text = run.text.replace('Niveau Buiten scope', 'Niveau Basis (voetnoot c)')
                changes.append(f"[{i}] IC-02 heading: Buiten scope → Basis")

    # [127] IC-02 text
    if 'Niveau Buiten scope conform PGS 15:2025 Tabel 6' in text and 'H290, H314, H318' in text:
        for run in p.runs:
            if 'Buiten scope' in run.text:
                run.text = run.text.replace(
                    'Niveau Buiten scope conform PGS 15:2025 Tabel 6. ADR klasse 8 VG II, hoeveelheid 150 kg.',
                    'Onbrandbaar. PGS 15:2025 Tabel 2: ondergrens 250 kg → 150 kg < 250 kg → basisniveau (voetnoot c). Via aggregatie klasse 8 PG II (totaal 600 kg > 250 kg): Niveau C.'
                )
                changes.append(f"[{i}] IC-02 text: Buiten scope → Basis + aggregatie")

    # [130] IC-03 H₂O₂ text: stays C but fix rationale
    if 'Acute toxiciteit cat. 3 gedetecteerd' in text and 'IC-03' not in text and i == 130:
        for run in p.runs:
            if 'Acute toxiciteit' in run.text:
                run.text = run.text.replace(
                    'Niveau C conform PGS 15:2025 Tabel 6. ADR klasse 5.1 VG II, hoeveelheid 150 kg. Acute toxiciteit cat. 3 gedetecteerd.',
                    'Niveau C conform PGS 15:2025 Tabel 6: ADR 5.1 <2.500 kg → C (Wbdbo 60 min). ADR klasse 5.1 VG II, hoeveelheid 150 kg.'
                )
                changes.append(f"[{i}] IC-03 text: fix rationale naar Tabel 6")

    # [133] IC-04 Na-persulfate text: stays C but fix rationale
    if 'Acute toxiciteit cat. 3 gedetecteerd' in text and i == 133:
        for run in p.runs:
            if 'Acute toxiciteit' in run.text:
                run.text = run.text.replace(
                    'Niveau C conform PGS 15:2025 Tabel 6. ADR klasse 5.1 VG III, hoeveelheid 150 kg. Acute toxiciteit cat. 3 gedetecteerd.',
                    'Niveau C conform PGS 15:2025 Tabel 6: ADR 5.1 <2.500 kg → C (Wbdbo 60 min). ADR klasse 5.1 VG III, hoeveelheid 150 kg. H334 (sensibiliserend): luchtkwaliteit aandachtspunt.'
                )
                changes.append(f"[{i}] IC-04 text: fix rationale naar Tabel 6")

    # [135] IC-05 HCl heading: "Buiten scope" → "Basis (voetnoot c)"
    if 'IC-05 Hydrochloric acid 20% — Niveau Buiten scope' in text:
        for run in p.runs:
            if 'Buiten scope' in run.text:
                run.text = run.text.replace('Niveau Buiten scope', 'Niveau Basis (voetnoot c)')
                changes.append(f"[{i}] IC-05 heading: Buiten scope → Basis")

    # [136] IC-05 text
    if 'Niveau Buiten scope conform PGS 15:2025 Tabel 6' in text and 'H290, H315, H319, H335' in text:
        for run in p.runs:
            if 'Buiten scope' in run.text:
                run.text = run.text.replace(
                    'Niveau Buiten scope conform PGS 15:2025 Tabel 6. ADR klasse 8 VG II, hoeveelheid 150 kg.',
                    'Onbrandbaar. PGS 15:2025 Tabel 2: ondergrens 250 kg → 150 kg < 250 kg → basisniveau (voetnoot c). Via aggregatie klasse 8 PG II (totaal 600 kg > 250 kg): Niveau C.'
                )
                changes.append(f"[{i}] IC-05 text: Buiten scope → Basis + aggregatie")

    # [138] EP-06 Na-aluminate heading: "Buiten scope" → "Basis (voetnoot c)"
    if 'EP-06 Sodium aluminate solution — Niveau Buiten scope' in text:
        for run in p.runs:
            if 'Buiten scope' in run.text:
                run.text = run.text.replace('Niveau Buiten scope', 'Niveau Basis (voetnoot c)')
                changes.append(f"[{i}] EP-06 heading: Buiten scope → Basis")

    # [139] EP-06 text — also fix PG III → PG II
    if 'Niveau Buiten scope conform PGS 15:2025 Tabel 6' in text and 'H314, H318' in text and i == 139:
        for run in p.runs:
            if 'Buiten scope' in run.text:
                run.text = run.text.replace(
                    'ADR klasse 8 PG III. H-codes: H314, H318. Niveau Buiten scope conform PGS 15:2025 Tabel 6. ADR klasse 8 VG III, hoeveelheid 150 kg.',
                    'ADR klasse 8 PG II (SDS: PG II). H-codes: H314, H318. Onbrandbaar. PGS 15:2025 Tabel 2: ondergrens 250 kg → 150 kg < 250 kg → basisniveau (voetnoot c).'
                )
                changes.append(f"[{i}] EP-06 text: Buiten scope → Basis + PG fix")

    # === §6 CMR-TOETSING ===

    # [161] V-electrolyte in CMR section
    if 'Vanadium elektrolyt (EP-09): Repr. 2 (H361) + STOT SE 1 (H370). Niveau C via CMR-route.' in text:
        for run in p.runs:
            if 'CMR-route' in run.text:
                run.text = run.text.replace(
                    'Niveau C via CMR-route.',
                    'Niveau C via klasse 8 PG II aggregatie (>250 kg). CMR Repr. 2 (H361) vereist arbo-maatregelen.'
                )
                changes.append(f"[{i}] §6 V-electrolyte: CMR-route → aggregatie + arbo")

    # === §8 VEREIST VEILIGHEIDSNIVEAU ===

    # [172] V₂O₅ CMR-route → ADR
    if 'V₂O₅ (Carc. 1B) verplicht Niveau C via de CMR-route' in text:
        for run in p.runs:
            if 'CMR-route' in run.text:
                run.text = run.text.replace(
                    'V₂O₅ (Carc. 1B) verplicht Niveau C via de CMR-route, ongeacht de ADR 6.1 PG III hoeveelheidsdrempel.',
                    'V₂O₅ (Carc. 1B) bereikt Niveau C via ADR 6.1 PG III <10.000 kg (PGS 15:2025 Tabel 6). Carc. 1B vereist aanvullende arbo-maatregelen.'
                )
                changes.append(f"[{i}] §8 V₂O₅: CMR-route → ADR 6.1")

    # [174] V-electrolyte in §8
    if 'Vanadium elektrolyt (Repr. 2) bevestigt Niveau C.' in text:
        for run in p.runs:
            if 'bevestigt Niveau C' in run.text:
                run.text = run.text.replace(
                    'Vanadium elektrolyt (Repr. 2) bevestigt Niveau C.',
                    'Vanadium elektrolyt (Repr. 2): Niveau C via klasse 8 PG II aggregatie (>250 kg).'
                )
                changes.append(f"[{i}] §8 V-electrolyte: bevestigt C → aggregatie C")

    # [175] Niveau B en A zin
    if 'Niveau B en A zijn niet van toepassing (geen brandbare stoffen' in text:
        for run in p.runs:
            if 'niet van toepassing' in run.text:
                run.text = run.text.replace(
                    'Niveau C is het hoogste vastgestelde niveau voor deze locatie. Niveau B en A zijn niet van toepassing (geen brandbare stoffen, geen hoeveelheden > 10.000 kg ADR 8/5.1).',
                    'Niveau C is het vereiste niveau, bepaald via ADR 6.1 <10.000 kg → C, ADR 5.1 <2.500 kg → C, en ADR 8 PG II aggregatie >250 kg → C. Niveau B en A zijn niet van toepassing (geen brandbare stoffen, geen hoeveelheden >2.500/10.000 kg).'
                )
                changes.append(f"[{i}] §8 conclusie: correctie route")

# === VERSION TABLE ===
for table in doc.tables:
    header = [cell.text.strip() for cell in table.rows[0].cells]
    if 'Versie' in header and 'Wijziging' in header:
        last = table.rows[-1]
        if '6.0' in last.cells[0].text:
            for run in last.cells[0].paragraphs[0].runs:
                run.text = run.text.replace('6.0', '6.1')
            for run in last.cells[2].paragraphs[0].runs:
                run.text = ('PGS 15:2025 norm-correctie: voetnoot c geïmplementeerd (ADR 8 onbrandbaar = basisniveau). '
                           'ADR-route leidend i.p.v. CMR-route. ADR 5.1 → C bevestigd. Aggregatie correct.')
            changes.append("Versietabel: v6.0 → v6.1")
        break

# === HEADER TABLE ===
for table in doc.tables:
    for row in table.rows:
        cells = row.cells
        if len(cells) >= 2:
            label = cells[0].text.strip()
            if label == 'Versie':
                for run in cells[1].paragraphs[0].runs:
                    if '6.0' in run.text:
                        run.text = run.text.replace('6.0', '6.1')
                        changes.append("Header: Versie → 6.1")
            elif label == 'Status':
                for run in cells[1].paragraphs[0].runs:
                    run.text = 'Definitief — PGS 15:2025 norm-gecorrigeerd (Engine v2)'
                    changes.append("Header: Status → norm-gecorrigeerd")
            elif label == 'Datum':
                for run in cells[1].paragraphs[0].runs:
                    run.text = '7 mei 2026'
                    changes.append("Header: Datum → 7 mei 2026")
            elif label == 'Auteur':
                for run in cells[1].paragraphs[0].runs:
                    run.text = 'JvG Consultancy — PGS-15 Scanner Engine v2'
                    changes.append("Header: Auteur → Engine v2")

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