"""Deterministic classifiers used by the series merge (no AI).

Everything here is a pure function of stored data, so it can be re-run on old extractions:
- report/page scope from the printed statement titles (not the extraction model's guess)
- value kind and unit (currency / per share / count / percent / ratio)
- section key (a small fixed set, so model wording changes don't matter)
- label key (the normalised line-item name used to recognise the same line across reports)
- value agreement with a tolerance per value kind
"""

import functools
import re
from typing import Any, Dict, Iterable, List, Optional, Tuple

from .csv_service import extract_year_int, normalize_field_name, normalize_unit
from .extraction_verifier import parse_markdown_tables

CONSOLIDATED = "CONSOLIDATED"
STANDALONE = "STANDALONE"

# ---------------------------------------------------------------------------
# Scope
# ---------------------------------------------------------------------------
_CONS_TITLE_RE = re.compile(
    r"\bconsolidated\s+(statements?\s+of|balance\s+sheet|income\s+statement|profit\s+(and|&)\s+loss|"
    r"cash\s+flows?|financial\s+position|comprehensive\s+income)", re.I)
_STATEMENT_TITLE_RE = re.compile(
    r"\b(statements?\s+of\s+(financial\s+position|profit|comprehensive\s+income|cash\s+flows?|changes\s+in\s+equity)|"
    r"balance\s+sheet|income\s+statement|profit\s+(and|&)\s+loss\s+(account|statement)|cash\s+flow\s+statement)", re.I)
_SEPARATE_RE = re.compile(r"\b(separate|standalone|stand-alone|solo)\b", re.I)


def page_heading(markdown: str, max_lines: int = 10) -> str:
    """The text printed above the page's first table (statement title, entity, date, unit)."""
    out = []
    for line in (markdown or "").splitlines():
        s = line.strip()
        if s.startswith("|"):
            break
        if s:
            out.append(s)
        if len(out) >= max_lines:
            break
    return " ".join(out)


def page_title_scope(markdown: str) -> Optional[str]:
    """CONSOLIDATED / STANDALONE from the page's printed title, or None when the page has no title."""
    head = page_heading(markdown)
    if _CONS_TITLE_RE.search(head):
        return STANDALONE if _SEPARATE_RE.search(head) and not re.search(r"consolidated\s+and\s+separate", head, re.I) else CONSOLIDATED
    if _SEPARATE_RE.search(head) or _STATEMENT_TITLE_RE.search(head):
        return STANDALONE
    return None


def resolve_report_scopes(pages: Dict[int, str], model_scopes: Dict[int, set]) -> Dict[int, Optional[str]]:
    """Scope for each page of ONE report: {page_number: "CONSOLIDATED" | "STANDALONE" | None}.

    None means "keep the extraction model's per-row scope" (a page that prints both scopes in its
    columns). Rules:
    - a report with no consolidated statement title is a single entity: every page is STANDALONE;
    - a page titled "Consolidated ..." is CONSOLIDATED, a page titled as the company's own statement
      is STANDALONE;
    - an untitled page (continuation or notes) takes the report's dominant titled scope;
    - a page whose rows the model split into both scopes is left to the model (dual-column page).
    """
    titles = {p: page_title_scope(md) for p, md in pages.items()}
    if not any(t == CONSOLIDATED for t in titles.values()):
        return {p: STANDALONE for p in set(pages) | set(model_scopes)}
    titled = [t for t in titles.values() if t]
    dominant = CONSOLIDATED if titled.count(CONSOLIDATED) >= titled.count(STANDALONE) else STANDALONE
    out: Dict[int, Optional[str]] = {}
    for p in set(pages) | set(model_scopes):
        if len(model_scopes.get(p, set())) > 1:
            out[p] = None
        else:
            out[p] = titles.get(p) or dominant
    return out


# ---------------------------------------------------------------------------
# Value kind and unit
# ---------------------------------------------------------------------------
CURRENCY, PER_SHARE, COUNT, PERCENT, RATIO = "CURRENCY", "PER_SHARE", "COUNT", "PERCENT", "RATIO"
VALUE_KINDS = (CURRENCY, PER_SHARE, COUNT, PERCENT, RATIO)

