const StatusService = require('../services/status.service');
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
const { insertStatusSchema, updateStatusSchema } = require('../validate/status.schema');

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

    const results = await StatusService.getAllStatus(queryParams);
    const response = await setResponsePaging(queryParams, results, 'Status found');

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

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

    const results = await StatusService.getStatusById(id);
    const response = await setResponse(results, 'Status found');

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

  // Create status
  static async create(req, res) {
    const { error, value } = await checkValidate(insertStatusSchema, req);

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

    value.userId = req.user.user_id;

    const results = await StatusService.createStatus(value);
    const response = await setResponse(results, 'Status created successfully');

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

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

    console.log("REQ BODY:", req.body);

    const { error, value } = await checkValidate(updateStatusSchema, req);

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

    value.userId = req.user.user_id;

    const results = await StatusService.updateStatus(id, value);
    const response = await setResponse(results, 'Status updated successfully');

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

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

    const results = await StatusService.deleteStatus(id, req.user.user_id);
    const response = await setResponse(results, 'Status deleted successfully');

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

module.exports = StatusController;
