const RolesService = require('../services/roles.service')
const ResponseHandle = require('../utils/responseHandle')

class RolesController {
  static async getRolesList(req, res) {
    try {
      const rolesList = await RolesService.getRolesList()
      ResponseHandle.success(res, rolesList, 'Roles retrieved successfully')
    } catch (error) {
      ResponseHandle.error(res, 500, 'Internal Server Error', error)
    }
  }

  // Get role by ID
  static async getRoleById(req, res) {
    try {
      const { role_id } = req.params
      const role = await RolesService.getRoleById(role_id)
      if (!role) {
        return ResponseHandle.error(res, 404, 'Role not found')
      }
      ResponseHandle.success(res, role, 'Role retrieved successfully')
    } catch (error) {
      ResponseHandle.error(res, error.status, error.message)
    }
  }

  // Create Role
  static async createRole(req, res) {
    try {
      const data = req.body
      const role = await RolesService.createRole(data)
      ResponseHandle.success(res, role, 'Role created successfully')
    } catch (error) {
      ResponseHandle.error(res, 500, 'Internal Server Error', error)
    }
  }

  // Update Role
  static async updateRole(req, res) {
    try {
      const { role_id, name } = req.body
      const updatedRole = await RolesService.updateRole(role_id, { name })
      ResponseHandle.success(res, updatedRole, 'Role updated successfully')
    } catch (error) {
      ResponseHandle.error(res, error.status, error.message)
    }
  }

  // Delete Role
  static async deleteRole(req, res) {
    try {
      const { role_id } = req.params
      await RolesService.deleteRole(role_id)
      ResponseHandle.success(res, null, 'Role deleted successfully')
    } catch (error) {
      ResponseHandle.error(res, error.status, error.message)
    }
  }
}

module.exports = RolesController