_PER_SHARE_RE = re.compile(r"per\s+(ordinary\s+)?share|\beps\b|\bnavps\b|\bnocfps\b|\bdividend\s+per\b|earnings?\s+per", re.I)
_COUNT_RE = re.compile(
    r"\bnumber\s+of\s+(ordinary\s+)?shares\b|\bno\.?\s+of\s+shares\b|\bshares\s+outstanding\b|"
    r"\bweighted\s+average\s+(number\s+of\s+)?(ordinary\s+)?shares\b|\bnumber\s+of\s+(employees|branches|customers|accounts)\b", re.I)
_PERCENT_RE = re.compile(r"%|\bpercent(age)?\b|\bmargin\b|\byield\b|\breturn\s+on\b", re.I)
_RATIO_RE = re.compile(r"\bratio\b|\(times\)|\btimes\b|\bcoverage\b", re.I)
_MODEL_KIND = {"PER_SHARE_RATIO": PER_SHARE, "PER_SHARE": PER_SHARE, "SHARES": COUNT, "COUNT": COUNT,
               "PERCENTAGE": PERCENT, "PERCENT": PERCENT, "RATIO": RATIO, "CURRENCY": CURRENCY}


def value_kind(label: str, units: Iterable[Optional[str]] = (), model_type: Optional[str] = None,
               values: Iterable[Optional[float]] = ()) -> str:
    """CURRENCY | PER_SHARE | COUNT | PERCENT | RATIO from the printed label, units and magnitude."""
    text = label or ""
    unit_l = {str(u).strip().lower() for u in units if u}
    small = all(v is None or abs(v) <= 1000 for v in values)
    if _PER_SHARE_RE.search(text):
        return PER_SHARE
    if _COUNT_RE.search(text) or unit_l & {"nos", "nos.", "shares", "number", "numbers"}:
        return COUNT
    if small and ("%" in unit_l or _PERCENT_RE.search(text) or _MODEL_KIND.get(str(model_type or "").upper()) == PERCENT):
        return PERCENT
    if small and _RATIO_RE.search(text):
        return RATIO
    kind = _MODEL_KIND.get(str(model_type or "").upper())
    if kind in (PER_SHARE, RATIO) and small:
        return kind
    return CURRENCY


def unit_for_kind(kind: str, units: Iterable[Optional[str]] = ()) -> str:
    """The unit written for a row: currency code, '<currency>/share', 'Nos.', '%' or 'Times'."""
    currency = "BDT"
    for u in units:
        nu = normalize_unit(u) if u else ""
        if nu in ("BDT", "USD", "EUR"):
            currency = nu
            break
    return {PER_SHARE: f"{currency}/share", COUNT: "Nos.", PERCENT: "%", RATIO: "Times"}.get(kind, currency)


# ---------------------------------------------------------------------------
# Section key
# ---------------------------------------------------------------------------
_SECTION_RULES: List[Tuple[str, re.Pattern]] = [
    ("PER_SHARE", re.compile(r"per\s+share|\beps\b", re.I)),
    ("OCI", re.compile(r"other\s+comprehensive", re.I)),
    ("NCA", re.compile(r"non[\s-]*current\s+assets?|fixed\s+assets?|long[\s-]*term\s+assets?|property,?\s+plant", re.I)),
    ("CA", re.compile(r"current\s+assets?", re.I)),
    ("NCL", re.compile(r"non[\s-]*current\s+liabilit|long[\s-]*term\s+liabilit", re.I)),
    ("CL", re.compile(r"current\s+liabilit", re.I)),
    ("EQ", re.compile(r"equity|reserve|share\s+capital|shareholders", re.I)),
    ("TAX", re.compile(r"\btax", re.I)),
    ("COS", re.compile(r"cost\s+of\s+(sales|goods|revenue)", re.I)),
    ("FIN", re.compile(r"financ(e|ial)\s+(cost|income|expense)|interest", re.I)),
    ("REV", re.compile(r"revenue|turnover|\bsales\b|operating\s+income", re.I)),
    ("OPEX", re.compile(r"expense|overhead|administrative|distribution", re.I)),
]
_CF_RULES: List[Tuple[str, re.Pattern]] = [
    ("CF_OP", re.compile(r"operating", re.I)),
    ("CF_INV", re.compile(r"investing", re.I)),
    ("CF_FIN", re.compile(r"financing", re.I)),
    ("CF_CASH", re.compile(r"cash\s+(and|&)\s+cash\s+equivalent|net\s+(increase|decrease|change)|opening|closing|beginning|end\s+of", re.I)),
]
# Pairs of sections that can never hold the same line item.
SECTION_CONTRASTS = {frozenset(p) for p in (
    ("CA", "NCA"), ("CL", "NCL"), ("CF_OP", "CF_INV"), ("CF_OP", "CF_FIN"), ("CF_INV", "CF_FIN"),
    ("CA", "CL"), ("NCA", "NCL"), ("CA", "NCL"), ("NCA", "CL"),
)}


