const {
  getAllRolesDb,
  getRolesByIdDb,
  insertRolesDb,
  updateRolesDb,
  deleteRolesDb
} = require('../db/roles.db');
const { ErrorHandler } = require('../helpers/error');

class RolesService {
  // Get all Roles
  static async getAllRoles(param) {
    try {
      const results = await getAllRolesDb(param);

      results.data.map(element => {
      });

      return results
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  // Get Roles by ID
  static async getRolesById(id) {
    try {
      const result = await getRolesByIdDb(id);

      if (result.length < 1) throw new ErrorHandler(404, 'Roles not found');

      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  // Create Roles
  static async createRoles(data) {
    try {
      if (!data || typeof data !== 'object') data = {};

      const result = await insertRolesDb(data);

      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  // Update Roles
  static async updateRoles(id, data) {
    try {
      if (!data || typeof data !== 'object') data = {};

      const dataExist = await getRolesByIdDb(id);

      if (dataExist.length < 1) {
        throw new ErrorHandler(404, 'Roles not found');
      }

      const result = await updateRolesDb(id, data);

      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }

  // Soft delete Roles
  static async deleteRoles(id, userId) {
    try {
      const dataExist = await getRolesByIdDb(id);

      if (dataExist.length < 1) {
        throw new ErrorHandler(404, 'Roles not found');
      }

      const result = await deleteRolesDb(id, userId);

      return result;
    } catch (error) {
      throw new ErrorHandler(error.statusCode, error.message);
    }
  }
}

module.exports = RolesService;
