package com.rit.ems.controller;

import com.rit.ems.model.InstitutionalEvent;
import com.rit.ems.repository.InstitutionalEventRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/api/institutional-events")
public class InstitutionalEventController {

    @Autowired
    private InstitutionalEventRepository institutionalEventRepository;

    @GetMapping
    public List<InstitutionalEvent> getAllEvents() {
        return institutionalEventRepository.findAll();
    }

    @PostMapping
    public ResponseEntity<?> createEvent(@RequestBody InstitutionalEvent event) {
        if (event.getId() == null) {
            event.setId("ie-" + System.currentTimeMillis());
        }
        institutionalEventRepository.save(event);
        return ResponseEntity.ok(event);
    }

    @DeleteMapping("/{id}")
    public ResponseEntity<?> deleteEvent(@PathVariable String id) {
        institutionalEventRepository.deleteById(id);
        return ResponseEntity.ok(Map.of("message", "Institutional event deleted successfully"));
    }
}
