"""Frozen mortgage calculator IR shipped by Feature Pack calculator-mortgage."""

from src.domain.calculation.ir import CalculationIR

MORTGAGE_CALCULATOR_ID = "mortgage_calculator"

MORTGAGE_IR = CalculationIR.model_validate(
    {
        "id": MORTGAGE_CALCULATOR_ID,
        "execution": "local",
        "authors": "feature_pack",
        "inputs": [
            {"key": "principal", "type": "money", "label": "Loan amount"},
            {"key": "annual_rate", "type": "percent", "label": "Annual interest rate"},
            {"key": "years", "type": "number", "label": "Term in years"},
        ],
        "fields": [
            {
                "key": "months",
                "type": "number",
                "expr": {
                    "op": "mul",
                    "args": [
                        {"op": "input", "key": "years"},
                        {"op": "lit", "value": 12},
                    ],
                },
            },
            {
                "key": "monthly_rate",
                "type": "percent",
                "expr": {
                    "op": "div",
                    "args": [{"op": "input", "key": "annual_rate"}, {"op": "lit", "value": 12}],
                },
            },
            {
                "key": "growth",
                "expr": {
                    "op": "pow",
                    "args": [
                        {
                            "op": "add",
                            "args": [
                                {"op": "lit", "value": 1},
                                {"op": "field", "key": "monthly_rate"},
                            ],
                        },
                        {"op": "field", "key": "months"},
                    ],
                },
            },
            {
                "key": "payment",
                "type": "money",
                "expr": {
                    "op": "if",
                    "cond": {
                        "op": "eq",
                        "args": [{"op": "field", "key": "monthly_rate"}, {"op": "lit", "value": 0}],
                    },
                    "then": {
                        "op": "div",
                        "args": [
                            {"op": "input", "key": "principal"},
                            {"op": "field", "key": "months"},
                        ],
                    },
                    "else": {
                        "op": "div",
                        "args": [
                            {
                                "op": "mul",
                                "args": [
                                    {
                                        "op": "mul",
                                        "args": [
                                            {"op": "input", "key": "principal"},
                                            {"op": "field", "key": "monthly_rate"},
                                        ],
                                    },
                                    {"op": "field", "key": "growth"},
                                ],
                            },
                            {
                                "op": "sub",
                                "args": [
                                    {"op": "field", "key": "growth"},
                                    {"op": "lit", "value": 1},
                                ],
                            },
                        ],
                    },
                },
            },
            {
                "key": "total_paid",
                "type": "money",
                "expr": {
                    "op": "mul",
                    "args": [{"op": "field", "key": "payment"}, {"op": "field", "key": "months"}],
                },
            },
            {
                "key": "total_interest",
                "type": "money",
                "expr": {
                    "op": "sub",
                    "args": [
                        {"op": "field", "key": "total_paid"},
                        {"op": "input", "key": "principal"},
                    ],
                },
            },
            {
                "key": "summary_rows",
                "expr": {
                    "op": "list",
                    "items": [
                        {
                            "op": "record",
                            "fields": {
                                "label": {"op": "lit", "value": "Monthly payment", "type": "text"},
                                "amount": {"op": "field", "key": "payment"},
                            },
                        },
                        {
                            "op": "record",
                            "fields": {
                                "label": {"op": "lit", "value": "Total paid", "type": "text"},
                                "amount": {"op": "field", "key": "total_paid"},
                            },
                        },
                        {
                            "op": "record",
                            "fields": {
                                "label": {"op": "lit", "value": "Total interest", "type": "text"},
                                "amount": {"op": "field", "key": "total_interest"},
                            },
                        },
                    ],
                },
            },
        ],
        "outputs": [
            {"key": "monthly_payment", "kind": "scalar", "from_field": "payment"},
            {"key": "total_paid", "kind": "scalar", "from_field": "total_paid"},
            {"key": "total_interest", "kind": "scalar", "from_field": "total_interest"},
            {
                "key": "summary",
                "kind": "dataset",
                "from_field": "summary_rows",
                "shape": "series",
            },
        ],
    }
)
