"""
Basic FastAPI reference app — companion to notes.html.
Run with: uvicorn main:app --reload
Docs at:  http://127.0.0.1:8000/docs
"""

from fastapi import FastAPI
from pydantic import BaseModel

app = FastAPI(title="FastAPI Basics")


# ── Minimal route ──────────────────────────────────────────────
@app.get("/")
def read_root():
    return {"message": "Hello, FastAPI"}


# ── Path parameters ────────────────────────────────────────────
@app.get("/items/{item_id}")
def read_item(item_id: int):
    return {"item_id": item_id}


# ── Query parameters ───────────────────────────────────────────
@app.get("/items/")
def list_items(skip: int = 0, limit: int = 10, q: str | None = None):
    return {"skip": skip, "limit": limit, "q": q}


# ── Request body (Pydantic model) ──────────────────────────────
class Item(BaseModel):
    name: str
    price: float
    in_stock: bool = True


@app.post("/items/")
def create_item(item: Item):
    return {"received": item}


# ── Response model (shapes what's returned, hides extra fields) ─
class ItemOut(BaseModel):
    name: str
    price: float


@app.post("/items/typed", response_model=ItemOut)
def create_item_typed(item: Item):
    return item
