"""
Neural LAB - AI Solutions Platform
Billing Resource - Usage and subscription management.

Author: Anderson Henrique da Silva
Location: Minas Gerais, Brasil
Created: 2026-01-24
"""

from dataclasses import dataclass
from typing import Any

from ..base import DataclassMixin


@dataclass
class Usage(DataclassMixin):
    """Current usage information."""

    total_requests: int
    total_tokens: int
    total_cost: float
    included_requests: int | None
    included_tokens: int | None
    requests_percentage: float
    tokens_percentage: float
    period_start: str
    period_end: str
    days_remaining: int


@dataclass
class Subscription(DataclassMixin):
    """Subscription information."""

    id: str
    plan_name: str
    status: str
    billing_cycle: str
    current_period_start: str
    current_period_end: str


@dataclass
class Credits(DataclassMixin):
    """Credits balance."""

    current_balance: float
    credit_limit: float
    available: float


class BillingResource:
    """
    Billing resource for usage and subscription management.

    Usage:
        # Check usage
        usage = client.billing.get_usage()

        # Get subscription
        sub = client.billing.get_subscription()

        # Check credits
        credits = client.billing.get_credits()
    """

    def __init__(self, client):
        self._client = client

    def get_usage(self) -> Usage:
        """
        Get current usage for billing period.

        Returns:
            Usage information
        """
        response = self._client.get("/api/billing/client/usage")

        return Usage(
            total_requests=response.get("total_requests", 0),
            total_tokens=response.get("total_tokens", 0),
            total_cost=response.get("total_cost", 0),
            included_requests=response.get("included_requests"),
            included_tokens=response.get("included_tokens"),
            requests_percentage=response.get("requests_percentage", 0),
            tokens_percentage=response.get("tokens_percentage", 0),
            period_start=response.get("period_start", ""),
            period_end=response.get("period_end", ""),
            days_remaining=response.get("days_remaining", 0),
        )

    def get_subscription(self) -> Subscription:
        """
        Get current subscription.

        Returns:
            Subscription information
        """
        response = self._client.get("/api/billing/client/subscription")

        plan = response.get("plan", {})
        return Subscription(
            id=response.get("id", ""),
            plan_name=plan.get("name", "free"),
            status=response.get("status", "active"),
            billing_cycle=response.get("billing_cycle", "monthly"),
            current_period_start=response.get("current_period_start", ""),
            current_period_end=response.get("current_period_end", ""),
        )

    def get_credits(self) -> Credits:
        """
        Get current credits balance.

        Returns:
            Credits balance
        """
        response = self._client.get("/api/billing/client/credits")

        return Credits(
            current_balance=response.get("current_balance", 0),
            credit_limit=response.get("credit_limit", 0),
            available=response.get("available", 0),
        )

    def get_invoices(
        self,
        limit: int = 10,
        offset: int = 0,
    ) -> list[dict[str, Any]]:
        """
        Get invoices.

        Args:
            limit: Number of invoices to return
            offset: Offset for pagination

        Returns:
            List of invoices
        """
        response = self._client.get(
            "/api/billing/client/invoices",
            params={"limit": limit, "offset": offset},
        )

        return response.get("invoices", [])

    def get_me(self) -> dict[str, Any]:
        """
        Get current client info.

        Returns:
            Client information
        """
        return self._client.get("/api/billing/client/me")
