"""
Error handler controller for managing application exceptions.

This module provides custom error handlers for different HTTP status codes.
"""
from fastapi import HTTPException, Request
from fastapi.responses import JSONResponse

from src.server.utils.template_helpers import render_template


async def not_found_handler(request: Request, exc: HTTPException):
    """Custom 404 handler."""
    if request.url.path.startswith("/api/"):
        return JSONResponse(
            status_code=404,
            content={"detail": "API endpoint not found"}
        )
    return render_template(
        "error.html",
        request,
        context={"error": "Page not found", "status_code": 404},
        title="404 - Not Found"
    )


async def server_error_handler(request: Request, exc: Exception):
    """Custom 500 handler."""
    if request.url.path.startswith("/api/"):
        return JSONResponse(
            status_code=500,
            content={"detail": "Internal server error"}
        )
    return render_template(
        "error.html",
        request,
        context={"error": "Internal server error", "status_code": 500},
        title="500 - Server Error"
    )