def section_key(category: Optional[str], statement_type: str = "") -> str:
    text = category or ""
    rules = _CF_RULES if statement_type == "CASH_FLOW" else _SECTION_RULES
    for key, pat in rules:
        if pat.search(text):
            return key
    return "OTHER"


_SIDE_ASSET_RE = re.compile(r"\bassets?\b", re.I)
_SIDE_LIAB_RE = re.compile(r"\bliabilit", re.I)


def parent_side(category: Optional[str], section: str) -> str:
    """"A" or "L" when a line with no known section sits under a parent heading about assets or
    liabilities (e.g. "Increase/(decrease) of other liabilities"); "" otherwise. Keeps same-named lines
    under an assets heading and a liabilities heading apart."""
    if section != "OTHER" or not category:
        return ""
    text = re.sub(r"\s-\s.*$", "", str(category))          # drop the " - <entity>" suffix
    a, l = bool(_SIDE_ASSET_RE.search(text)), bool(_SIDE_LIAB_RE.search(text))
    return "A" if a and not l else "L" if l and not a else ""


def sections_contrast(a: str, b: str) -> bool:
    return frozenset((a, b)) in SECTION_CONTRASTS


# ---------------------------------------------------------------------------
# Label key
# ---------------------------------------------------------------------------
_LABEL_SYNONYMS = [
    (r"\bloans?\b", "borrowings"),
    (r"\bsales\b", "revenue"),
    (r"\bturnover\b", "revenue"),
    (r"\bobligation\b", "obligations"),
    (r"\bincome[\s-]tax\b", "income tax"),
    (r"\bshare\b", "shares"),
    (r"\b(net\s+assets?\s+value|nav)\s+per\s+shares?\b|\bnavps\b", "navps"),
    (r"\b(net\s+operating\s+)?cash\s+flows?\s+per\s+shares?\b|\bnocfps\b", "nocfps"),
    (r"\bearnings?\s+per\s+shares?\b|\beps\b", "eps"),
    (r"\band\b|&", " "),
    (r"\bthe\b", " "),
]


_LEADING_NOTE_RE = re.compile(r"^\s*(?:note\s*)?\d{1,2}(?:\.\d+)*[a-z]?[.)]?\s+(?!(?:months?|years?|days?)\b|%|percent)")
_BRACKET_ALT_RE = re.compile(r"\s*/\s*\(\s*(?:loss(?:es)?|expenses?|deficit|gain)\s*\)")
_BRITISH_RE = re.compile(r"\b([a-z]{3,})iz(ation|ations|ed|e|es|ing)\b")
_REPEATED_WORD_RE = re.compile(r"\b([a-z0-9]+)(?: \1\b)+")
_NO_SINGULAR = ("ss", "us", "is")      # business, status, analysis: not plurals


_KEEP_WORDS = {"navps", "nocfps", "eps", "pps"}      # abbreviations that merely end in "s"


def _singular(word: str) -> str:
    if word in _KEEP_WORDS:
        return word
    if len(word) > 4 and word.endswith("ies"):
        return word[:-3] + "y"                                    # liabilities -> liability, currencies -> currency
    return word[:-1] if len(word) > 3 and word.endswith("s") and not word.endswith(_NO_SINGULAR) else word


