"""Fee estimation and management for NEXUS SDK.

Provides access to Monero transaction fee estimates and client fee configuration.
"""

from __future__ import annotations

from typing import TYPE_CHECKING, Any

from .types import (
    AllFeeEstimates,
    ClientFeeConfig,
    ClientFeeEstimate,
    DaemonHealthSummary,
    FeeEstimate,
)

if TYPE_CHECKING:
    from .client import NexusClient


class FeeManager:
    """Manages fee estimation and configuration.

    Accessed via ``client.fees``.

    Example::

        async with NexusClient(api_key="nxs_...") as client:
            estimate = await client.fees.estimate(priority="normal")
            print(f"Fee for 2-output TX: {estimate.fee_xmr} XMR")
    """

    def __init__(self, client: NexusClient) -> None:
        self._client = client

    async def estimate(
        self,
        *,
        priority: str | None = None,
        tx_size: int | None = None,
    ) -> FeeEstimate:
        """Get fee estimate for a Monero transaction.

        Args:
            priority: Fee priority (unimportant, normal, elevated, priority).
            tx_size: Custom transaction size in bytes.

        Returns:
            Fee estimate with per-byte rate and total estimates.
        """
        params: dict[str, Any] = {}
        if priority is not None:
            params["priority"] = priority
        if tx_size is not None:
            params["tx_size"] = tx_size
        response = await self._client._request(
            "GET", "/api/v1/fees/estimate", params=params
        )
        return FeeEstimate.model_validate(response)

    async def estimate_all(self) -> AllFeeEstimates:
        """Get fee estimates for all priority levels.

        Returns:
            Fee estimates for unimportant, normal, elevated, and priority.
        """
        response = await self._client._request("GET", "/api/v1/fees/all")
        return AllFeeEstimates.model_validate(response)

    async def daemon_health(self) -> DaemonHealthSummary:
        """Get health status of daemon endpoints.

        Returns:
            Summary of daemon pool health including per-endpoint status.
        """
        response = await self._client._request("GET", "/api/v1/daemon/health")
        return DaemonHealthSummary.model_validate(response)

    async def get_client_fees(self) -> ClientFeeConfig:
        """Get current fee configuration for the authenticated client.

        Returns:
            Client fee configuration (basis points, source).
        """
        response = await self._client._request("GET", "/api/v1/client/fees")
        return ClientFeeConfig.model_validate(response)

    async def estimate_client_fee(
        self,
        amount_atomic: int,
        *,
        is_refund: bool = False,
    ) -> ClientFeeEstimate:
        """Estimate platform fee for a given amount.

        Args:
            amount_atomic: Transaction amount in piconero.
            is_refund: Whether this is a refund (may have different fee).

        Returns:
            Fee breakdown including net amount after deduction.
        """
        params: dict[str, Any] = {"amount_atomic": amount_atomic}
        if is_refund:
            params["is_refund"] = "true"
        response = await self._client._request(
            "GET", "/api/v1/client/fees/estimate", params=params
        )
        return ClientFeeEstimate.model_validate(response)
