const express = require('express');
const router = express.Router();
const visitRequestController = require('../controllers/visitRequestController');
const { requireAuth, requirePage } = require('../middleware/auth');

router.use(requireAuth);

// Visit requests CRUD
router.post('/requests', requirePage('safety_visit_request'), visitRequestController.createVisitRequest);
router.get('/requests', visitRequestController.getAllVisitRequests);
router.get('/requests/:id', visitRequestController.getVisitRequestById);
router.put('/requests/:id', requirePage('safety_visit_request'), visitRequestController.updateVisitRequest);
router.delete('/requests/:id', requirePage('safety_visit_request'), visitRequestController.deleteVisitRequest);
router.put('/requests/:id/approve', requirePage('safety_visit_management'), visitRequestController.approveVisitRequest);
router.put('/requests/:id/reject', requirePage('safety_visit_management'), visitRequestController.rejectVisitRequest);

// Check-in / Check-out
router.put('/requests/:id/check-in', visitRequestController.checkIn);
router.put('/requests/:id/check-out', visitRequestController.checkOut);

// Entry Dashboard (통합 출입 현황)
router.get('/entry-dashboard', visitRequestController.getEntryDashboard);
router.get('/entry-dashboard/stats', visitRequestController.getEntryStats);

// Categories & Workplaces
router.get('/categories', visitRequestController.getAllCategories);
router.get('/workplaces', visitRequestController.getWorkplaces);

// Departments
router.get('/departments', visitRequestController.getDepartments);

// Visit purposes
router.get('/purposes', visitRequestController.getAllVisitPurposes);
router.get('/purposes/active', visitRequestController.getActiveVisitPurposes);
router.post('/purposes', requirePage('safety_visit_management'), visitRequestController.createVisitPurpose);
router.put('/purposes/:id', requirePage('safety_visit_management'), visitRequestController.updateVisitPurpose);
router.delete('/purposes/:id', requirePage('safety_visit_management'), visitRequestController.deleteVisitPurpose);

// Training records
router.post('/training', requirePage('safety_visit_management'), visitRequestController.createTrainingRecord);
router.get('/training', visitRequestController.getTrainingRecords);
router.get('/training/request/:requestId', visitRequestController.getTrainingRecordByRequestId);
router.put('/training/:id', requirePage('safety_visit_management'), visitRequestController.updateTrainingRecord);
router.delete('/training/:id', requirePage('safety_visit_management'), visitRequestController.deleteTrainingRecord);
router.post('/training/:id/complete', requirePage('safety_visit_management'), visitRequestController.completeTraining);

module.exports = router;
