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


# Схемы для коллекций
class CollectionBase(BaseModel):
    name: str
    slug: Optional[str] = None
    description: Optional[str] = None
    is_active: bool = True


class CollectionCreate(CollectionBase):
    pass


class CollectionUpdate(CollectionBase):
    name: Optional[str] = None
    slug: Optional[str] = None
    description: Optional[str] = None
    is_active: Optional[bool] = None


class Collection(CollectionBase):
    id: int
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# Схемы для категорий
class CategoryBase(BaseModel):
    name: str
    slug: Optional[str] = None
    description: Optional[str] = None
    parent_id: Optional[int] = None
    is_active: bool = True


class CategoryCreate(CategoryBase):
    pass


class CategoryUpdate(CategoryBase):
    name: Optional[str] = None
    slug: Optional[str] = None
    description: Optional[str] = None
    parent_id: Optional[int] = None
    is_active: Optional[bool] = None


class Category(CategoryBase):
    id: int
    created_at: datetime
    updated_at: Optional[datetime] = None
    products_count: Optional[int] = 0

    class Config:
        from_attributes = True



# Схемы для вариантов продуктов
class ProductVariantBase(BaseModel):
    product_id: int
    name: str
    sku: str
    price: float
    discount_price: Optional[float] = None
    stock: int = 0
    is_active: bool = True


class ProductVariantCreate(ProductVariantBase):
    pass


class ProductVariantUpdate(ProductVariantBase):
    product_id: Optional[int] = None
    name: Optional[str] = None
    sku: Optional[str] = None
    price: Optional[float] = None
    discount_price: Optional[float] = None
    stock: Optional[int] = None
    is_active: Optional[bool] = None


class ProductVariant(ProductVariantBase):
    id: int
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True


# Схемы для изображений продуктов
class ProductImageBase(BaseModel):
    product_id: int
    image_url: str
    alt_text: Optional[str] = None
    is_primary: bool = False


class ProductImageCreate(ProductImageBase):
    pass


class ProductImageUpdate(BaseModel):
    alt_text: Optional[str] = None
    is_primary: Optional[bool] = None


class ProductImage(ProductImageBase):
    id: int
    created_at: datetime
    updated_at: Optional[datetime] = None

    class Config:
        from_attributes = True



# Схемы для продуктов
class ProductBase(BaseModel):
    name: str
    slug: Optional[str] = None
    description: Optional[str] = None
    is_active: bool = True
    category_id: int
    collection_id: Optional[int] = None


class ProductCreate(ProductBase):
    pass


class ProductUpdate(ProductBase):
    name: Optional[str] = None
    slug: Optional[str] = None
    description: Optional[str] = None
    is_active: Optional[bool] = None
    category_id: Optional[int] = None
    collection_id: Optional[int] = None


class Product(ProductBase):
    id: int
    created_at: datetime
    updated_at: Optional[datetime] = None
    variants: Optional[List[ProductVariant]] = []
    images: Optional[List[ProductImage]] = []

    class Config:
        from_attributes = True


# Расширенные схемы для отображения
class CategoryWithSubcategories(Category):
    subcategories: List['CategoryWithSubcategories'] = []
    products_count: Optional[int] = 0

    class Config:
        from_attributes = True


class ProductWithDetails(Product):
    category: Category
    collection: Optional[Collection] = None
    variants: List[ProductVariant] = []
    images: List[ProductImage] = []

    class Config:
        from_attributes = True


# Рекурсивное обновление для CategoryWithChildren
CategoryWithSubcategories.update_forward_refs() 