const express = require('express');
const router = express.Router();
const ctrl = require('../controllers/scheduleController');
const { createRequirePage } = require('../../../shared/middleware/pagePermission');
const { getDb } = require('../dbPool');
const requirePage = createRequirePage(getDb);

// 제품유형
router.get('/product-types', ctrl.getProductTypes);

// 표준공정 자동 생성
router.post('/generate-from-template', requirePage('factory_schedules'), ctrl.generateFromTemplate);

// 공정 단계
router.get('/phases', ctrl.getPhases);
router.post('/phases', requirePage('factory_schedules'), ctrl.createPhase);
router.put('/phases/:id', requirePage('factory_schedules'), ctrl.updatePhase);

// 작업 템플릿
router.get('/templates', ctrl.getTemplates);

// 공정표 항목
router.get('/entries', ctrl.getEntries);
router.get('/entries/gantt', ctrl.getGanttData);
router.post('/entries', requirePage('factory_schedules'), ctrl.createEntry);
router.post('/entries/batch', requirePage('factory_schedules'), ctrl.createBatchEntries);
router.put('/entries/:id', requirePage('factory_schedules'), ctrl.updateEntry);
router.put('/entries/:id/progress', requirePage('factory_schedules'), ctrl.updateProgress);
router.delete('/entries/:id', requirePage('factory_schedules'), ctrl.deleteEntry);

// 의존관계
router.post('/entries/:id/dependencies', requirePage('factory_schedules'), ctrl.addDependency);
router.delete('/entries/:id/dependencies/:depId', requirePage('factory_schedules'), ctrl.removeDependency);

// 마일스톤
router.get('/milestones', ctrl.getMilestones);
router.post('/milestones', requirePage('factory_schedules'), ctrl.createMilestone);
router.put('/milestones/:id', requirePage('factory_schedules'), ctrl.updateMilestone);
router.delete('/milestones/:id', requirePage('factory_schedules'), ctrl.deleteMilestone);

// 부적합 연동
router.get('/nonconformance', ctrl.getNonconformance);

module.exports = router;
