import StatusCodes from 'http-status-codes'
import { Request, Response, Router } from 'express'
import { ApiError, notFoundError, paramMissingError } from '@shared/responseTypes'
import { IBikePoint } from '@entities/BikePoint'
import BikePointDao from '@daos/BikePoint.ts/BikePointDao'

const router = Router()
const bikePointDao = new BikePointDao()
const { NOT_FOUND, OK } = StatusCodes

interface IBikePointsResponse {
    bikePoints: IBikePoint[]
}

interface IBikePointResponse {
    bikePoint: IBikePoint
}



/******************************************************************************
 *         Get Bike Points - "GET /api/bike-points/all"
 ******************************************************************************/

router.get('/all', async (req: Request, res: Response<IBikePointsResponse | ApiError>) => {
    const bikePoints = await bikePointDao.getAll()
    return res.status(OK).json({bikePoints})
})



/******************************************************************************
 *         Get single Bike Point by id - "GET /api/bike-points/:bikePointId"
 ******************************************************************************/

router.get('/:bikePointId', async (req: Request, res: Response<IBikePointResponse | ApiError>) => {
    const { bikePointId } = req.params

    if (!bikePointId) {
        return res.status(StatusCodes.BAD_REQUEST).json({error: paramMissingError})
    }

    const bikePoint = await bikePointDao.getById(bikePointId)

    if (!bikePoint) {
        return res.status(NOT_FOUND).json({error: notFoundError})
    }

    return res.status(OK).json({bikePoint})
})



/******************************************************************************
 *                                 Export
 ******************************************************************************/

export default router
