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

from app.models.order_models import OrderStatus, PaymentMethod


# Схемы для элементов корзины
class CartItemBase(BaseModel):
    variant_id: int
    quantity: int = 1


class CartItemCreate(CartItemBase):
    pass


class CartItemUpdate(BaseModel):
    quantity: Optional[int] = None


class CartItem(CartItemBase):
    id: int
    user_id: int
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


class CartItemWithProduct(CartItem):
    product_name: str
    product_price: float
    product_image: Optional[str] = None
    variant_name: str
    variant_price_adjustment: float
    total_price: float


# Схемы для элементов заказа
class OrderItemBase(BaseModel):
    variant_id: int
    quantity: int = 1
    price: float


class OrderItemCreate(OrderItemBase):
    pass


class OrderItem(OrderItemBase):
    id: int
    order_id: int
    created_at: datetime

    class Config:
        from_attributes = True


class OrderItemWithProduct(OrderItem):
    product_name: str
    variant_name: Optional[str] = None


# Схемы для заказов
class OrderBase(BaseModel):
    shipping_address_id: Optional[int] = None
    payment_method: Optional[PaymentMethod] = None
    notes: Optional[str] = None


class OrderCreate(OrderBase):
    cart_items: Optional[List[int]] = None  # ID элементов корзины
    items: Optional[List[OrderItemCreate]] = None  # Прямые элементы заказа


class OrderUpdate(BaseModel):
    status: Optional[OrderStatus] = None
    shipping_address_id: Optional[int] = None
    payment_method: Optional[PaymentMethod] = None
    payment_details: Optional[str] = None
    tracking_number: Optional[str] = None
    notes: Optional[str] = None


class Order(OrderBase):
    id: int
    user_id: int
    status: OrderStatus
    total_amount: float
    payment_details: Optional[str] = None
    tracking_number: Optional[str] = None
    created_at: datetime
    updated_at: Optional[datetime] = None
    items: List[OrderItem] = []

    class Config:
        from_attributes = True


# Расширенные схемы для отображения
class CartItemWithDetails(BaseModel):
    id: int
    user_id: int
    variant_id: int
    quantity: int
    created_at: datetime
    updated_at: Optional[datetime] = None
    product_id: int
    product_name: str
    product_price: float
    product_image: Optional[str] = None
    variant_name: str
    variant_price_adjustment: float
    total_price: float


class OrderWithDetails(BaseModel):
    id: int
    user_id: int
    status: OrderStatus
    total_amount: float
    shipping_address_id: Optional[int] = None
    payment_method: Optional[PaymentMethod] = None
    payment_details: Optional[str] = None
    tracking_number: Optional[str] = None
    notes: Optional[str] = None
    created_at: datetime
    updated_at: Optional[datetime] = None
    user_email: Optional[str] = None
    shipping_address: Optional[Dict[str, Any]] = None
    items: List[Dict[str, Any]] = [] 