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

// 보조 데이터
router.get('/consumable-items', ctrl.getConsumableItems);
router.put('/consumable-items/:id/photo', ctrl.updateItemPhoto);
router.get('/vendors', ctrl.getVendors);
router.get('/search', ctrl.search);

// 내 신청 (모바일용 페이지네이션) — /:id 보다 먼저 등록
router.get('/my-requests', ctrl.getMyRequests);

// 품목 등록 + 신청 동시 (트랜잭션)
router.post('/register-and-request', ctrl.registerAndRequest);
// 일괄 신청 (장바구니)
router.post('/bulk', ctrl.bulkCreate);

// 구매신청 CRUD
router.get('/', ctrl.getAll);
router.get('/:id', ctrl.getById);
router.post('/', ctrl.create);
router.put('/:id/hold', requirePage('factory_purchases'), ctrl.hold);
router.put('/:id/revert', requirePage('factory_purchases'), ctrl.revert);
router.put('/:id/receive', requirePage('factory_purchases'), ctrl.receive);
router.put('/:id/cancel', requirePage('factory_purchases'), ctrl.cancel);
router.put('/:id/return', requirePage('factory_purchases'), ctrl.returnItem);
router.put('/:id/revert-cancel', requirePage('factory_purchases'), ctrl.revertCancel);
router.delete('/:id', ctrl.delete);

module.exports = router;
