const {
  getAllShiftDb,
  getShiftByIdDb,
  insertShiftDb,
  updateShiftDb,
  deleteShiftDb
} = require('../db/shift.db');
const { ErrorHandler } = require('../helpers/error');

class ShiftService {
  // Get all Shift
  static async getAllShift(param) {
    try {
      const results = await getAllShiftDb(param);

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

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

  // Get Shift by ID
  static async getShiftById(id) {
    try {
      const result = await getShiftByIdDb(id);

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

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

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

      const result = await insertShiftDb(data);

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

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

      const dataExist = await getShiftByIdDb(id);

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

      const result = await updateShiftDb(id, data);

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

  // Soft delete Shift
  static async deleteShift(id, userId) {
    try {
      const dataExist = await getShiftByIdDb(id);

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

      const result = await deleteShiftDb(id, userId);

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

module.exports = ShiftService;
