"""Synthetic credit data fixtures for testing validation and credit modules.

Provides:
- SAMPLE_LOANS: 10 synthetic loan records as dicts
- SAMPLE_PORTFOLIO: portfolio-level aggregation dict
- EXPECTED_LOSS_SCENARIOS: 3 stress-test scenarios (base, adverse, severely_adverse)
"""

from __future__ import annotations

SAMPLE_LOANS: list[dict] = [
    {
        "loan_id": "LN-2024-00001",
        "loan_amount": 250_000.00,
        "interest_rate": 0.045,
        "credit_score": 720,
        "debt_to_income": 0.35,
        "loan_to_value": 0.80,
        "term_months": 360,
        "origination_date": "2024-01-15",
        "default_flag": 0,
        "delinquency_days": 0,
        "loss_given_default": 0.40,
        "probability_of_default": 0.02,
        "exposure_at_default": 250_000.00,
    },
    {
        "loan_id": "LN-2024-00002",
        "loan_amount": 180_000.00,
        "interest_rate": 0.055,
        "credit_score": 680,
        "debt_to_income": 0.42,
        "loan_to_value": 0.90,
        "term_months": 360,
        "origination_date": "2024-02-01",
        "default_flag": 0,
        "delinquency_days": 15,
        "loss_given_default": 0.45,
        "probability_of_default": 0.05,
        "exposure_at_default": 180_000.00,
    },
    {
        "loan_id": "LN-2024-00003",
        "loan_amount": 500_000.00,
        "interest_rate": 0.038,
        "credit_score": 780,
        "debt_to_income": 0.25,
        "loan_to_value": 0.65,
        "term_months": 360,
        "origination_date": "2024-03-10",
        "default_flag": 0,
        "delinquency_days": 0,
        "loss_given_default": 0.35,
        "probability_of_default": 0.008,
        "exposure_at_default": 500_000.00,
    },
    {
        "loan_id": "LN-2024-00004",
        "loan_amount": 75_000.00,
        "interest_rate": 0.072,
        "credit_score": 620,
        "debt_to_income": 0.48,
        "loan_to_value": 0.95,
        "term_months": 180,
        "origination_date": "2024-01-20",
        "default_flag": 1,
        "delinquency_days": 90,
        "loss_given_default": 0.55,
        "probability_of_default": 0.15,
        "exposure_at_default": 75_000.00,
    },
    {
        "loan_id": "LN-2024-00005",
        "loan_amount": 320_000.00,
        "interest_rate": 0.042,
        "credit_score": 740,
        "debt_to_income": 0.30,
        "loan_to_value": 0.75,
        "term_months": 360,
        "origination_date": "2024-04-05",
        "default_flag": 0,
        "delinquency_days": 0,
        "loss_given_default": 0.38,
        "probability_of_default": 0.015,
        "exposure_at_default": 320_000.00,
    },
    {
        "loan_id": "LN-2024-00006",
        "loan_amount": 150_000.00,
        "interest_rate": 0.062,
        "credit_score": 660,
        "debt_to_income": 0.45,
        "loan_to_value": 0.88,
        "term_months": 240,
        "origination_date": "2024-02-28",
        "default_flag": 0,
        "delinquency_days": 30,
        "loss_given_default": 0.48,
        "probability_of_default": 0.08,
        "exposure_at_default": 150_000.00,
    },
    {
        "loan_id": "LN-2024-00007",
        "loan_amount": 425_000.00,
        "interest_rate": 0.040,
        "credit_score": 760,
        "debt_to_income": 0.28,
        "loan_to_value": 0.70,
        "term_months": 360,
        "origination_date": "2024-05-12",
        "default_flag": 0,
        "delinquency_days": 0,
        "loss_given_default": 0.36,
        "probability_of_default": 0.012,
        "exposure_at_default": 425_000.00,
    },
    {
        "loan_id": "LN-2024-00008",
        "loan_amount": 95_000.00,
        "interest_rate": 0.068,
        "credit_score": 640,
        "debt_to_income": 0.50,
        "loan_to_value": 0.92,
        "term_months": 180,
        "origination_date": "2024-03-22",
        "default_flag": 1,
        "delinquency_days": 120,
        "loss_given_default": 0.52,
        "probability_of_default": 0.12,
        "exposure_at_default": 95_000.00,
    },
    {
        "loan_id": "LN-2024-00009",
        "loan_amount": 600_000.00,
        "interest_rate": 0.035,
        "credit_score": 800,
        "debt_to_income": 0.20,
        "loan_to_value": 0.60,
        "term_months": 360,
        "origination_date": "2024-06-01",
        "default_flag": 0,
        "delinquency_days": 0,
        "loss_given_default": 0.30,
        "probability_of_default": 0.005,
        "exposure_at_default": 600_000.00,
    },
    {
        "loan_id": "LN-2024-00010",
        "loan_amount": 210_000.00,
        "interest_rate": 0.050,
        "credit_score": 700,
        "debt_to_income": 0.38,
        "loan_to_value": 0.82,
        "term_months": 360,
        "origination_date": "2024-04-18",
        "default_flag": 0,
        "delinquency_days": 0,
        "loss_given_default": 0.42,
        "probability_of_default": 0.03,
        "exposure_at_default": 210_000.00,
    },
]

