const {
  getAllScheduleDb,
  getScheduleByIdDb,
  insertScheduleDb,
  updateScheduleDb,
  deleteScheduleDb
} = require('../db/schedule.db');
const { ErrorHandler } = require('../helpers/error');

class ScheduleService {
  // Get all Schedule
  static async getAllSchedule(param) {
    try {
      const results = await getAllScheduleDb(param);

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

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

  // Get Schedule by ID
  static async getScheduleById(id) {
    try {
      const result = await getScheduleByIdDb(id);

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

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

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

      const result = await insertScheduleDb(data);

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

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

      const dataExist = await getScheduleByIdDb(id);

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

      const result = await updateScheduleDb(id, data);

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

  // Soft delete Schedule
  static async deleteSchedule(id, userId) {
    try {
      const dataExist = await getScheduleByIdDb(id);

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

      const result = await deleteScheduleDb(id, userId);

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

module.exports = ScheduleService;
