const PermissionsService = require('../services/permissions.service')
const ResponseHandle = require('../utils/responseHandle')

class PermissionsController {
  // Get all permissions
  static async getPermissionsList(req, res) {
    try {
      const permissionsList = await PermissionsService.getPermissionsList()
      ResponseHandle.success(res, permissionsList, 'Permissions retrieved successfully')
    } catch (error) {
      ResponseHandle.error(res, 500, 'Internal Server Error', error)
    }
  }

  // Get permission by ID
  static async getPermissionById(req, res) {
    try {
      const { permission_id } = req.params
      const permission = await PermissionsService.getPermissionById(permission_id)
      if (!permission) {
        return ResponseHandle.error(res, 404, 'Permission not found')
      }
      ResponseHandle.success(res, permission, 'Permission retrieved successfully')
    } catch (error) {
      ResponseHandle.error(res, error.status, error.message)
    }
  }

  // Create a new permission
  static async createPermission(req, res) {
    try {
      const data = req.body
      const permission = await PermissionsService.createPermission(data)
      ResponseHandle.success(res, permission, 'Permission created successfully')
    } catch (error) {
      ResponseHandle.error(res, 500, 'Internal Server Error', error)
    }
  }

  // Update an existing permission
  static async updatePermission(req, res) {
    try {
      const { permission_id } = req.body
      const data = req.body
      const updatedPermission = await PermissionsService.updatePermission(permission_id, data)
      ResponseHandle.success(res, updatedPermission, 'Permission updated successfully')
    } catch (error) {
      ResponseHandle.error(res, error.status, error.message)
    }
  }

  // Delete a permission
  static async deletePermission(req, res) {
    try {
      const { permission_id } = req.params
      await PermissionsService.deletePermission(permission_id)
      ResponseHandle.success(res, null, 'Permission deleted successfully')
    } catch (error) {
      ResponseHandle.error(res, error.status, error.message)
    }
  }
}

module.exports = PermissionsController
