import * as eventService from '../services/event.service.js';
import { triggerSingleEventNotification } from '../cron/index.js';

/**
 * Get calendar data (events + todos) by month
 * GET /api/events?year=2024&month=1
 */
export const getCalendarData = async (req, res, next) => {
  try {
    const { year, month } = req.query;
    const userId = req.userId;
    const isAdmin = req.user?.role === 'admin';

    const currentDate = new Date();
    const queryYear = year ? parseInt(year) : currentDate.getFullYear();
    const queryMonth = month ? parseInt(month) : currentDate.getMonth() + 1;

    const data = await eventService.getCalendarData(queryYear, queryMonth, userId, isAdmin);

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

/**
 * Get event by ID
 * GET /api/events/:eventId
 */
export const getEventById = async (req, res, next) => {
  try {
    const { eventId } = req.params;
    const userId = req.userId;
    const isAdmin = req.user?.role === 'admin';

    const event = await eventService.getEventById(eventId, userId, isAdmin);

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

/**
 * Create a new event (admin only)
 * POST /api/events
 */
export const createEvent = async (req, res, next) => {
  try {
    const userId = req.userId;
    const data = req.body;

    const event = await eventService.createEvent(userId, data);

    res.status(201).json({
      success: true,
      data: event,
      message: 'Event bol vytvorený',
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Update an event (admin only)
 * PUT /api/events/:eventId
 */
export const updateEvent = async (req, res, next) => {
  try {
    const { eventId } = req.params;
    const data = req.body;

    const event = await eventService.updateEvent(eventId, data);

    res.status(200).json({
      success: true,
      data: event,
      message: 'Event bol upravený',
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Delete an event (admin only)
 * DELETE /api/events/:eventId
 */
export const deleteEvent = async (req, res, next) => {
  try {
    const { eventId } = req.params;

    const result = await eventService.deleteEvent(eventId);

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

/**
 * Send notification for an event to all assigned users (admin only)
 * POST /api/events/:eventId/notify
 */
export const sendEventNotification = async (req, res, next) => {
  try {
    const { eventId } = req.params;
    const adminUserId = req.userId;

    const stats = await triggerSingleEventNotification(eventId, adminUserId);

    res.status(200).json({
      success: true,
      data: stats,
      message: `Notifikácie pre "${stats.eventTitle}" odoslané: ${stats.sent}, neúspešné: ${stats.failed}, preskočené: ${stats.skipped}`,
    });
  } catch (error) {
    next(error);
  }
};

/**
 * Get count of events and todos for today (for calendar badge)
 * GET /api/events/today-count
 */
export const getTodayCount = async (req, res, next) => {
  try {
    const userId = req.userId;
    const isAdmin = req.user?.role === 'admin' || req.user?.role === 'team_leader';

    const counts = await eventService.getTodayCalendarCount(userId, isAdmin);

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