"""Models for OxaPay Payout API."""

from enum import Enum

from pydantic import BaseModel, Field

from .base import OrderDirection, PaginatedResponse, SortField
from .common import (
    AddressMixin,
    AmountCurrencyMixin,
    DataResponse,
    TrackIdMixin,
)


class PayoutStatus(str, Enum):
    """Payout status enum."""

    PROCESSING = "processing"
    PENDING = "pending"
    CONFIRMING = "confirming"
    CONFIRMED = "confirmed"
    COMPLETE = (
        "complete"  # Keeping for backward compat if needed, but docs say confirmed
    )
    REJECTED = "rejected"
    CANCELED = "canceled"


class PayoutType(str, Enum):
    """Payout type enum."""

    INTERNAL = "internal"
    EXTERNAL = "external"


class CreatePayoutRequest(BaseModel):
    """Request model for creating a payout."""

    address: str = Field(..., description="Cryptocurrency address of the recipient")
    amount: float = Field(..., description="Amount of cryptocurrency to send")
    currency: str = Field(..., description="Cryptocurrency symbol")
    network: str | None = Field(None, description="Blockchain network")
    callback_url: str | None = Field(
        None,
        description="URL for payout notifications",
    )
    description: str | None = Field(None, description="Additional information")


class PayoutData(
    TrackIdMixin,
    AmountCurrencyMixin,
    AddressMixin,
    BaseModel,
):
    """Data model for payout information."""

    network: str | None = Field(None, description="Blockchain network")
    status: str = Field(..., description="Current status")
    tx_hash: str | None = Field(None, description="Transaction hash")
    description: str | None = Field(None, description="Description")
    date: str = Field(..., description="Creation timestamp")
    type: str | None = Field(None, description="Payout type")
    fee: float | None = Field(None, description="Transaction fee")
    internal: bool | None = Field(None, description="Internal transaction flag")
    memo: str | None = Field(None, description="Memo")


class CreatePayoutResponse(DataResponse[PayoutData]):
    """Response model for payout creation."""


class PayoutInfoRequest(BaseModel):
    """Request model for getting payout information."""

    track_id: str = Field(
        ...,
        description="Unique identifier of the payout",
    )


class PayoutInfoResponse(DataResponse[PayoutData]):
    """Response model for payout information."""


class PayoutHistoryRequest(BaseModel):
    """Request model for getting payout history."""

    status: str | None = Field(None, description="Filter by status")
    type: str | None = Field(None, description="Filter by type")
    currency: str | None = Field(None, description="Filter by currency")
    network: str | None = Field(None, description="Filter by network")
    from_amount: float | None = Field(None, description="Min amount")
    to_amount: float | None = Field(None, description="Max amount")
    from_date: int | None = Field(None, description="Start date (unix)")
    to_date: int | None = Field(None, description="End date (unix)")
    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="Page size")
    page: int | None = Field(1, ge=1, description="Page number")


class PayoutHistoryResponse(PaginatedResponse[PayoutData]):
    """Response model for payout history."""
