const TagsService = require('../services/tags.service');
const { setResponse, setResponsePaging, checkValidate } = require('../helpers/utils');
const { insertTagsSchema, updateTagsSchema } = require('../validate/tags.schema');

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

    const results = await TagsService.getAllTags(queryParams);
    const response = await setResponsePaging(queryParams, results, 'Tags found')

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

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

    const results = await TagsService.getTagByID(id);
    const response = await setResponse(results, 'Tags found')

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

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

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

    value.userId = req.user.user_id

    const results = await TagsService.createTags(value);
    const response = await setResponse(results, 'Tags created successfully')

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

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

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

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

    value.userId = req.user.user_id

    const results = await TagsService.updateTags(id, value);
    const response = await setResponse(results, 'Tags updated successfully')

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

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

    const results = await TagsService.deleteTags(id, req.user.user_id);
    const response = await setResponse(results, 'Tags deleted successfully')

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

module.exports = TagsController;
