import re
from parsers.utils import extract_raw_text


def parse(pdf_path, vendor):

    raw_text = extract_raw_text(pdf_path)

    invoice_number = ""
    purchase_order = ""
    total_amount = ""

    # =========================
    # 1️⃣ Invoice Number
    # Match: Invoice # 745755
    # =========================
    match = re.search(
        r"Invoice\s*#\s*(\d+)",
        raw_text,
        re.IGNORECASE
    )
    if match:
        invoice_number = match.group(1)

    # =========================
    # 2️⃣ Purchase Order
    # Match: PO # PO000135262
    # =========================
    match = re.search(
        r"PO\s*#\s*(PO\d+)",
        raw_text,
        re.IGNORECASE
    )
    if match:
        purchase_order = match.group(1)

    # =========================
    # 3️⃣ Total Amount
    # Prefer Amount Due (most reliable)
    # =========================
    match = re.search(
        r"Amount\s*Due\s*\$?([\d,]+\.\d{2})",
        raw_text,
        re.IGNORECASE
    )
    if match:
        total_amount = match.group(1)
    else:
        # fallback to Total line
        match = re.search(
            r"\bTotal\s*\n?\s*([\d,]+\.\d{2})",
            raw_text,
            re.IGNORECASE
        )
        if match:
            total_amount = match.group(1)

    return {
        "vendor_name": vendor,
        "invoice_number": invoice_number,
        "purchase_order": purchase_order,
        "total_amount_usd": f"${total_amount}" if total_amount else "",
        "status": "Processed" if invoice_number else "Pending"
    }