from fastapi import APIRouter, HTTPException
from resources import strings
from data_objects.routes import Routes
from starlette.status import HTTP_404_NOT_FOUND, HTTP_400_BAD_REQUEST, HTTP_204_NO_CONTENT

router = APIRouter()
route_data: Routes = Routes(0, 0)


@router.post("", status_code=201, summary="Create a new route.", description="This endpoint allows you to create a new route entry in the database. Note: Routes are automatically created when a new delivery is entered into the database.", responses={HTTP_400_BAD_REQUEST: {"description": strings.GENERIC_INVALID_DATA_ERROR}})
def create_route(new_route: Routes) -> None:
    """
    Args:
        new_route (route): The route data to be created.
    Example:
    ```
    HTTP POST /route
    {
        "route_id": 1,
        "delivery_id": 1
    }
    ```
    """
    invalid_data_error: HTTPException = HTTPException(
        status_code=HTTP_400_BAD_REQUEST, detail=strings.GENERIC_INVALID_DATA_ERROR)
    try:
        route_data.push_to_database(new_route)
    except Exception:
        raise invalid_data_error


@router.get("", status_code=200, summary="Fetch all route data.", description="This endpoint retrieves all route data from the database.", responses={HTTP_204_NO_CONTENT: {"description": strings.GENERIC_NO_CONTENT}})
def fetch_all_route() -> None:
    """
    Example:
    ```
    HTTP GET /route
    ```
    """
    no_content_message: HTTPException = HTTPException(
        status_code=HTTP_204_NO_CONTENT, description=strings.GENERIC_NO_CONTENT)
    route_list: list[Routes] = route_data.pull_from_database()
    if (len(route_list) == 0):
        raise no_content_message
    return route_list


@router.get("/{route_id}", status_code=200, summary="Fetch data for a single route.", description="This endpoint retrieves data for a specific route based on its unique ID.", responses={HTTP_404_NOT_FOUND: {"description": strings.GENERIC_DATA_NOT_FOUND_ERROR}})
def fetch_one_route(route_id: int) -> Routes:
    """
    Args:
        route_id (int): The ID of the route to retrieve.
    Example:
    ```
    HTTP GET /deliveries/1
    ```
    """
    not_found_error: HTTPException = HTTPException(
        status_code=HTTP_404_NOT_FOUND, detail=strings.GENERIC_DATA_NOT_FOUND_ERROR)
    route_list: list[Routes] = route_data.pull_from_database()
    for route in route_list:
        if (int(route.route_id) == route_id):
            return route
    raise not_found_error


@router.put("/{route_id}", status_code=202, summary="Update route data.", description="This endpoint allows you to update route data based on its unique ID.", responses={HTTP_400_BAD_REQUEST: {"description": strings.GENERIC_INVALID_DATA_ERROR}})
def update_route(route_id: int, updated_data: Routes) -> None:
    """
    Args:
        route_id (int): The ID of the route to update.
        updated_data (route): The updated route data.
    Example:
    ```
    HTTP PUT /deliveries/1
    {
        "route_id": 1,
        "route_id": 1
    }
    ```
    """
    bad_request_error: HTTPException = HTTPException(
        status_code=HTTP_400_BAD_REQUEST, detail=strings.GENERIC_INVALID_DATA_ERROR)
    try:
        route_data.update_in_database(route_id, updated_data)
    except Exception:
        raise bad_request_error


@router.delete("/{route_id}", status_code=200, summary="Delete a route.", description="This endpoint allows you to delete a specific route based on its unique ID.", responses={HTTP_404_NOT_FOUND: {"description": strings.GENERIC_DATA_NOT_FOUND_ERROR}})
def delete_route(route_id: int) -> None:
    """
    Args:
        route_id (int): The ID of the route to delete.
    Example:
    ```
    HTTP DELETE /route/1
    ```
    """
    data_not_found_error: HTTPException = HTTPException(
        status_code=HTTP_404_NOT_FOUND, detail=strings.GENERIC_DATA_NOT_FOUND_ERROR)
    try:
        route_data.remove_from_database(route_id)
    except Exception:
        raise data_not_found_error
