from __future__ import annotations

"""Analytics resource — dashboard metrics via the Nexure API."""

from typing import Any

from nexure.models import (
    AnalyticsComparisons,
    AnalyticsDrift,
    AnalyticsOverrides,
    AnalyticsOverview,
    VisibilityScore,
)
from nexure.resources._base import BaseResource


class AnalyticsResource(BaseResource):
    """nx.analytics — decision analytics."""

    def overview(self, *, days: int = 30, decision_type: str | None = None) -> AnalyticsOverview:
        """Get decision overview metrics.

        Args:
            days: Lookback period (all metrics scoped to this window).
            decision_type: Filter by decision type (e.g. "credit_underwriting").
        """
        params: dict[str, Any] = {"days": days}
        if decision_type is not None:
            params["decision_type"] = decision_type
        data = self._get("/analytics/overview", params=params)
        return AnalyticsOverview(**data)

    def override_rates(
        self, *, lookback_days: int = 30, decision_type: str | None = None
    ) -> AnalyticsOverrides:
        """Get per-reviewer override rates with anomaly detection.

        Args:
            lookback_days: Number of days to look back (default 30).
            decision_type: Filter by decision type.
        """
        params: dict[str, Any] = {"lookback_days": lookback_days}
        if decision_type is not None:
            params["decision_type"] = decision_type
        data = self._get("/analytics/overrides", params=params)
        return AnalyticsOverrides(**data)

    def drift(self, *, weeks: int = 12, decision_type: str | None = None) -> AnalyticsDrift:
        """Get week-over-week decision pattern drift.

        Args:
            weeks: Number of weeks to analyze.
            decision_type: Filter by decision type.
        """
        params: dict[str, Any] = {"weeks": weeks}
        if decision_type is not None:
            params["decision_type"] = decision_type
        data = self._get("/analytics/drift", params=params)
        return AnalyticsDrift(**data)

    def comparisons(
        self, *, limit: int = 20, min_similarity: float = 0.70, decision_type: str | None = None
    ) -> AnalyticsComparisons:
        """Find inconsistent decision pairs.

        Args:
            limit: Maximum number of pairs to return.
            min_similarity: Minimum similarity score (0.0-1.0, default 0.70).
            decision_type: Filter by decision type.
        """
        params: dict[str, Any] = {"limit": limit, "min_similarity": min_similarity}
        if decision_type is not None:
            params["decision_type"] = decision_type
        data = self._get("/analytics/comparisons", params=params)
        return AnalyticsComparisons(**data)

    def visibility_score(self, *, decision_type: str | None = None) -> VisibilityScore:
        """Get Decision Visibility Score (composite 0-100 metric).

        Args:
            decision_type: Filter by decision type.
        """
        params: dict[str, Any] = {}
        if decision_type is not None:
            params["decision_type"] = decision_type
        data = self._get("/analytics/visibility-score", params=params or None)
        return VisibilityScore(**data)
