const ScheduleService = require('../services/schedule.service');
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
const { updateScheduleSchema, insertScheduleSchema } = require('../validate/schedule.schema');

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

    const results = await ScheduleService.getAllSchedule(queryParams);
    const response = await setResponsePaging(queryParams, results, 'Schedule found')

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

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

    const results = await ScheduleService.getScheduleById(id);
    const response = await setResponse(results, 'Schedule found')

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

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

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

    value.userId = req.user.user_id

    const results = await ScheduleService.insertScheduleDb(value);
    const response = await setResponse(results, 'Schedule created successfully')

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

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

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

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

    value.userId = req.user.user_id

    const results = await ScheduleService.updateSchedule(id, value);
    const response = await setResponse(results, 'Schedule updated successfully')

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

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

    const results = await ScheduleService.deleteSchedule(id, req.user.user_id);
    const response = await setResponse(results, 'Schedule deleted successfully')

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

module.exports = ScheduleController;
