"""API routers for {{ module_name }} domain."""

from typing import List
from fastapi import APIRouter, Depends, HTTPException, Query, status
from sqlalchemy.orm import Session

from config.database import get_db
from .schemas import {{ module_name_title }}, {{ module_name_title }}Create, {{ module_name_title }}Update, {{ module_name_title }}List
from .services import {{ module_name_title }}Service
from .models import {{ module_name_title }} as {{ module_name_title }}Model

router = APIRouter(tags=["{{ module_name }}s"])


@router.get("/", response_model={{ module_name_title }}List)
async def get_{{ module_name }}s(
    skip: int = Query(0, ge=0, description="Number of items to skip"),
    limit: int = Query(100, ge=1, le=1000, description="Number of items to return"),
    active_only: bool = Query(True, description="Return only active {{ module_name }}s"),
    db: Session = Depends(get_db)
):
    """Get all {{ module_name }}s with pagination."""
    {{ module_name }}s = {{ module_name_title }}Service.get_{{ module_name }}s(
        db, skip=skip, limit=limit, active_only=active_only
    )
    
    # Count total records for pagination
    total = db.query({{ module_name_title }}Model).count()
    
    return {{ module_name_title }}List(
        items={{ module_name }}s,
        total=total,
        page=skip // limit + 1,
        size=limit
    )


@router.get("/{ {{ module_name }}_id }", response_model={{ module_name_title }})
async def get_{{ module_name }}({{ module_name }}_id: int, db: Session = Depends(get_db)):
    """Get a specific {{ module_name }} by ID."""
    try:
        return {{ module_name_title }}Service.get_{{ module_name }}(db, {{ module_name }}_id)
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=str(e)
        )


@router.post("/", response_model={{ module_name_title }}, status_code=status.HTTP_201_CREATED)
async def create_{{ module_name }}({{ module_name }}_data: {{ module_name_title }}Create, db: Session = Depends(get_db)):
    """Create a new {{ module_name }}."""
    try:
        return {{ module_name_title }}Service.create_{{ module_name }}(db, {{ module_name }}_data)
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=str(e)
        )


@router.put("/{ {{ module_name }}_id }", response_model={{ module_name_title }})
async def update_{{ module_name }}(
    {{ module_name }}_id: int, 
    {{ module_name }}_data: {{ module_name_title }}Update, 
    db: Session = Depends(get_db)
):
    """Update an existing {{ module_name }}."""
    try:
        return {{ module_name_title }}Service.update_{{ module_name }}(db, {{ module_name }}_id, {{ module_name }}_data)
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_400_BAD_REQUEST,
            detail=str(e)
        )


@router.delete("/{ {{ module_name }}_id }", status_code=status.HTTP_204_NO_CONTENT)
async def delete_{{ module_name }}({{ module_name }}_id: int, db: Session = Depends(get_db)):
    """Soft delete a {{ module_name }} (set is_active to False)."""
    try:
        {{ module_name_title }}Service.delete_{{ module_name }}(db, {{ module_name }}_id)
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=str(e)
        )


@router.delete("/{ {{ module_name }}_id }/hard", status_code=status.HTTP_204_NO_CONTENT)
async def hard_delete_{{ module_name }}({{ module_name }}_id: int, db: Session = Depends(get_db)):
    """Hard delete a {{ module_name }} from database."""
    try:
        {{ module_name_title }}Service.hard_delete_{{ module_name }}(db, {{ module_name }}_id)
    except Exception as e:
        raise HTTPException(
            status_code=status.HTTP_404_NOT_FOUND,
            detail=str(e)
        )
