/*
|--------------------------------------------------------------------------
| Routes file
|--------------------------------------------------------------------------
|
| The routes file is used for defining the HTTP routes.
|
*/

import router from '@adonisjs/core/services/router'
import { middleware } from '#start/kernel'
import { throttle } from '#start/limiter'

const AuthController = () => import('#controllers/auth_controller')
const RestaurantMenuItemsController = () => import('#controllers/restaurant_menu_items_controller')
const RestaurantMenusController = () => import('#controllers/restaurant_menus_controller')
const CategoriesController = () => import('#controllers/categories_controller')
const UsersController = () => import('#controllers/users_controller')
const ImagesController = () => import('#controllers/images_controller')
const RoomReservationsController = () => import('#controllers/room_reservations_controller')

router
  .group(() => {
    // Auth routes
    router.group(() => {
      router.post('/register', [AuthController, 'register']).as('auth.register').use(throttle).use(middleware.auth())
      router.post('/login', [AuthController, 'login']).as('auth.login').use(throttle)
      router.delete('/logout', [AuthController, 'logout']).as('auth.logout').use(middleware.auth())
      router.get('/me', [AuthController, 'me']).as('auth.me').use(middleware.auth()).use(throttle)
    })

    // RestaurantMenuItem routes
    router
      .group(() => {
        router.get('/', [RestaurantMenuItemsController, 'getAll']).as('menu-items.getAll')
        router
          .post('/', [RestaurantMenuItemsController, 'getOne'])
          .as('menu-items.getOne')
          .use([middleware.auth(), middleware.admin()])
        router
          .post('/create', [RestaurantMenuItemsController, 'create'])
          .as('menu-items.create')
          .use([middleware.auth(), middleware.admin()])
        router
          .put('/', [RestaurantMenuItemsController, 'update'])
          .as('menu-items.update')
          .use([middleware.auth(), middleware.admin()])
        router
          .delete('/', [RestaurantMenuItemsController, 'delete'])
          .as('menu-items.delete')
          .use([middleware.auth(), middleware.admin(), throttle])
        router
          .post('/attach', [RestaurantMenuItemsController, 'connectToMenu'])
          .as('menu-items.attach')
          .use([middleware.auth(), middleware.admin()])
      })
      .prefix('/menu-items')
    // RestaurantMenu routes
    router
      .group(() => {
        router.get('/', [RestaurantMenusController, 'getAll']).as('menus.getAll')
        router
          .post('/', [RestaurantMenusController, 'getOne'])
          .as('menus.getOne')
          .use([middleware.auth(), middleware.admin()])
        router
          .post('/create', [RestaurantMenusController, 'create'])
          .as('menus.create')
          .use([middleware.auth(), middleware.admin()])
        router
          .put('/', [RestaurantMenusController, 'update'])
          .as('menus.update')
          .use([middleware.auth(), middleware.admin()])
        router
          .delete('/', [RestaurantMenusController, 'delete'])
          .as('menus.delete')
          .use([middleware.auth(), middleware.admin(), throttle])
      })
      .prefix('/menus')

    // Category routes
    router
      .group(() => {
        router.get('/', [CategoriesController, 'getAll']).as('categories.getAll')
        router
          .post('/', [CategoriesController, 'getOne'])
          .as('categories.getOne')
          .use([middleware.auth(), middleware.admin()])
        router
          .post('/create', [CategoriesController, 'create'])
          .as('categories.create')
          .use([middleware.auth(), middleware.admin()])
        router
          .put('/', [CategoriesController, 'update'])
          .as('categories.update')
          .use([middleware.auth(), middleware.admin()])
        router
          .delete('/', [CategoriesController, 'delete'])
          .as('categories.delete')
          .use([middleware.auth(), middleware.admin(), throttle])
      })
      .prefix('/categories')

    // User routes
    router
      .group(() => {
        router.get('/', [UsersController, 'getAll']).as('users.getAll').use(middleware.auth())
        router
          .post('/', [UsersController, 'create'])
          .as('users.create')
          .use([middleware.auth(), middleware.admin()])
        router
          .post('/get-one', [UsersController, 'getOne'])
          .as('users.getOne')
          .use([middleware.auth(), middleware.admin()])
        router
          .put('/', [UsersController, 'update'])
          .as('users.update')
          .use([middleware.auth(), middleware.admin()])
        router
          .delete('/', [UsersController, 'delete'])
          .as('users.delete')
          .use([middleware.auth(), middleware.admin()])
      })
      .prefix('/users')

    // Image routes
    router
      .group(() => {
        router
          .get('/', [ImagesController, 'list'])
          .as('images.list')
          //.use([middleware.auth(), middleware.admin()])
        router
          .post('/upload', [ImagesController, 'upload'])
          .as('images.upload')
          .use([middleware.auth(), middleware.admin()])
        router
          .get('/:filename', [ImagesController, 'show'])
          .as('images.show')
      })
      .prefix('/images')

    // Room and Reservation routes
    router
      .group(() => {
        // Rooms
        router.get('/rooms', [RoomReservationsController, 'listRooms']).as('rooms.list')
        router
          .post('/rooms', [RoomReservationsController, 'createRoom'])
          .as('rooms.create')
          .use([middleware.auth(), middleware.admin()])
        router
          .put('/rooms', [RoomReservationsController, 'updateRoom'])
          .as('rooms.update')
          .use([middleware.auth(), middleware.admin()])
        router
          .delete('/rooms', [RoomReservationsController, 'deleteRoom'])
          .as('rooms.delete')
          .use([middleware.auth(), middleware.admin()])

        // Reservations
        router.get('/reservations', [RoomReservationsController, 'index']).as('reservations.index')
        router
          .post('/reservations', [RoomReservationsController, 'store'])
          .as('reservations.store')
          .use([middleware.auth(), middleware.admin()])
        router
          .put('/reservations', [RoomReservationsController, 'update'])
          .as('reservations.update')
          .use([middleware.auth(), middleware.admin()])
        router
          .delete('/reservations', [RoomReservationsController, 'destroy'])
          .as('reservations.destroy')
          .use([middleware.auth(), middleware.admin()])
      })
      .prefix('/room-reservations')
  })
  .prefix('/api')
