# retoor <retoor@molodetz.nl>

import logging

from devplacepy.database import get_correction_usage, get_modifier_usage
from devplacepy.services.manager import service_manager
from devplacepy.services.openai_gateway import quota as gateway_quota
from devplacepy.services.openai_gateway.analytics import user_spend_24h

logger = logging.getLogger(__name__)


def _usage_view(data: dict, include_cost: bool) -> dict:
    usage = {
        "calls": data["calls"],
        "prompt_tokens": data["prompt_tokens"],
        "completion_tokens": data["completion_tokens"],
        "total_tokens": data["total_tokens"],
        "avg_tokens": data["avg_tokens"],
        "avg_latency_ms": data["avg_upstream_latency_ms"],
        "avg_total_latency_ms": data["avg_total_latency_ms"],
        "avg_tokens_per_second": data["avg_tokens_per_second"],
        "total_time_s": round(data["upstream_latency_ms"] / 1000.0, 1),
        "last_used": data["updated_at"],
    }
    if include_cost:
        usage["cost_usd"] = round(data["cost_usd"], 6)
        usage["avg_cost_usd"] = round(data["avg_cost_usd"], 6)
    return usage


def _correction_usage(user_uid: str, include_cost: bool = False) -> dict:
    return _usage_view(get_correction_usage(user_uid), include_cost)


def _modifier_usage(user_uid: str, include_cost: bool = False) -> dict:
    return _usage_view(get_modifier_usage(user_uid), include_cost)


def _ai_quota(
    user_uid: str, include_cost: bool = False, is_admin: bool = False
) -> dict | None:
    devii = service_manager.get_service("devii")
    if devii is None:
        return None
    try:
        limit = devii.daily_limit_for("user", is_admin)
        turns = devii.hub().ledger.turns_24h("user", user_uid)
    except Exception:
        logger.exception("Failed to compute AI quota for %s", user_uid)
        return None
    gateway = user_spend_24h(user_uid)
    spent = gateway["spent_usd"]
    unlimited = limit <= 0
    used_pct = 0.0 if unlimited else round(min(100.0, spent / limit * 100), 1)
    quota = {
        "used_pct": used_pct,
        "unlimited": unlimited,
        "turns": turns,
        "requests": gateway["requests"],
        "last_used": gateway["last_used"],
    }
    if include_cost:
        quota["spent_usd"] = round(spent, 4)
        quota["limit_usd"] = round(limit, 2)
    gateway_svc = service_manager.get_service("openai")
    if gateway_svc is not None:
        try:
            owner_kind = "admin" if is_admin else "user"
            cfg = gateway_svc.effective_config()
            gw_limit, gw_scope, gw_rule = gateway_quota.resolve_for_owner(owner_kind, user_uid, cfg)
            gw_spent = gateway_quota.spent_24h(*gw_scope)
            gw_unlimited = gw_limit <= 0
            quota["gateway_unlimited"] = gw_unlimited
            quota["gateway_used_pct"] = (
                0.0 if gw_unlimited else round(min(100.0, gw_spent / gw_limit * 100), 1)
            )
            if include_cost:
                quota["gateway_spent_usd"] = round(gw_spent, 4)
                quota["gateway_limit_usd"] = round(gw_limit, 2)
                quota["gateway_pooled"] = bool(gw_rule and gw_rule.owner_id is None)
        except Exception:
            logger.exception("Failed to compute gateway-level AI quota for %s", user_uid)
    return quota
