const ShiftService = require('../services/shift.service');
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
const { updateShiftSchema, insertShiftSchema } = require('../validate/shift.schema');

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

    const results = await ShiftService.getAllShift(queryParams);
    const response = await setResponsePaging(queryParams, results, 'Shift found')

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

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

    const results = await ShiftService.getShiftById(id);
    const response = await setResponse(results, 'Shift found')

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

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

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

    value.userId = req.user.user_id

    const results = await ShiftService.createShift(value);
    const response = await setResponse(results, 'Shift created successfully')

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

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

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

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

    value.userId = req.user.user_id

    const results = await ShiftService.updateShift(id, value);
    const response = await setResponse(results, 'Shift updated successfully')

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

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

    const results = await ShiftService.deleteShift(id, req.user.user_id);
    const response = await setResponse(results, 'Shift deleted successfully')

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

module.exports = ShiftController;
