from fastapi import APIRouter, Depends, HTTPException
from data_objects.drivers import Driver
from resources import strings
from starlette.status import HTTP_404_NOT_FOUND, HTTP_400_BAD_REQUEST, HTTP_204_NO_CONTENT

router = APIRouter()
drivers_data: Driver = Driver(0, 0)


@router.post("", status_code=201, summary="Create a new driver.", description="This endpoint allows you to create a new drivers entry in the database.", responses={HTTP_400_BAD_REQUEST: {"description": strings.GENERIC_INVALID_DATA_ERROR}})
def create_drivers(new_drivers: Driver) -> None:
    """
    Args:
        new_driver (drivers): The drivers data to be created.
    Example:
    ```
    HTTP POST /driver
    {
        "driver_id": 1,
        "route_id": 1
    }
    ```
    """
    invalid_data_error: HTTPException = HTTPException(
        status_code=HTTP_400_BAD_REQUEST, detail=strings.GENERIC_INVALID_DATA_ERROR)
    try:
        drivers_data.push_to_database(new_drivers)
    except Exception:
        raise invalid_data_error


@router.get("", status_code=200, summary="Fetch all drivers data.", description="This endpoint retrieves all drivers data from the database.", responses={204: {"description": strings.GENERIC_NO_CONTENT}})
def fetch_all_drivers(driver_list: list[Driver] = Depends(drivers_data.pull_from_database)) -> None:
    """
    Returns:
        list[drivers]: A list of drivers data.
    Example:
    ```
    HTTP GET /driver
    ```
    """
    no_content_message: HTTPException = HTTPException(
        status_code=HTTP_204_NO_CONTENT, detail=strings.GENERIC_NO_CONTENT)
    if (len(driver_list) == 0):
        raise no_content_message
    return driver_list


@router.get("/{driver_id}", status_code=200, summary="Fetch data for a single drivers.", description="This endpoint retrieves data for a specific drivers based on its unique ID.", responses={HTTP_404_NOT_FOUND: {"description": strings.GENERIC_DATA_NOT_FOUND_ERROR}})
def fetch_one_drivers(driver_id: int) -> Driver:
    """
    Args:
        drivers_id (int): The ID of the drivers to retrieve.
    Example:
    ```
    HTTP GET /deliveries/1
    ```
    """
    data_not_found_error: HTTPException = HTTPException(
        status_code=HTTP_404_NOT_FOUND, detail=strings.GENERIC_DATA_NOT_FOUND_ERROR)
    drivers_list: list[Driver] = drivers_data.pull_from_database()
    for driver in drivers_list:
        if (int(driver.drivers_id) == driver_id):
            return driver
    raise data_not_found_error


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


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