"""Recipe API schemas."""

from __future__ import annotations

from datetime import datetime
from typing import Any, Dict, List, Optional

from pydantic import BaseModel, Field

# ── Response ──


class RecipeItemResponse(BaseModel):
    id: str
    recipe_id: str
    item_type: str
    item_id: str
    position: int
    metadata_: Dict[str, Any] = Field(default_factory=dict, alias="metadata")

    class Config:
        populate_by_name = True


class RecipeResponse(BaseModel):
    id: str
    user_id: str
    name: str
    description: str = ""
    template_id: str = ""
    generation_params: Dict[str, Any] = Field(default_factory=dict)
    items: List[RecipeItemResponse] = Field(default_factory=list)
    is_active: bool = True
    metadata_: Dict[str, Any] = Field(default_factory=dict, alias="metadata")
    created_at: datetime
    updated_at: datetime

    class Config:
        populate_by_name = True


class ListRecipesResponse(BaseModel):
    items: List[RecipeResponse]
    total: int = 0


class UseRecipeResponse(BaseModel):
    recipe: RecipeResponse
    warnings: List[Dict[str, Any]] = Field(default_factory=list)


# ── Request ──


class RecipeItemRequest(BaseModel):
    item_type: str
    item_id: str
    position: int = 0
    metadata_: Dict[str, Any] = Field(default_factory=dict, alias="metadata")

    class Config:
        populate_by_name = True


class CreateRecipeRequest(BaseModel):
    name: str
    description: str = ""
    template_id: str = ""
    generation_params: Dict[str, Any] = Field(default_factory=dict)
    items: List[RecipeItemRequest] = Field(default_factory=list)
    metadata_: Dict[str, Any] = Field(default_factory=dict, alias="metadata")

    class Config:
        populate_by_name = True


class UpdateRecipeRequest(BaseModel):
    name: Optional[str] = None
    description: Optional[str] = None
    template_id: Optional[str] = None
    generation_params: Optional[Dict[str, Any]] = None
    items: Optional[List[RecipeItemRequest]] = None
    metadata_: Optional[Dict[str, Any]] = Field(default=None, alias="metadata")

    class Config:
        populate_by_name = True
