const RolesService = require('../services/roles.service');
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
const { updateRolesSchema, insertRolesSchema } = require('../validate/roles.schema');

class RolesController {
  // Get all Roles
  static async getAll(req, res) {
    const queryParams = req.query;

    const results = await RolesService.getAllRoles(queryParams);
    const response = await setResponsePaging(queryParams, results, 'Roles found')

    res.status(response.statusCode).json(response);
  }

  // Get Roles by ID
  static async getById(req, res) {
    const { id } = req.params;

    const results = await RolesService.getRolesById(id);
    const response = await setResponse(results, 'Roles found')

    res.status(response.statusCode).json(response);
  }

  // Create Roles
  static async create(req, res) {
    const { error, value } = await checkValidate(insertRolesSchema, req)

    if (error) {
      return res.status(400).json(setResponse(error, 'Validation failed', 400));
    }

    value.userId = req.user.user_id

    const results = await RolesService.createRoles(value);
    const response = await setResponse(results, 'Roles created successfully')

    return res.status(response.statusCode).json(response);
  }

  // Update Roles
  static async update(req, res) {
    const { id } = req.params;

    const { error, value } = checkValidate(updateRolesSchema, req)

    if (error) {
      return res.status(400).json(setResponse(error, 'Validation failed', 400));
    }

    value.userId = req.user.user_id

    const results = await RolesService.updateRoles(id, value);
    const response = await setResponse(results, 'Roles updated successfully')

    res.status(response.statusCode).json(response);
  }

  // Soft delete Roles
  static async delete(req, res) {
    const { id } = req.params;

    const results = await RolesService.deleteRoles(id, req.user.user_id);
    const response = await setResponse(results, 'Roles deleted successfully')

    res.status(response.statusCode).json(response);
  }
}

module.exports = RolesController;
