from datetime import datetime
from typing import Any

from pydantic import BaseModel, ConfigDict, Field


class CartItemCreate(BaseModel):
    product_id: int
    cycle: str = Field(..., min_length=1, max_length=16)
    quantity: int = Field(1, ge=1)
    auto_renew: bool = False
    alias: str | None = Field(None, max_length=64)
    os_image: str | None = Field(None, max_length=128)
    unit_price: float = Field(..., ge=0)
    addon_price: float = Field(0, ge=0)
    setup_fee: float = Field(0, ge=0)
    slider_values: dict[str, Any] | None = None


class CartItemUpdate(BaseModel):
    quantity: int | None = Field(None, ge=1)
    auto_renew: bool | None = None
    alias: str | None = Field(None, max_length=64)


class CartProductSummary(BaseModel):
    """购物车里展开的产品简要信息（嵌入响应里，避免前端再发一次产品请求）。"""

    model_config = ConfigDict(from_attributes=True)
    id: int
    sku: str
    name: str
    status: str
    vcpu: int | None
    memory: str | None
    disk: str | None
    bandwidth: str | None
    stock_policy: str
    stock: int
    min_quantity: int
    max_quantity: int
    group_id: int


class CartItemResponse(BaseModel):
    model_config = ConfigDict(from_attributes=True)
    id: int
    user_id: int
    product_id: int
    cycle: str
    quantity: int
    auto_renew: bool
    alias: str | None
    os_image: str | None
    unit_price: float
    addon_price: float
    setup_fee: float
    slider_values: dict[str, Any] | None
    created_at: datetime
    updated_at: datetime
    product: CartProductSummary | None = None
