from datetime import datetime
from typing import Optional, List
from pydantic import BaseModel, Field

# API Response Schemas (matching TCGPlayer API structure)
class TCGPlayerAPIOrderProduct(BaseModel):
    name: str
    unitPrice: float
    extendedPrice: float
    quantity: int
    url: str
    productId: str
    skuId: str

class TCGPlayerAPIOrderTax(BaseModel):
    code: str
    amount: float

class TCGPlayerAPIOrderTransaction(BaseModel):
    productAmount: float
    shippingAmount: float
    grossAmount: float
    feeAmount: float
    netAmount: float
    directFeeAmount: float
    taxes: List[TCGPlayerAPIOrderTax]

class TCGPlayerAPIOrderRefund(BaseModel):
    createdAt: datetime
    type: str
    amount: float
    description: Optional[str] = None
    origin: str
    shippingAmount: float
    products: List[dict]

class TCGPlayerAPIOrderShippingAddress(BaseModel):
    recipientName: str
    addressOne: str
    addressTwo: Optional[str] = None
    city: str
    territory: str
    country: str
    postalCode: str

class TCGPlayerAPIOrderTrackingNumber(BaseModel):
    createdAt: str
    carrier: str
    trackingNumber: str
    status: str

class TCGPlayerAPIOrder(BaseModel):
    createdAt: str
    status: str
    orderChannel: str
    orderFulfillment: str
    orderNumber: str
    sellerName: str
    buyerName: str
    paymentType: str
    pickupStatus: str
    shippingType: str
    estimatedDeliveryDate: datetime
    transaction: TCGPlayerAPIOrderTransaction
    shippingAddress: TCGPlayerAPIOrderShippingAddress
    products: List[TCGPlayerAPIOrderProduct]
    refunds: Optional[List[TCGPlayerAPIOrderRefund]]
    refundStatus: str
    trackingNumbers: List[TCGPlayerAPIOrderTrackingNumber]
    allowedActions: List[str]

class TCGPlayerAPIOrderSummary(BaseModel):
    orderNumber: str
    orderDate: datetime
    orderChannel: str
    orderStatus: str
    buyerName: str
    shippingType: str
    productAmount: float
    shippingAmount: float
    totalAmount: float
    buyerPaid: bool
    orderFulfillment: str

class TCGPlayerAPIOrderSearchResponse(BaseModel):
    totalOrders: int
    orders: List[TCGPlayerAPIOrderSummary]
# Database Schemas (matching your internal structure)
class TCGPlayerOrderBase(BaseModel):
    order_number: str
    order_created_at: datetime
    status: str
    channel: str
    fulfillment: str
    seller_name: str
    buyer_name: str
    payment_type: str
    pickup_status: str
    shipping_type: str
    estimated_delivery_date: Optional[datetime]
    recipient_name: str
    address_line_1: str
    address_line_2: Optional[str]
    city: str
    state: str
    zip_code: str
    country: str
    tracking_numbers: List[str]

class TCGPlayerOrderCreate(TCGPlayerOrderBase):
    pass

class TCGPlayerOrder(TCGPlayerOrderBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

class TCGPlayerOrderTransactionBase(BaseModel):
    order_number: str
    product_amount: float
    shipping_amount: float
    gross_amount: float
    fee_amount: float
    net_amount: float
    direct_fee_amount: float
    taxes: List[dict]

class TCGPlayerOrderTransactionCreate(TCGPlayerOrderTransactionBase):
    pass

class TCGPlayerOrderTransaction(TCGPlayerOrderTransactionBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

class TCGPlayerOrderProductBase(BaseModel):
    order_number: str
    product_name: str
    unit_price: float
    extended_price: float
    quantity: int
    url: str
    product_id: str
    sku_id: str

class TCGPlayerOrderProductCreate(TCGPlayerOrderProductBase):
    pass

class TCGPlayerOrderProduct(TCGPlayerOrderProductBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True

class TCGPlayerOrderRefundBase(BaseModel):
    order_number: str
    refund_created_at: datetime
    type: str
    amount: float
    description: Optional[str] = None
    origin: str
    shipping_amount: float
    products: List[dict]

class TCGPlayerOrderRefundCreate(TCGPlayerOrderRefundBase):
    pass

class TCGPlayerOrderRefund(TCGPlayerOrderRefundBase):
    id: int
    created_at: datetime
    updated_at: datetime

    class Config:
        from_attributes = True 