"""
{{ pascal_name }} Endpoints
Demonstrates AURA v2 patterns:
- Atlas SSO authentication
- Two-level authorization
- Response encryption for GET
- Proper HTTP status codes
"""
from fastapi import APIRouter, Depends, Query, status
from sqlalchemy.orm import Session
from atams.db import get_db
from atams.schemas import DataResponse, PaginationResponse

from app.services.{{ singular_name }}_service import {{ pascal_name }}Service
from app.schemas.{{ singular_name }} import {{ pascal_name }}, {{ pascal_name }}Create, {{ pascal_name }}Update

router = APIRouter()
{{ singular_name }}_service = {{ pascal_name }}Service()


# NOTE: Atlas SSO dependencies need to be injected from main app
# Example:
# from app.api.deps import require_auth, require_min_role_level
#
# Then use:
# dependencies=[Depends(require_min_role_level(50))]
# current_user: dict = Depends(require_auth)


@router.get(
    "/",
    status_code=status.HTTP_200_OK,
)
async def get_{{ plural_name }}(
    skip: int = Query(0, ge=0, description="Number of records to skip"),
    limit: int = Query(100, ge=1, le=1000, description="Maximum records to return"),
    db: Session = Depends(get_db),
    # current_user: dict = Depends(require_auth)  # TODO: Add auth
):
    """
    Get list of {{ plural_name }} with pagination

    **Authorization:**
    - FIRST LEVEL (Route): Requires role level >= 50
    - SECOND LEVEL (Service): Level 100 can view all, Level 50 can view org
    """
    {{ plural_name }} = {{ singular_name }}_service.get_{{ plural_name }}(
        db,
        skip=skip,
        limit=limit,
        current_user_role_level=100  # TODO: Use current_user["role_level"]
    )
    total = {{ singular_name }}_service.get_total_{{ plural_name }}(db)

    response = PaginationResponse(
        success=True,
        message="{{ pascal_name }} retrieved successfully",
        data={{ plural_name }},
        total=total,
        page=skip // limit + 1,
        size=limit,
        pages=(total + limit - 1) // limit
    )

    # TODO: Add encryption for GET endpoints
    # from app.core.encryption import encrypt_response_data
    # return encrypt_response_data(response)

    return response


@router.get(
    "/{%raw%}{{%endraw%}{{ singular_name }}_id{%raw%}}{%endraw%}",
    status_code=status.HTTP_200_OK,
)
async def get_{{ singular_name }}(
    {{ singular_name }}_id: int,
    db: Session = Depends(get_db),
    # current_user: dict = Depends(require_auth)  # TODO: Add auth
):
    """Get single {{ singular_name }} by ID"""
    {{ singular_name }} = {{ singular_name }}_service.get_{{ singular_name }}(
        db,
        {{ singular_name }}_id,
        current_user_role_level=100,  # TODO: Use current_user["role_level"]
        current_user_id=1  # TODO: Use current_user["user_id"]
    )

    response = DataResponse(
        success=True,
        message="{{ pascal_name }} retrieved successfully",
        data={{ singular_name }}
    )

    # TODO: Add encryption for GET endpoints
    return response


@router.post(
    "/",
    response_model=DataResponse[{{ pascal_name }}],
    status_code=status.HTTP_201_CREATED,
)
async def create_{{ singular_name }}(
    {{ singular_name }}: {{ pascal_name }}Create,
    db: Session = Depends(get_db),
    # current_user: dict = Depends(require_auth)  # TODO: Add auth
):
    """Create new {{ singular_name }}"""
    new_{{ singular_name }} = {{ singular_name }}_service.create_{{ singular_name }}(
        db,
        {{ singular_name }},
        current_user_role_level=100  # TODO: Use current_user["role_level"]
    )

    return DataResponse(
        success=True,
        message="{{ pascal_name }} created successfully",
        data=new_{{ singular_name }}
    )


@router.put(
    "/{%raw%}{{%endraw%}{{ singular_name }}_id{%raw%}}{%endraw%}",
    response_model=DataResponse[{{ pascal_name }}],
    status_code=status.HTTP_200_OK,
)
async def update_{{ singular_name }}(
    {{ singular_name }}_id: int,
    {{ singular_name }}: {{ pascal_name }}Update,
    db: Session = Depends(get_db),
    # current_user: dict = Depends(require_auth)  # TODO: Add auth
):
    """Update existing {{ singular_name }}"""
    updated_{{ singular_name }} = {{ singular_name }}_service.update_{{ singular_name }}(
        db,
        {{ singular_name }}_id,
        {{ singular_name }},
        current_user_role_level=100,  # TODO: Use current_user["role_level"]
        current_user_id=1  # TODO: Use current_user["user_id"]
    )

    return DataResponse(
        success=True,
        message="{{ pascal_name }} updated successfully",
        data=updated_{{ singular_name }}
    )


@router.delete(
    "/{%raw%}{{%endraw%}{{ singular_name }}_id{%raw%}}{%endraw%}",
    status_code=status.HTTP_204_NO_CONTENT,
)
async def delete_{{ singular_name }}(
    {{ singular_name }}_id: int,
    db: Session = Depends(get_db),
    # current_user: dict = Depends(require_auth)  # TODO: Add auth
):
    """Delete {{ singular_name }}"""
    {{ singular_name }}_service.delete_{{ singular_name }}(
        db,
        {{ singular_name }}_id,
        current_user_role_level=100  # TODO: Use current_user["role_level"]
    )

    return None
