"""
PDF issuer detector — Story 9.2.

Identifies the card issuer from raw PDF text using pattern matching.
Returns (issuer_key, confidence) where confidence is 0.0–1.0.
Confidence < 0.7 should prompt user to confirm.

Supported issuers: chase, bofa, apple_card, kohls, discover, dcu, target
"""
from __future__ import annotations

import re


SUPPORTED_ISSUERS = {
    "chase":      "Chase",
    "bofa":       "Bank of America",
    "apple_card": "Apple Card",
    "kohls":      "Kohl's",
    "discover":   "Discover",
    "dcu":        "DCU Credit Union",
    "target":     "Target RedCard",
}

# (pattern, issuer_key, weight)
_PATTERNS: list[tuple[re.Pattern, str, float]] = [
    (re.compile(r"JPMORGAN\s*CHASE", re.I),               "chase",      1.0),
    (re.compile(r"\bCHASE\s+BANK\b", re.I),               "chase",      0.95),
    (re.compile(r"\bchase\.com\b", re.I),                  "chase",      0.90),
    (re.compile(r"\bJPMorgan\b", re.I),                    "chase",      0.80),

    (re.compile(r"Bank\s+of\s+America", re.I),             "bofa",       1.0),
    (re.compile(r"\bBankofAmerica\b", re.I),               "bofa",       1.0),
    (re.compile(r"\bbankofamerica\.com\b", re.I),          "bofa",       0.95),
    (re.compile(r"\bBANK\s+OF\s+AMERICA\b", re.I),        "bofa",       1.0),

    (re.compile(r"\bApple\s+Card\b", re.I),                "apple_card", 1.0),
    (re.compile(r"Goldman\s+Sachs\s+Bank", re.I),          "apple_card", 0.85),
    (re.compile(r"titanium.*apple|apple.*titanium", re.I), "apple_card", 0.75),

    (re.compile(r"Kohl['']s\s+Charge\s+Card", re.I),      "kohls",      1.0),
    (re.compile(r"Kohl['']s\s+Credit", re.I),             "kohls",      0.95),
    (re.compile(r"\bKohl['']s\b", re.I),                   "kohls",      0.75),

    (re.compile(r"\bDISCOVER\s+CARD\b", re.I),            "discover",   1.0),
    (re.compile(r"\bDiscover\s+it\b", re.I),               "discover",   0.95),
    (re.compile(r"discover\.com", re.I),                   "discover",   0.85),
    (re.compile(r"\bDISCOVER\b", re.I),                    "discover",   0.70),

    (re.compile(r"Digital\s+Federal\s+Credit\s+Union", re.I), "dcu",    1.0),
    (re.compile(r"\bDCU\b"),                               "dcu",        0.85),
    (re.compile(r"dcu\.org", re.I),                        "dcu",        0.90),

    (re.compile(r"Target\s+REDcard", re.I),                "target",     1.0),
    (re.compile(r"Target\s+Red\s+Card", re.I),             "target",     0.95),
    (re.compile(r"\bREDcard\b", re.I),                     "target",     0.85),
    (re.compile(r"\btarget\.com\b", re.I),                 "target",     0.70),
]

_CONFIDENCE_THRESHOLD = 0.70


def detect_issuer(text: str) -> tuple[str | None, float]:
    """
    Scan PDF text for issuer patterns.

    Returns:
        (issuer_key, confidence) — issuer_key is None if no match found.
        confidence < _CONFIDENCE_THRESHOLD means user should confirm.
    """
    if not text:
        return None, 0.0

    scores: dict[str, float] = {}
    for pattern, issuer_key, weight in _PATTERNS:
        if pattern.search(text):
            current = scores.get(issuer_key, 0.0)
            scores[issuer_key] = max(current, weight)

    if not scores:
        return None, 0.0

    best_issuer = max(scores, key=lambda k: scores[k])
    best_confidence = scores[best_issuer]
    return best_issuer, best_confidence


def needs_confirmation(confidence: float) -> bool:
    return confidence < _CONFIDENCE_THRESHOLD
