const express = require('express');
const router = express.Router();
const eventController = require('../controllers/eventController');
const fileUploadController = require('../controllers/fileUploadController');
const { authenticateJWT } = require('../middleware/authMiddleware');
const requireFeature = require('../middleware/requireFeature');

// 이벤트 기본 CRUD
router.post('/', authenticateJWT, requireFeature('event_management'), eventController.getAllEvents);
router.get('/:id', authenticateJWT, requireFeature('event_management'), eventController.getEventById);
router.post('/create', authenticateJWT, requireFeature('event_management'), eventController.createEvent);
router.put('/:id', authenticateJWT, requireFeature('event_management'), eventController.updateEvent);
router.delete('/:id', authenticateJWT, requireFeature('event_management'), eventController.deleteEvent);
// 이벤트 완전 삭제 (하드 삭제)
router.delete('/:id/permanent', authenticateJWT, requireFeature('event_management'), eventController.deleteEventPermanently);

// 이벤트 참가자 관리
router.post('/:id/participants/list', authenticateJWT, requireFeature('event_management'), eventController.getEventParticipants);
router.post('/:id/participants', authenticateJWT, requireFeature('event_management'), eventController.addEventParticipant);
router.put('/:id/participants', authenticateJWT, requireFeature('event_management'), eventController.updateEventParticipant);
router.delete('/:id/participants', authenticateJWT, requireFeature('event_management'), eventController.deleteEventParticipant);

// [팀 생성 기능] 이벤트별 팀 배정 저장
router.post('/:id/teams', authenticateJWT, requireFeature('team_create'), eventController.createOrUpdateEventTeams);
router.get('/:id/teams', authenticateJWT, requireFeature('team_create'), eventController.getEventTeams);

// 이벤트 점수 관리
router.get('/:id/scores', authenticateJWT, requireFeature('event_management'), eventController.getEventScores);
router.post('/:id/scores', authenticateJWT, requireFeature('event_management'), eventController.addEventScore);
router.put('/:id/scores/:scoreId', authenticateJWT, requireFeature('event_management'), eventController.updateEventScore);
router.delete('/:id/scores/:scoreId', authenticateJWT, requireFeature('event_management'), eventController.deleteEventScore);
router.delete('/:id/participants/:participantId/scores', authenticateJWT, requireFeature('event_management'), eventController.deleteParticipantScores);

// 사용자별 이벤트 관리
router.get('/user/events', authenticateJWT, requireFeature('event_management'), eventController.getUserEvents);



// 공개 URL(publicHash) 재생성
router.patch('/:id/publicHash', eventController.regeneratePublicHash);

// 파일 업로드 관련 라우트
router.post('/upload', authenticateJWT, requireFeature('event_management'), fileUploadController.uploadEventFile);
router.post('/parse-preview', authenticateJWT, requireFeature('event_management'), fileUploadController.parseEventFile);
router.post('/save-file-data', authenticateJWT, requireFeature('event_management'), fileUploadController.saveEventFileData);
router.post('/delete-file', authenticateJWT, requireFeature('event_management'), fileUploadController.deleteUploadedFile);
// 업로드 저장 롤백
router.post('/rollback-import', authenticateJWT, requireFeature('event_management'), fileUploadController.rollbackEventImport);

module.exports = router;