@functools.lru_cache(maxsize=200_000)      # pure, and called hundreds of thousands of times per results request
def label_key(field: Optional[str]) -> str:
    """Normalised line-item name: case, punctuation, note references, scope words and common
    synonyms removed, so the same printed line matches across reports.

    Also folded (the same line is printed these ways in different reports): a leading note number
    ("31. Other comprehensive income"), "(s)" and plurals, British / American spelling (-ise / -ize),
    a repeated word ("eps eps") and a bracketed alternative ("Profit/(Loss)"). The word "total" is kept:
    "X" and "X total" are often a part and its subtotal, so they are matched by `core_key`, not merged here."""
    s = (field or "").strip().lower()
    s = _BRACKET_ALT_RE.sub(" ", s)
    s = re.sub(r"\(\s*s\s*\)", "s", s)
    s = _LEADING_NOTE_RE.sub("", s)
    s = normalize_field_name(s)
    s = re.sub(r"\bnote\s*\d+(\.\d+)*\b", " ", s)
    s = _LEADING_NOTE_RE.sub("", s)
    s = re.sub(r"^\s*(consolidated|standalone|separate|the\s+company'?s?)\s+", "", s)
    s = _BRITISH_RE.sub(lambda m: f"{m.group(1)}is{m.group(2)}", s)
    for pat, rep in _LABEL_SYNONYMS:
        s = re.sub(pat, rep, s)
    s = re.sub(r"[^a-z0-9 ]", " ", s)
    s = " ".join(_singular(t) for t in s.split())
    s = _REPEATED_WORD_RE.sub(r"\1", s)
    return re.sub(r"\s+", " ", s).strip()


def core_key(label: str) -> str:
    """A label key without the word "total": "deposits and other accounts" == "deposits and other accounts total"."""
    return " ".join(t for t in label.split() if t != "total")


def stem_tokens(text: str) -> set:
    return {t[:-1] if len(t) > 3 and t.endswith("s") else t for t in re.findall(r"[a-z0-9]+", (text or "").lower())
            if t not in {"of", "and", "the", "for", "to", "in", "on", "from", "at", "by"}}


def name_similarity(a: str, b: str) -> float:
    """Jaccard of stemmed tokens (0..1)."""
    ta, tb = stem_tokens(a), stem_tokens(b)
    return len(ta & tb) / len(ta | tb) if ta and tb else 0.0


# ---------------------------------------------------------------------------
# Period length of a column (18-month / 6-month transition reports)
# ---------------------------------------------------------------------------
_MONTHS_RE = re.compile(r"\b(\d{1,2}|six|nine|three|twelve|eighteen)\s*[-\s]?months?\b", re.I)
_WORD_NUM = {"three": 3, "six": 6, "nine": 9, "twelve": 12, "eighteen": 18}


def period_months(header: str) -> Optional[int]:
    m = _MONTHS_RE.search(header or "")
    if not m:
        return None
    g = m.group(1).lower()
    return _WORD_NUM.get(g) or (int(g) if g.isdigit() else None)


def column_headers(markdown: str) -> Dict[Tuple[int, int], str]:
    """{(table_num, column): header text} for every table on a page."""
    out = {}
    for t in parse_markdown_tables(markdown or ""):
        for line in t["header_lines"]:
            for c, cell in enumerate(line, start=1):
                if cell:
                    out[(t["num"], c)] = (out.get((t["num"], c), "") + " " + cell).strip()
    return out


def report_end_year(fiscal_year: str) -> int:
    return extract_year_int(fiscal_year) or 0


# ---------------------------------------------------------------------------
# Value agreement (per value kind)
# ---------------------------------------------------------------------------
def agree(a: Optional[float], b: Optional[float], kind: str, mult_a: float = 1.0, mult_b: float = 1.0) -> bool:
    """True when two printed values are the same number for this value kind."""
    if a is None or b is None:
        return False
    diff, big = abs(a - b), max(abs(a), abs(b))
    if kind == CURRENCY:
        # one printed unit of the coarser scale (Taka vs Taka '000), plus float noise
        return diff <= max(max(mult_a or 1.0, mult_b or 1.0), 1e-9 * big)
    if kind == COUNT:
        return diff <= 0.5 or diff <= 1e-6 * big
    if kind == PERCENT:
        if diff <= 0.011:
            return True
        # 15.3 vs 0.153 (printed as a percent vs a fraction)
        return abs(a - b * 100) <= 0.011 or abs(a * 100 - b) <= 0.011
    # PER_SHARE, RATIO
    return diff <= 0.011 or (big > 0 and diff / big <= 0.005)


