#!/usr/bin/env python3
"""Extract text from all Phoenix Metals source documents."""
import os, sys, traceback

def extract_docx(path):
    from docx import Document
    doc = Document(path)
    text = []
    for p in doc.paragraphs:
        text.append(p.text)
    for table in doc.tables:
        for row in table.rows:
            cells = [cell.text.strip() for cell in row.cells]
            text.append(" | ".join(cells))
    return "\n".join(text)

def extract_xlsx(path):
    from openpyxl import load_workbook
    wb = load_workbook(path, data_only=True)
    text = []
    for ws in wb.worksheets:
        text.append(f"=== Sheet: {ws.title} ===")
        for row in ws.iter_rows(max_row=min(ws.max_row, 200), values_only=True):
            cells = [str(c) if c is not None else "" for c in row]
            if any(cells):
                text.append(" | ".join(cells))
    return "\n".join(text)

def extract_pdf(path):
    import fitz
    doc = fitz.open(path)
    text = []
    for page_num, page in enumerate(doc):
        text.append(page.get_text())
    return "\n".join(text)

def extract_pptx(path):
    from pptx import Presentation
    prs = Presentation(path)
    text = []
    for i, slide in enumerate(prs.slides):
        text.append(f"=== Slide {i+1} ===")
        for shape in slide.shapes:
            if shape.has_text_frame:
                for p in shape.text_frame.paragraphs:
                    text.append(p.text)
            if shape.has_table:
                for row in shape.table.rows:
                    cells = [cell.text.strip() for cell in row.cells]
                    text.append(" | ".join(cells))
    return "\n".join(text)

def extract(path):
    ext = path.lower().split('.')[-1]
    if ext == 'docx':
        return extract_docx(path)
    elif ext in ('xlsx', 'xlsm'):
        return extract_xlsx(path)
    elif ext == 'pdf':
        return extract_pdf(path)
    elif ext == 'pptx':
        return extract_pptx(path)
    else:
        return f"[Unsupported format: {ext}]"

