from fastapi import APIRouter, Depends, HTTPException
from pydantic import BaseModel
from sqlalchemy.orm import Session
from datetime import datetime
from {{ base_package }}.shared.database import get_db
from {{ base_package }}.domain.{{ table.name }}_model import {{ table.class_name }}

router = APIRouter()

# --- 1. Request DTO ---
class Create{{ table.class_name }}Request(BaseModel):
    {% for col in table.columns if not col.is_pk -%}
    {{ col.name }}: {{ col.python_type }}{% if col.nullable %} = None{% endif %}
    {% endfor %}

# --- 2. Response DTO ---
class {{ table.class_name }}Response(BaseModel):
    {% for col in table.columns -%}
    {{ col.name }}: {{ col.python_type }}{% if col.nullable %} = None{% endif %}
    {% endfor %}
    
    class Config:
        from_attributes = True

# --- 3. Business Logic ---
def create_{{ table.name }}(request: Create{{ table.class_name }}Request, db: Session) -> {{ table.class_name }}:
    db_obj = {{ table.class_name }}(
        **request.model_dump()
    )
    db.add(db_obj)
    db.commit()
    db.refresh(db_obj)
    return db_obj

# --- 4. Endpoint ---
@router.post("/", response_model={{ table.class_name }}Response)
async def create_endpoint(req: Create{{ table.class_name }}Request, db: Session = Depends(get_db)):
    return create_{{ table.name }}(req, db)
