"""Supported chronological origins, faculty balancing, and earlier-only priors.

Origins are single semesters in the target term of the three-semester academic
calendar. A cell is eligible only when both outcomes have support. Faculty
coverage is an explicit fraction of the full selected faculty schema; the
production default requires every faculty. No target outcome enters selection
or evidence construction.
"""
from __future__ import annotations

import numpy as np
import pandas as pd

from data import Table


SUPPORT_COLUMNS = (
    'semester', 'faculty', 'rows', 'positives', 'negatives', 'origin_rows', 'train_rows',
    'train_faculty_rows', 'same_term', 'eligible', 'reason', 'origin_eligible', 'selected',
)


class UnsupportedOriginsError(ValueError):
    """No supported origin exists; ``audit`` retains every inspected cell.

    Callers must print and, outside preflight, save the full audit rather than
    falling back to an unsupported semester or a different validation protocol.
    """
    def __init__(self, audit: pd.DataFrame):
        self.audit = audit.copy()
        super().__init__('No supported earlier same-term origins; inspect the full support audit.')


def _integer(value, name: str, minimum=None) -> int:
    try:
        result = int(value)
    except (TypeError, ValueError, OverflowError) as exc:
        raise ValueError(f'{name} must be a finite integer') from exc
    if result != value or (minimum is not None and result < minimum):
        bound = f' >= {minimum}' if minimum is not None else ''
        raise ValueError(f'{name} must be a finite integer{bound}')
    return result


def faculty_weights(table: Table) -> np.ndarray:
    """Mean-one inverse faculty-frequency weights, with no class balancing.

    An absent schema faculty has no rows to weight. Each observed faculty has
    equal total mass, and the average row weight is one.
    """
    if not len(table):
        return np.empty(0, dtype=np.float32)
    counts = np.bincount(table.faculty, minlength=len(table.faculty_names))
    present = int(np.count_nonzero(counts))
    return (len(table) / (present * counts[table.faculty])).astype(np.float32)


def choose_origins(table: Table, target_semester, n_origins=2, min_rows=2000,
                   min_faculty_rows=30, min_positive=5, min_negative=20,
                   min_train_rows=5000, coverage=1.0):
    """Return the latest supported same-term origins and an exhaustive audit.

    ``eligible`` describes a faculty's validation outcome support independently
    of term and global training support. ``min_faculty_rows`` applies to both
    each validation cell and that faculty's earlier training rows. An
    ``origin_eligible`` semester also needs ``min_rows`` across its entire cohort,
    must precede target, match its term, cover the requested faculty fraction,
    have enough earlier training rows, and have enough earlier rows for *every*
    eligible validation faculty. Less than n_origins may be returned; zero is
    always an explicit UnsupportedOriginsError carrying a headered audit.
    """
    target = _integer(target_semester, 'target_semester')
    number = _integer(n_origins, 'n_origins', 1)
    row_min = _integer(min_rows, 'min_rows', 0)
    faculty_min = _integer(min_faculty_rows, 'min_faculty_rows', 0)
    positive_min = max(1, _integer(min_positive, 'min_positive', 0))
    negative_min = max(1, _integer(min_negative, 'min_negative', 0))
    train_min = _integer(min_train_rows, 'min_train_rows', 0)
    try:
        required_coverage = float(coverage)
    except (TypeError, ValueError, OverflowError) as exc:
        raise ValueError('coverage must be finite and in (0, 1]') from exc
    if not np.isfinite(required_coverage) or not 0 < required_coverage <= 1:
        raise ValueError('coverage must be finite and in (0, 1]')

    # Group once; production caches have millions of rows and many semesters.
    observed = pd.DataFrame({'semester': table.semester, 'faculty': table.faculty,
                             'positive': table.y.astype(np.int64)})
    counts = observed.groupby(['semester', 'faculty'], sort=True)['positive'].agg(['size', 'sum'])
    preceding = np.zeros(len(table.faculty_names), dtype=np.int64)
    records, eligible_origins = [], []
    for semester in np.unique(table.semester):
        semester = int(semester)
        same_term = semester % 3 == target % 3
        train_rows = int(preceding.sum())
        term_records = []
        for fi, faculty in enumerate(table.faculty_names):
            key = (semester, fi)
            if key in counts.index:
                row = counts.loc[key]
                rows, positives = int(row['size']), int(row['sum'])
            else:
                rows, positives = 0, 0
            negatives = rows - positives
            reasons = []
            if rows < faculty_min:
                reasons.append(f'faculty_rows<{faculty_min}')
            if positives < positive_min:
                reasons.append(f'positives<{positive_min}')
            if negatives < negative_min:
                reasons.append(f'negatives<{negative_min}')
            cell_eligible = not reasons
            if preceding[fi] < faculty_min:
                reasons.append(f'train_faculty_rows<{faculty_min}')
            term_records.append(dict(
                semester=semester, faculty=faculty, rows=rows, positives=positives,
                negatives=negatives, train_rows=train_rows,
                train_faculty_rows=int(preceding[fi]), same_term=bool(same_term),
                eligible=bool(cell_eligible), reason=reasons,
            ))
        eligible_cells = [r for r in term_records if r['eligible']]
        origin_rows = sum(r['rows'] for r in term_records)
        cohort_ok = origin_rows >= row_min
        coverage_ok = len(eligible_cells) / len(table.faculty_names) >= required_coverage
        training_ok = train_rows >= train_min
        faculty_training_ok = all(r['train_faculty_rows'] >= faculty_min for r in eligible_cells)
        origin_eligible = bool(semester < target and same_term and cohort_ok and coverage_ok and training_ok
                               and faculty_training_ok and eligible_cells)
        if origin_eligible:
            eligible_origins.append(semester)
        shared_reasons = []
        if semester >= target:
            shared_reasons.append('not_earlier_than_target')
        if not same_term:
            shared_reasons.append('not_same_term')
        if not cohort_ok:
            shared_reasons.append(f'origin_rows<{row_min}')
        if not coverage_ok:
            shared_reasons.append(f'faculty_coverage<{required_coverage:g}')
        if not training_ok:
            shared_reasons.append(f'train_rows<{train_min}')
        if not faculty_training_ok:
            shared_reasons.append('eligible_faculty_training_unsupported')
        for record in term_records:
            record['origin_rows'] = origin_rows
            record['reason'] = ';'.join(record['reason'] + shared_reasons) or 'supported'
            record['origin_eligible'] = origin_eligible
            record['selected'] = False
            records.append(record)
        preceding += np.asarray([r['rows'] for r in term_records], dtype=np.int64)
    audit = pd.DataFrame.from_records(records, columns=SUPPORT_COLUMNS)
    origins = eligible_origins[-number:]
    if not origins:
        raise UnsupportedOriginsError(audit)
    audit['selected'] = audit['semester'].isin(origins) & audit['eligible']
    return origins, audit


