"""$Name plugin API routes."""

from flask import Blueprint, request
from flask_jwt_extended import jwt_required

from shopdb.utils.responses import (
    success_response,
    error_response,
    paginated_response,
    ErrorCodes,
)
from shopdb.utils.pagination import get_pagination_params, paginate_query

from ..models import $Name


${name}_bp = Blueprint('$name', __name__)


@${name}_bp.route('', methods=['GET'])
@jwt_required(optional=True)
def list_${name}():
    """List $name assets, paginated."""
    page, per_page = get_pagination_params(request)

    query = ${Name}.query
    items, total = paginate_query(query, page, per_page)
    data = [item.to_dict() for item in items]

    return paginated_response(data, page, per_page, total)


@${name}_bp.route('/<int:assetid>', methods=['GET'])
@jwt_required(optional=True)
def get_${name}(assetid: int):
    """Get a single $name by assetid."""
    item = ${Name}.query.get(assetid)
    if not item:
        return error_response(
            ErrorCodes.NOT_FOUND,
            f'$Name with assetid {assetid} not found',
            http_code=404,
        )
    return success_response(item.to_dict())
