import * as serviceFolderService from '../services/service-folder.service.js';

export const getAllFolders = async (req, res, next) => {
  try {
    const folders = await serviceFolderService.getAllFolders();
    res.json({ data: folders });
  } catch (error) {
    next(error);
  }
};

export const getFolderById = async (req, res, next) => {
  try {
    const { folderId } = req.params;
    const folder = await serviceFolderService.getFolderById(folderId);
    res.json({ data: folder });
  } catch (error) {
    next(error);
  }
};

export const createFolder = async (req, res, next) => {
  try {
    const { name } = req.body;
    const userId = req.user.id;
    const folder = await serviceFolderService.createFolder({ name, userId });
    res.status(201).json({ data: folder });
  } catch (error) {
    next(error);
  }
};

export const updateFolder = async (req, res, next) => {
  try {
    const { folderId } = req.params;
    const { name } = req.body;
    const folder = await serviceFolderService.updateFolder(folderId, { name });
    res.json({ data: folder });
  } catch (error) {
    next(error);
  }
};

export const deleteFolder = async (req, res, next) => {
  try {
    const { folderId } = req.params;
    const result = await serviceFolderService.deleteFolder(folderId);
    res.json({ data: result });
  } catch (error) {
    next(error);
  }
};
