"""Schemas for the Regulatory Optimization Engine.

These DTOs are *derived views* (computed-not-stored): nothing here is persisted; every value
is recomputed from the input each call. No new meta-model class, no graph (freeze v1.0).
Python 3.9 compatible (no `|` unions).
"""

from __future__ import annotations

from typing import List

from pydantic import BaseModel, Field


class RankedMeasure(BaseModel):
    """One measure (a capability to implement) ranked by its regulatory leverage."""

    capability_id: str
    covers: List[str] = Field(default_factory=list)        # the in-scope requirements it satisfies
    leverage: int = 0                                       # = len(covers): how many it closes at once
    cumulative_requirements: int = 0                        # running total of requirements closed (ranked order)
    cumulative_coverage: float = 0.0                        # cumulative_requirements / total_requirements (0..1)


class OptimizationPlan(BaseModel):
    """Measures ranked by regulatory leverage — greatest regulatory effect first.

    `total_requirements` counts the IDENTIFIED requirements in scope (the known delta from the
    patterns), NOT a company's total legal duties. The percentages are exact count ratios over
    this identified set — never a compliance verdict (Welt-1 discipline).
    """

    in_scope_requirements: List[str] = Field(default_factory=list)   # the distinct requirement keys counted
    total_measures: int = 0                                          # number of distinct measures (delta capabilities)
    total_requirements: int = 0                                      # Sum of leverage = identified requirements closable
    ranked_measures: List[RankedMeasure] = Field(default_factory=list)
    headline: str = ""                                               # "N identifizierte Anforderungen -> M Massnahmen ..."


class BudgetPlan(BaseModel):
    """The budget answer: with a budget of K measures, which K and how much do they close?"""

    budget: int = 0
    selected_capabilities: List[str] = Field(default_factory=list)
    requirements_closed: int = 0
    total_requirements: int = 0
    coverage_ratio: float = 0.0                                      # requirements_closed / total_requirements (0..1)
    headline: str = ""
