import * as noteService from '../services/note.service.js';

export const getCompanyNotes = async (req, res, next) => {
  try {
    const { companyId } = req.params;

    const notes = await noteService.getNotesByCompanyId(companyId);

    res.status(200).json({
      success: true,
      count: notes.length,
      data: notes,
    });
  } catch (error) {
    next(error);
  }
};

export const addCompanyNote = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { companyId } = req.params;
    const { content, dueDate } = req.body;

    const note = await noteService.createNote(userId, {
      content,
      companyId,
      dueDate,
    });

    res.status(201).json({
      success: true,
      data: note,
      message: 'Poznámka bola pridaná',
    });
  } catch (error) {
    next(error);
  }
};

export const updateCompanyNote = async (req, res, next) => {
  try {
    const { noteId } = req.params;
    const { content, dueDate } = req.body;

    const note = await noteService.updateNote(noteId, {
      content,
      dueDate,
    });

    res.status(200).json({
      success: true,
      data: note,
      message: 'Poznámka bola aktualizovaná',
    });
  } catch (error) {
    next(error);
  }
};

export const deleteCompanyNote = async (req, res, next) => {
  try {
    const { noteId } = req.params;

    const result = await noteService.deleteNote(noteId);

    res.status(200).json({
      success: true,
      message: result.message,
    });
  } catch (error) {
    next(error);
  }
};
