"""Models for OxaPay Exchange API."""

from enum import Enum

from pydantic import BaseModel, Field

from .base import OrderDirection, PaginatedResponse, SortField
from .common import DataResponse, TrackIdMixin


class SwapType(str, Enum):
    """Swap type enum."""

    AUTO_CONVERT = "autoConvert"
    MANUAL_SWAP = "manualSwap"
    SWAP_BY_API = "swapByApi"


class SwapPair(BaseModel):
    """Model for swap pair information."""

    from_currency: str = Field(
        ...,
        validation_alias="fromCurrency",
        description="Source currency symbol",
    )
    to_currency: str = Field(
        ...,
        validation_alias="toCurrency",
        description="Target currency symbol",
    )
    min_amount: float = Field(
        ...,
        validation_alias="minAmount",
        description="Minimum conversion amount",
    )


class SwapPairsResponse(DataResponse[list[SwapPair]]):
    """Response model for swap pairs."""


class SwapRateRequest(BaseModel):
    """Request model for swap rate."""

    from_currency: str = Field(
        ...,
        description="Source currency symbol",
    )
    to_currency: str = Field(
        ...,
        description="Target currency symbol",
    )


class SwapRateData(BaseModel):
    """Data model for swap rate."""

    rate: float = Field(..., description="Swap rate")


class SwapRateResponse(DataResponse[SwapRateData]):
    """Response model for swap rate."""


class SwapCalculateRequest(BaseModel):
    """Request model for swap calculation."""

    from_currency: str = Field(
        ...,
        description="Source currency symbol",
    )
    to_currency: str = Field(
        ...,
        description="Target currency symbol",
    )
    amount: float = Field(..., description="Amount to convert")


class SwapCalculateData(BaseModel):
    """Data model for swap calculation."""

    rate: float = Field(..., description="Swap rate")
    amount: float = Field(..., description="Input amount")
    to_amount: float = Field(
        ...,
        validation_alias="to_amount",
        description="Resulting amount",
    )


class SwapCalculateResponse(DataResponse[SwapCalculateData]):
    """Response model for swap calculation."""


class SwapRequest(BaseModel):
    """Request model for creating a swap."""

    from_currency: str = Field(
        ...,
        description="Source currency symbol",
    )
    to_currency: str = Field(
        ...,
        description="Target currency symbol",
    )
    amount: float = Field(..., description="Amount to convert")


class SwapData(TrackIdMixin, BaseModel):
    """Data model for swap transaction."""

    status: str | None = Field(None, description="Swap status")
    amount: float | None = Field(None, description="Input amount")
    from_currency: str = Field(
        ...,
        validation_alias="fromCurrency",
        description="Source currency",
    )
    to_currency: str = Field(
        ...,
        validation_alias="toCurrency",
        description="Target currency",
    )
    to_amount: float | None = Field(
        None,
        validation_alias="toAmount",
        description="Resulting amount",
    )
    rate: float | None = Field(None, description="Swap rate")
    date: str = Field(..., description="Swap timestamp")
    from_amount: float | None = Field(
        None,
        validation_alias="fromAmount",
        description="Input amount alias",
    )


class SwapResponse(DataResponse[SwapData]):
    """Response model for swap request."""


class SwapHistoryRequest(BaseModel):
    """Request model for swap history."""

    track_id: int | None = Field(
        None,
        description="Filter by transaction ID",
    )
    type: SwapType | None = Field(None, description="Filter by swap type")
    from_currency: str | None = Field(
        None,
        description="Filter by source currency",
    )
    to_currency: str | None = Field(
        None,
        description="Filter by target currency",
    )
    from_date: int | None = Field(
        None,
        description="Start date (unix timestamp)",
    )
    to_date: int | None = Field(
        None,
        description="End date (unix timestamp)",
    )
    sort_by: SortField | None = Field(
        SortField.CREATE_DATE,
        description="Sort field",
    )
    sort_type: OrderDirection | None = Field(
        OrderDirection.DESC,
        description="Sort direction",
    )
    size: int | None = Field(10, ge=1, le=200, description="Records per page")
    page: int | None = Field(1, ge=1, description="Page number")


class SwapHistoryResponse(PaginatedResponse[SwapData]):
    """Response model for swap history."""
