"""
Модели данных для AuroraPay Payment API
"""

from enum import Enum
from typing import Optional
from dataclasses import dataclass


class PaymentMethod(str, Enum):
    """Методы оплаты"""
    CARD = "card"  # Карты РФ
    SBP = "sbp"    # СБП (Система Быстрых Платежей)
    KZT = "kzt"    # Карты Казахстана


class PaymentStatus(str, Enum):
    """Статусы платежа"""
    PENDING = "pending"           # Ожидает обработки
    PROCESSING = "processing"     # В процессе обработки
    COMPLETED = "completed"       # Успешно завершен
    FAILED = "failed"            # Ошибка при обработке
    CANCELED = "canceled"        # Отменен пользователем
    AUTOCANCELED = "autocanceled"  # Автоматически отменен


@dataclass
class CreateOrderRequest:
    """Запрос на создание заказа"""
    amount: float
    payment_method: PaymentMethod
    
    def to_dict(self) -> dict:
        return {
            "amount": self.amount,
            "paymentMethod": self.payment_method.value
        }


@dataclass
class PaymentDetails:
    """Детали платежа"""
    bank_name: Optional[str]
    cardholder: Optional[str]
    wallet_payment: str
    order_status: str
    amount_payable: str
    amount_receivable: str
    exchange_rate: str


@dataclass
class CreateOrderResponse:
    """Ответ на создание заказа"""
    order_id: str
    bank: str
    amount: float
    commission: float
    commission_percent: float
    payment_details: PaymentDetails
    payment_link: str
    
    @classmethod
    def from_dict(cls, data: dict) -> "CreateOrderResponse":
        payment_details = PaymentDetails(
            bank_name=data["paymentDetails"].get("bankName"),
            cardholder=data["paymentDetails"].get("cardholder"),
            wallet_payment=data["paymentDetails"]["walletPayment"],
            order_status=data["paymentDetails"]["orderStatus"],
            amount_payable=data["paymentDetails"]["amountPayable"],
            amount_receivable=data["paymentDetails"]["amountReceivable"],
            exchange_rate=data["paymentDetails"]["exchangeRate"],
        )
        
        return cls(
            order_id=data["orderId"],
            bank=data["bank"],
            amount=data["amount"],
            commission=data["commission"],
            commission_percent=data["commissionPercent"],
            payment_details=payment_details,
            payment_link=data["paymentLink"],
        )


@dataclass
class PaymentStatusResponse:
    """Ответ на запрос статуса платежа"""
    order_id: str
    status: PaymentStatus
    amount: float
    payment_method: Optional[str]
    payment_details: dict
    created_at: str
    updated_at: str
    
    @classmethod
    def from_dict(cls, data: dict) -> "PaymentStatusResponse":
        return cls(
            order_id=data["orderId"],
            status=PaymentStatus(data["status"]),
            amount=data["amount"],
            payment_method=data.get("paymentMethod"),
            payment_details=data["paymentDetails"],
            created_at=data["createdAt"],
            updated_at=data["updatedAt"],
        )