def origin_cells(audit: pd.DataFrame, origin) -> list[str]:
    """Eligible faculty names at an origin, preserving the audit's schema order."""
    semester = _integer(origin, 'origin')
    return audit.loc[(audit['semester'] == semester) & audit['eligible'], 'faculty'].drop_duplicates().tolist()


def prior_evidence(bank_table: Table, query_table: Table, embargo=0) -> np.ndarray:
    """Cumulative earlier-only faculty failure and performance means [N, 2].

    For each query, bank.semester must be strictly below query.semester-embargo.
    Performance is the observed grade divided by ten, or 1-y if grade is absent.
    Faculty defaults without earlier observations are failure=.1, performance=.65.
    Row order and query outcomes cannot alter these priors.
    """
    excluded = _integer(embargo, 'embargo', 0)
    if (bank_table.faculty_names != query_table.faculty_names
            or bank_table.feature_names != query_table.feature_names):
        raise ValueError('Prior bank/query feature and faculty schemas must match')
    result = np.tile(np.asarray([.1, .65], dtype=np.float32), (len(query_table), 1))
    for fi in range(len(bank_table.faculty_names)):
        bank_rows = np.flatnonzero(bank_table.faculty == fi)
        query_rows = np.flatnonzero(query_table.faculty == fi)
        if not len(bank_rows) or not len(query_rows):
            continue
        order = bank_rows[np.argsort(bank_table.semester[bank_rows], kind='stable')]
        semester = bank_table.semester[order]
        failure = bank_table.y[order].astype(np.float64)
        grade = bank_table.grade[order]
        performance = np.where(np.isfinite(grade), grade / 10., 1. - failure)
        failure_sum = np.concatenate([[0.], np.cumsum(failure, dtype=np.float64)])
        performance_sum = np.concatenate([[0.], np.cumsum(performance, dtype=np.float64)])
        # Python integer subtraction avoids overflowing a pathological ordinal.
        cutoff = np.asarray([int(value) - excluded for value in query_table.semester[query_rows]])
        count = np.searchsorted(semester, cutoff, side='left')
        known = count > 0
        supported_rows, denominator = query_rows[known], count[known]
        result[supported_rows, 0] = failure_sum[denominator] / denominator
        result[supported_rows, 1] = performance_sum[denominator] / denominator
    return result