# How a later printing of a figure differs from the first one printed. The type of every restatement:
RESTATED_IDENTICAL = "RESTATED_IDENTICAL"   # printed again with the same figure
RESTATED_MICRO = "RESTATED_MICRO"           # a very small difference: rounding (agree() for the value kind)
RESTATED_LOW = "RESTATED_LOW"               # up to 25 % of the first figure
RESTATED_MID = "RESTATED_MID"               # more than 25 % and up to 100 %
RESTATED_HIGH = "RESTATED_HIGH"             # more than 100 % (also: the first figure was 0 and the new one is not)
USER_DEFINED = "USER_DEFINED"               # the cell's current figure was chosen by a user
RESTATEMENT_LOW_MAX = 25.0
RESTATEMENT_MID_MAX = 100.0


def restatement_type(first: float, later: float, kind: str, mult_first: float = 1.0,
                     mult_later: float = 1.0) -> Tuple[str, float, Optional[float]]:
    """(type, difference, percent change) of a later printing against the first one: difference = later - first,
    percent change = difference / |first| x 100 (None when first is 0). The type is RESTATED_IDENTICAL for the same
    figure, RESTATED_MICRO when the two agree to rounding, else LOW (up to 25 %), MID (up to 100 %) or HIGH (more than 100 %;
    a first figure of 0 and a later one that is not 0 is HIGH)."""
    diff = later - first
    pct = None if not first else diff / abs(first) * 100.0
    if abs(diff) <= 1e-4:
        return RESTATED_IDENTICAL, diff, pct
    if agree(first, later, kind, mult_first, mult_later):
        return RESTATED_MICRO, diff, pct
    if pct is None or abs(pct) > RESTATEMENT_MID_MAX:
        return RESTATED_HIGH, diff, pct
    return (RESTATED_LOW if abs(pct) <= RESTATEMENT_LOW_MAX else RESTATED_MID), diff, pct


def informative_weight(value: float, kind: str, mult: float = 1.0) -> float:
    """How much an exact match on this value proves (small or round numbers match by chance)."""
    v = abs(value or 0.0)
    if v == 0:
        return 0.0
    if kind == CURRENCY:
        steps = v / max(mult or 1.0, 1.0)
        return 1.0 if steps >= 1000 else 0.3
    if kind == COUNT:
        return 1.0 if v >= 10000 else 0.3
    if kind == PER_SHARE:
        return 0.4
    return 0.3 if kind == RATIO else 0.4


AGREE, DISAGREE, NONE = "AGREE", "DISAGREE", "NONE"


def compare_values(vals_a: Dict[int, Tuple[float, float]], vals_b: Dict[int, Tuple[float, float]],
                   kind: str) -> Dict[str, Any]:
    """Compare two year->(value, multiplier) maps on their overlapping years.

    Returns {"class": AGREE|DISAGREE|NONE, "overlap": [...], "weight": float}."""
    overlap, weight, mismatch_weight = [], 0.0, 0.0
    for y in sorted(set(vals_a) & set(vals_b)):
        (va, ma), (vb, mb) = vals_a[y], vals_b[y]
        match = agree(va, vb, kind, ma, mb)
        sign_flip = not match and agree(va, -vb, kind, ma, mb) and va != 0
        w = informative_weight(va if va else vb, kind, max(ma or 1, mb or 1))
        if match:
            weight += w
        elif sign_flip:
            weight += 0.8 * w
        else:
            mismatch_weight = max(mismatch_weight, w)
        overlap.append({"year": y, "a": va, "b": vb, "match": match, "sign_flip": sign_flip, "w": round(w, 2)})
    if mismatch_weight >= 1.0 or (overlap and weight == 0 and mismatch_weight > 0):
        cls = DISAGREE
    elif weight >= 1.0 and mismatch_weight < 1.0:
        cls = AGREE
    else:
        cls = NONE
    return {"class": cls, "overlap": overlap, "weight": round(weight, 2)}
