/**
 * Equipment Routes
 */

const express = require('express');
const router = express.Router();
const equipmentController = require('../controllers/equipmentController');
const { requireAuth } = require('../middleware/auth');
const { createRequirePage } = require('../../../shared/middleware/pagePermission');
const { getPool } = require('../../shared/config/database');
const requirePage = createRequirePage(getPool);
const upload = require('../middleware/upload');

// 고정 경로를 /:id 보다 먼저 등록
router.get('/types', requireAuth, equipmentController.getTypes);
router.get('/next-code', requireAuth, equipmentController.getNextCode);
router.get('/workplace/:workplaceId', requireAuth, equipmentController.getByWorkplace);
// 사진 삭제 (photo_id만으로)
router.delete('/photos/:photoId', requirePage('tkuser.equipments'), equipmentController.deletePhoto);

// 기본 CRUD
router.get('/', requireAuth, equipmentController.getAll);
router.get('/:id', requireAuth, equipmentController.getById);
router.post('/', requirePage('tkuser.equipments'), equipmentController.create);
router.put('/:id', requirePage('tkuser.equipments'), equipmentController.update);
router.delete('/:id', requirePage('tkuser.equipments'), equipmentController.remove);

// 지도 위치
router.patch('/:id/map-position', requirePage('tkuser.equipments'), equipmentController.updateMapPosition);

// 사진
router.post('/:id/photos', requirePage('tkuser.equipments'), upload.single('photo'), equipmentController.addPhoto);
router.get('/:id/photos', requireAuth, equipmentController.getPhotos);

module.exports = router;
