// routes/calendarRoutes.js
const express = require('express');
const router = express.Router();
const db = require('../models');
const CalendarEvent = db.CalendarEvent;

// Create event
router.post('/', async (req, res) => {
  try {
    const { title, startDate, endDate, type } = req.body;
    const newEvent = await CalendarEvent.create({ title, startDate, endDate, type });
    res.status(201).json(newEvent);
  } catch (err) {
    console.error('Create event error:', err);
    res.status(500).json({ message: 'Failed to create event' });
  }
});

// Get all events (manual only, no auto-delete)
router.get('/', async (req, res) => {
  try {
    const events = await CalendarEvent.findAll({
      order: [['startDate', 'ASC']]
    });
    res.json(events);
  } catch (err) {
    console.error('Error fetching events:', err);
    res.status(500).json({ message: 'Error fetching events' });
  }
});

// Delete event manually
router.delete('/:id', async (req, res) => {
  try {
    const event = await CalendarEvent.findByPk(req.params.id);
    if (!event) return res.status(404).json({ message: 'Event not found' });

    await event.destroy();
    res.json({ message: 'Deleted successfully' });
  } catch (err) {
    console.error('Delete error:', err);
    res.status(500).json({ message: 'Server error' });
  }
});

module.exports = router;
