# app/modules/payments/schemas/payment.py
"""
Payment Pydantic schemas for API validation and responses.

This module provides schemas for:
- Payment configuration
- Stripe integration
- Payment methods
- Transactions and balance
- Refunds
"""

from datetime import datetime
from typing import Any

from pydantic import BaseModel, Field

# ============================================================================
# PAYMENT CONFIGURATION SCHEMAS
# ============================================================================


class PaymentConfigResponse(BaseModel):
    """Response for payment configuration."""

    payment_gateway: str | None = None
    accepted_methods: list[str] = []
    currency: str = "EUR"
    stripe_connected: bool = False
    stripe_account_id: str | None = None
    paypal_connected: bool = False
    message: str | None = None


class PaymentConfigUpdate(BaseModel):
    """Request model for updating payment configuration."""

    payment_gateway: str | None = Field(None, max_length=50)
    accepted_methods: list[str] | None = None
    currency: str | None = Field(None, max_length=3)


class PaymentConfigUpdateResponse(BaseModel):
    """Response for payment configuration update."""

    success: bool = False
    message: str | None = None


# ============================================================================
# STRIPE INTEGRATION SCHEMAS
# ============================================================================


class StripeConnectRequest(BaseModel):
    """Request model for connecting Stripe account."""

    authorization_code: str | None = None
    state: str | None = None


class StripeConnectResponse(BaseModel):
    """Response for Stripe connection."""

    connected: bool = False
    stripe_account_id: str | None = None
    message: str | None = None


class StripeDisconnectResponse(BaseModel):
    """Response for Stripe disconnection."""

    disconnected: bool = False
    message: str | None = None


# ============================================================================
# PAYMENT METHODS SCHEMAS
# ============================================================================


class PaymentMethodInfo(BaseModel):
    """Information about a payment method."""

    id: str
    name: str
    type: str  # credit_card, paypal, bank_transfer, etc.
    enabled: bool = True
    icon: str | None = None


class PaymentMethodsResponse(BaseModel):
    """Response for payment methods listing."""

    methods: list[PaymentMethodInfo] = []
    message: str | None = None


# ============================================================================
# TRANSACTION SCHEMAS
# ============================================================================


class TransactionInfo(BaseModel):
    """Information about a payment transaction."""

    id: int
    order_id: int | None = None
    amount: float
    currency: str = "EUR"
    status: str  # pending, completed, failed, refunded
    payment_method: str | None = None
    customer_email: str | None = None
    created_at: datetime
    completed_at: datetime | None = None
    metadata: dict[str, Any] | None = None


class TransactionsResponse(BaseModel):
    """Response for payment transactions listing."""

    transactions: list[TransactionInfo] = []
    total: int = 0
    skip: int = 0
    limit: int = 50
    message: str | None = None


# ============================================================================
# BALANCE SCHEMAS
# ============================================================================


class PaymentBalanceResponse(BaseModel):
    """Response for payment balance information."""

    available_balance: float = 0.0
    pending_balance: float = 0.0
    currency: str = "EUR"
    next_payout_date: datetime | None = None
    last_payout_date: datetime | None = None
    last_payout_amount: float | None = None
    message: str | None = None


# ============================================================================
# REFUND SCHEMAS (for payment config endpoints)
# ============================================================================


class PaymentRefundRequest(BaseModel):
    """Request model for processing a refund (payment module version)."""

    amount: float | None = Field(
        None, gt=0, description="Partial refund amount, or None for full refund"
    )
    reason: str | None = Field(None, max_length=500)


class PaymentRefundResponse(BaseModel):
    """Response for refund operation (payment module version)."""

    refund_id: int | None = None
    payment_id: int | None = None
    amount: float | None = None
    status: str | None = None  # pending, completed, failed
    message: str | None = None
