import * as companyReminderService from '../services/company-reminder.service.js';

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

    const reminders = await companyReminderService.getRemindersByCompanyId(companyId);

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

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

    const reminder = await companyReminderService.createReminder(companyId, { description, dueDate, isChecked }, { userId, ipAddress: req.ip, userAgent: req.headers['user-agent'] });

    res.status(201).json({
      success: true,
      data: reminder,
      message: 'Reminder bol pridaný',
    });
  } catch (error) {
    next(error);
  }
};

export const updateCompanyReminder = async (req, res, next) => {
  try {
    const userId = req.userId;
    const { companyId, reminderId } = req.params;
    const { description, dueDate, isChecked } = req.body;

    const reminder = await companyReminderService.updateReminder(companyId, reminderId, { description, dueDate, isChecked }, { userId, ipAddress: req.ip, userAgent: req.headers['user-agent'] });

    res.status(200).json({
      success: true,
      data: reminder,
      message: 'Reminder bol aktualizovaný',
    });
  } catch (error) {
    next(error);
  }
};

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

    const result = await companyReminderService.deleteReminder(companyId, reminderId, { userId, ipAddress: req.ip, userAgent: req.headers['user-agent'] });

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

export const getReminderSummary = async (req, res, next) => {
  try {
    const userId = req.user?.id;
    const userRole = req.user?.role;
    const summary = await companyReminderService.getReminderSummary(userId, userRole);
    res.status(200).json({
      success: true,
      data: summary,
    });
  } catch (error) {
    next(error);
  }
};

export const getReminderCountsByCompany = async (req, res, next) => {
  try {
    const userId = req.user?.id;
    const userRole = req.user?.role;
    const counts = await companyReminderService.getReminderCountsByCompany(userId, userRole);
    res.status(200).json({
      success: true,
      data: counts,
    });
  } catch (error) {
    next(error);
  }
};

export const getUpcomingReminders = async (req, res, next) => {
  try {
    const userId = req.user?.id;
    const userRole = req.user?.role;
    const reminders = await companyReminderService.getUpcomingReminders(userId, userRole);
    res.status(200).json({
      success: true,
      count: reminders.length,
      data: reminders,
    });
  } catch (error) {
    next(error);
  }
};
