import * as noteService from '../services/note.service.js';
import { formatErrorResponse } from '../utils/errors.js';

/**
 * Get all notes
 * GET /api/notes?search=query&companyId=xxx&projectId=xxx&todoId=xxx&contactId=xxx
 */
export const getAllNotes = async (req, res) => {
  try {
    const { search, companyId, projectId, todoId, contactId } = req.query;

    const filters = {
      searchTerm: search,
      companyId,
      projectId,
      todoId,
      contactId,
    };

    const notes = await noteService.getAllNotes(filters);

    res.status(200).json({
      success: true,
      count: notes.length,
      data: notes,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

/**
 * Get note by ID
 * GET /api/notes/:noteId
 */
export const getNoteById = async (req, res) => {
  try {
    const { noteId } = req.params;

    const note = await noteService.getNoteById(noteId);

    res.status(200).json({
      success: true,
      data: note,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

/**
 * Create new note
 * POST /api/notes
 * Body: { title, content, companyId, projectId, todoId, contactId }
 */
export const createNote = async (req, res) => {
  try {
    const userId = req.userId;
    const data = req.body;

    const note = await noteService.createNote(userId, data);

    res.status(201).json({
      success: true,
      data: note,
      message: 'Poznámka bola vytvorená',
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

/**
 * Update note
 * PATCH /api/notes/:noteId
 * Body: { title, content, companyId, projectId, todoId, contactId }
 */
export const updateNote = async (req, res) => {
  try {
    const { noteId } = req.params;
    const data = req.body;

    const note = await noteService.updateNote(noteId, data);

    res.status(200).json({
      success: true,
      data: note,
      message: 'Poznámka bola aktualizovaná',
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

/**
 * Delete note
 * DELETE /api/notes/:noteId
 */
export const deleteNote = async (req, res) => {
  try {
    const { noteId } = req.params;

    const result = await noteService.deleteNote(noteId);

    res.status(200).json({
      success: true,
      message: result.message,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

/**
 * Get upcoming reminders for current user
 * GET /api/notes/my-reminders
 */
export const getMyReminders = async (req, res) => {
  try {
    const userId = req.userId;

    const reminders = await noteService.getUpcomingRemindersForUser(userId);

    res.status(200).json({
      success: true,
      count: reminders.length,
      data: reminders,
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};

/**
 * Mark reminder as sent
 * POST /api/notes/:noteId/mark-reminder-sent
 */
export const markReminderSent = async (req, res) => {
  try {
    const { noteId } = req.params;

    const updated = await noteService.markReminderAsSent(noteId);

    res.status(200).json({
      success: true,
      data: updated,
      message: 'Reminder označený ako odoslaný',
    });
  } catch (error) {
    const errorResponse = formatErrorResponse(error, process.env.NODE_ENV === 'development');
    res.status(error.statusCode || 500).json(errorResponse);
  }
};
