"""OpenAPI 3.1 spec generator for the Orxaq REST API. Stdlib-only."""

from __future__ import annotations

_SEC: list[dict[str, list[str]]] = [{"ApiKeyAuth": []}, {"BearerAuth": []}]


def generate_spec() -> dict:
    """Return a complete OpenAPI 3.1.0 specification dict."""
    try:
        import orxaq

        version = getattr(orxaq, "__version__", "1.0.0")
    except Exception:
        version = "1.0.0"

    def _post(op_id, summary, req_props):
        return {
            "post": {
                "operationId": op_id,
                "summary": summary,
                "security": _SEC,
                "requestBody": {
                    "required": True,
                    "content": {
                        "application/json": {"schema": {"type": "object", "properties": req_props}}
                    },
                },
                "responses": {"200": {"description": "OK"}, "401": {"description": "Unauthorized"}},
            }
        }

    return {
        "openapi": "3.1.0",
        "info": {
            "title": "Orxaq Credit Risk API",
            "version": version,
            "description": "Production REST API for Orxaq credit risk analytics.",
            "contact": {"name": "Orxaq Engineering", "email": "api@orxaq.com"},
            "license": {"name": "Proprietary"},
        },
        "paths": {
            "/api/v1/health": {
                "get": {
                    "operationId": "health_check",
                    "summary": "Health/readiness probe",
                    "security": [],
                    "responses": {"200": {"description": "OK"}},
                }
            },
            "/api/v1/ontology": {
                "get": {
                    "operationId": "get_ontology",
                    "summary": "Credit ontology entities and edges",
                    "security": _SEC,
                    "responses": {
                        "200": {"description": "OK"},
                        "401": {"description": "Unauthorized"},
                    },
                }
            },
            "/api/v1/ecl/compute": _post(
                "ecl_compute",
                "Compute CECL ECL",
                {
                    "portfolio_size": {"type": "integer", "default": 200},
                    "scenario": {"type": "string", "default": "baseline"},
                    "horizon": {"default": "dynamic"},
                    "seed": {"type": "integer", "default": 42},
                },
            ),
            "/api/v1/filing/generate": _post(
                "filing_generate",
                "Generate CECL/CCAR filing package",
                {
                    "institution_id": {"type": "string", "default": "BANK001"},
                    "period": {"type": "string", "default": "2026Q1"},
                    "horizon_quarters": {"type": "integer", "default": 9},
                    "n_samples": {"type": "integer", "default": 100},
                },
            ),
            "/api/v1/stress-test/run": _post(
                "stress_test_run",
                "Run CCAR/DFAST stress test",
                {
                    "scenario": {"type": "string", "default": "adverse"},
                    "horizon": {"type": "integer", "default": 9},
                    "portfolio_size": {"type": "integer", "default": 100},
                },
            ),
            "/api/v1/scenarios/compare": _post(
                "scenarios_compare",
                "Compare stress scenarios",
                {
                    "scenarios": {
                        "type": "array",
                        "items": {"type": "string"},
                        "default": ["baseline", "adverse", "severely_adverse"],
                    },
                    "cet1_ratio": {"type": "number", "default": 0.12},
                },
            ),
            "/api/v1/regulatory/export": _post(
                "regulatory_export",
                "Export regulatory artifact",
                {
                    "framework": {"type": "string", "enum": ["cecl", "ccar"], "default": "cecl"},
                    "format": {"type": "string", "enum": ["json", "markdown"], "default": "json"},
                    "institution_id": {"type": "string"},
                    "period": {"type": "string"},
                },
            ),
        },
        "components": {
            "securitySchemes": {
                "ApiKeyAuth": {"type": "apiKey", "in": "header", "name": "X-API-Key"},
                "BearerAuth": {"type": "http", "scheme": "bearer"},
            }
        },
    }


__all__ = ["generate_spec"]
