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()

# --- 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 get_{{ table.name }}_by_id(id: any, db: Session) -> {{ table.class_name }}:
    db_obj = db.query({{ table.class_name }}).filter({{ table.class_name }}.{{ table.primary_keys[0] }} == id).first()
    if not db_obj:
        raise HTTPException(status_code=404, detail="{{ table.class_name }} not found")
    return db_obj

# --- 4. Endpoint ---
@router.get("/{id}", response_model={{ table.class_name }}Response)
async def get_by_id_endpoint(id: int, db: Session = Depends(get_db)):
    return get_{{ table.name }}_by_id(id, db)