SAMPLE_PORTFOLIO: dict = {
    "portfolio_id": "PF-2024-001",
    "total_loans": len(SAMPLE_LOANS),
    "total_exposure": sum(loan["exposure_at_default"] for loan in SAMPLE_LOANS),
    "weighted_avg_credit_score": sum(
        loan["credit_score"] * loan["loan_amount"] for loan in SAMPLE_LOANS
    )
    / sum(loan["loan_amount"] for loan in SAMPLE_LOANS),
    "weighted_avg_ltv": sum(loan["loan_to_value"] * loan["loan_amount"] for loan in SAMPLE_LOANS)
    / sum(loan["loan_amount"] for loan in SAMPLE_LOANS),
    "default_rate": sum(1 for loan in SAMPLE_LOANS if loan["default_flag"] == 1)
    / len(SAMPLE_LOANS),
    "total_expected_loss": sum(
        loan["probability_of_default"] * loan["loss_given_default"] * loan["exposure_at_default"]
        for loan in SAMPLE_LOANS
    ),
    "concentration_top3": sum(
        sorted((loan["loan_amount"] for loan in SAMPLE_LOANS), reverse=True)[:3]
    )
    / sum(loan["loan_amount"] for loan in SAMPLE_LOANS),
}

EXPECTED_LOSS_SCENARIOS: list[dict] = [
    {
        "scenario_name": "base",
        "description": "Baseline economic conditions — no stress applied",
        "pd_multiplier": 1.0,
        "lgd_multiplier": 1.0,
        "expected_loss": SAMPLE_PORTFOLIO["total_expected_loss"],
    },
    {
        "scenario_name": "adverse",
        "description": "Moderate recession — elevated defaults and loss severity",
        "pd_multiplier": 2.5,
        "lgd_multiplier": 1.3,
        "expected_loss": sum(
            loan["probability_of_default"]
            * 2.5
            * loan["loss_given_default"]
            * 1.3
            * loan["exposure_at_default"]
            for loan in SAMPLE_LOANS
        ),
    },
    {
        "scenario_name": "severely_adverse",
        "description": "Severe recession — sharp increase in defaults and losses",
        "pd_multiplier": 5.0,
        "lgd_multiplier": 1.6,
        "expected_loss": sum(
            loan["probability_of_default"]
            * 5.0
            * loan["loss_given_default"]
            * 1.6
            * loan["exposure_at_default"]
            for loan in SAMPLE_LOANS
        ),
    },
]