# All sources
SOURCES = [
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Vergunning/2025-09-23, verslag vooroverleg.docx", "04_vooroverleg"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Vergunning/Uitgangspuntendocument Phoenix Metals IJmuiden.xlsx", "05_uitgangspunten"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Vergunning/Vergunning to do.docx", "06_vergunning_todo"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Vergunning/Overzicht vragen Arcadis update 10-3-2025.xlsx", "07_arcadis_vragen"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Business plan - Haalbaarheids studie/Pilot_Plant_Project_Execution_Plan_Index.docx", "29_pm_execution_plan"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/phoenix metals.docx", "08_pm_bedrijfsomschrijving"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/algemene omschrijving process/1. Block Flow Diagram.pdf", "09_pm_bfd"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/algemene omschrijving process/2. PFD.pdf", "10_pm_pfd"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/algemene omschrijving process/3. Plant Layout.pdf", "11_pm_layout"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Demonstrator/Demo Process VR.docx", "12_pm_demo_process"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Demonstrator/Demo Layout.pdf", "13_pm_demo_layout"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Gevaarlijke stoffen/Phoenix Metals 2024/Registration Chemicals - MASTER (versie 2.1.0) - beveiligd.xlsx", "14_pm_chemicals_master"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Gevaarlijke stoffen/MSDS/vanadium concentrate Versie 1-nl.pdf", "15_msds_vanadium"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Gevaarlijke stoffen/MSDS/Sulfuric acid_ 25-75% Versie 1-en.pdf", "16_msds_zwavelzuur"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Gevaarlijke stoffen/MSDS/Sodium hydroxide 50% Versie 1-en.pdf", "17_msds_naoh"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Gevaarlijke stoffen/PE-240-01-NL_V1 Aanvraag, beoordeling en goedkeuring van gevaarlijke stoffen.docx", "18_pm_gevaarlijke_stoffen_procedure"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/HSE policy/Phoenix metals HSE policy 2025/Phoenix Metals HSE policy-V1.docx", "19_pm_hse_policy"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Bedrijfsnoodplan/Phoenix metals BNP 2024/Phoenix Metals BNP-V1.docx", "20_pm_bnp"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/PBM's/Phoenix Metals 2024/PBM matrix/PBM matrix Phoenix metals draft 1.xlsm", "21_pm_pbm_matrix"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/PBM's/Phoenix Metals 2024/PS-201-NL_V1.0 - PBM beleid.docx", "22_pm_pbm_beleid"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/compliance/Register Relevante Milieu Wet- en Regelgeving.pdf", "23_pm_milieu_wetgeving"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/compliance/Environmental Aspects and Compliance Register.xlsx", "24_pm_compliance_register"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/compliance/Legal compliance Phoenix Metals 2025.pptx", "25_pm_legal_compliance"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/document control/overzicht RELATIE BRZO ELEMENTEN EN VBS 3.xlsx", "26_pm_brzo_overzicht"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Info Ash/Fact sheet Ash 2025.docx", "27_pm_fact_sheet_ash"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/RI&E/instrumenten.rie/Inventarisatie PhoenixMetals (1).docx", "31_pm_rie_inventarisatie"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/RI&E/instrumenten.rie/Inventarisatie PhoenixMetals MKB.docx", "32_pm_rie_mkb"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/RI&E/RIE Action plan and follow up.docx", "33_pm_rie_actionplan"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/Management of change/Phoenix metals 2024/PE-202-01_V1 Management of Change.docx", "30_pm_moc"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/PSSR/HS-P-0018 Procedure PSSR.docx", "36_pm_pssr"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/document control/PE-210-04-NL_V14 Opstellen wijzigen goedkeuren en archiveren van voorschrijvende documenten van het managementsysteem.pdf", "37_pm_document_control"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/competence management/Phoenix Metals 2024/Phoenix metals opleiding matrix 2024.xlsx", "38_pm_opleiding_matrix"),
    ("/root/Documents/HSEQ_kennis/Phoenixmetals/trainingen/Exposure to hazardous chemicals Phoenix Metals 2025.pptx", "39_pm_training_chemicals"),
    ("/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Kennisdossier_Phoenix_Metals_D1_v1.0.docx", "40_v1_d1"),
    ("/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Veilig_Werken_HSEQ_Phoenix_Metals_D2_v1.0.docx", "41_v1_d2"),
    ("/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Veiligheidsinfo_Documentatie_Phoenix_Metals_D3_v1.0.docx", "42_v1_d3"),
    ("/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Master_SOP_Opslagveiligheid_Phoenix_Metals_D4_v1.0.docx", "43_v1_d4"),
    ("/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Basishandleiding_v2.0.docx", "44_v2_basishandleiding"),
    ("/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Audit_Procedure_v2.0.docx", "45_v2_audit_procedure"),
    ("/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Audit_Checklist_v2.0.docx", "46_v2_audit_checklist"),
    ("/root/projects/jg/2026-pgs15-phoenix-metals/deliverables/docx/PGS15_Audit_Rapport_Template_v2.0.docx", "47_v2_audit_rapport_template"),
]

OUTDIR = "/root/projects/jg/2026-pgs15-phoenix-metals/working/bronnen_data/raw"
os.makedirs(OUTDIR, exist_ok=True)

for path, name in SOURCES:
    outpath = os.path.join(OUTDIR, f"{name}.txt")
    if not os.path.exists(path):
        print(f"MISSING: {name} — {path}")
        with open(outpath, 'w') as f:
            f.write(f"[BESTAND NIET GEVONDEN: {path}]")
        continue
    try:
        text = extract(path)
        with open(outpath, 'w') as f:
            f.write(text[:50000])  # cap at 50K chars
        size = os.path.getsize(path)
        print(f"OK: {name} ({len(text)} chars, source {size//1024}KB)")
    except Exception as e:
        print(f"ERROR: {name} — {e}")
        with open(outpath, 'w') as f:
            f.write(f"[EXTRACTIE FOUT: {e}]\n{traceback.format_exc()}")

print("\nKLAAR — alle ruwe extracts opgeslagen.")
