from fastapi import APIRouter, Depends
from fastapi_pagination import Page, add_pagination

{% if is_async %}
from sqlalchemy.ext.asyncio import AsyncSession
{% else %}
from sqlalchemy.orm import Session
{% endif %}

from app.database import get_db
from .dependencies import parse_query_params

{% if use_auth and name == 'user' %}
from app.auth.routers import fastapi_users
from app.models.user_model import User
from app.schemas.user_schema import UserRead
from app.services.user_service import user_service as service

# Dependency to get the current, active, superuser
current_superuser = fastapi_users.current_user(active=True, superuser=True)

router = APIRouter(prefix="/users", tags=["users"])

@router.get("/", response_model=Page[UserRead], summary="Get All Users (Admin Only)")
async def get_all_users(
    query: dict = Depends(parse_query_params),
    db: AsyncSession = Depends(get_db),
    user: User = Depends(current_superuser)
):
    """
    (Admin Only) Retrieve a paginated and filterable list of all users.
    """
    return await service.get_all(db, query=query)

# Add pagination to the router
add_pagination(router)

{% else %}
from fastapi import HTTPException, status
from app.services.{{ name }}_service import {{ name }}_service as service
from app.schemas.{{ name }}_schema import {{ ClassName }}Create, {{ ClassName }}Update, {{ ClassName }}Response

router = APIRouter(prefix="/{{ plural_name }}", tags=["{{ plural_name }}"])

@router.post("/", response_model={{ ClassName }}Response, status_code=status.HTTP_201_CREATED)
{% if is_async %}async {% endif %}def create_{{ name }}(
    payload: {{ ClassName }}Create,
    db: {% if is_async %}AsyncSession{% else %}Session{% endif %} = Depends(get_db)
):
    return {% if is_async %}await {% endif %}service.create(db, payload=payload)

@router.get("/", response_model=Page[{{ ClassName }}Response])
{% if is_async %}async {% endif %}def get_all_{{ name }}s(
    query: dict = Depends(parse_query_params),
    db: {% if is_async %}AsyncSession{% else %}Session{% endif %} = Depends(get_db)
):
    return {% if is_async %}await {% endif %}service.get_all(db, query=query)

@router.get("/{{ '{' }}{{ name }}_id{{ '}' }}", response_model={{ ClassName }}Response)
{% if is_async %}async {% endif %}def get_{{ name }}_by_id(
    {{ name }}_id: int,
    db: {% if is_async %}AsyncSession{% else %}Session{% endif %} = Depends(get_db)
):
    db_obj = {% if is_async %}await {% endif %}service.get_by_id(db, id={{ name }}_id)
    if db_obj is None:
        raise HTTPException(status_code=404, detail="{{ ClassName }} not found")
    return db_obj

@router.put("/{{ '{' }}{{ name }}_id{{ '}' }}", response_model={{ ClassName }}Response)
{% if is_async %}async {% endif %}def update_{{ name }}(
    {{ name }}_id: int,
    payload: {{ ClassName }}Update,
    db: {% if is_async %}AsyncSession{% else %}Session{% endif %} = Depends(get_db)
):
    db_obj = {% if is_async %}await {% endif %}service.update(db, id={{ name }}_id, payload=payload)
    if db_obj is None:
        raise HTTPException(status_code=404, detail="{{ ClassName }} not found")
    return db_obj

@router.delete("/{{ '{' }}{{ name }}_id{{ '}' }}", response_model={{ ClassName }}Response)
{% if is_async %}async {% endif %}def delete_{{ name }}(
    {{ name }}_id: int,
    db: {% if is_async %}AsyncSession{% else %}Session{% endif %} = Depends(get_db)
):
    db_obj = {% if is_async %}await {% endif %}service.delete(db, id={{ name }}_id)
    if db_obj is None:
        raise HTTPException(status_code=404, detail="{{ ClassName }} not found")
    return db_obj

add_pagination(router)
{% endif %}