/**
 * Supplier routes
 */

const express = require('express');
const { create, getAll, getOne, update } = require('../controllers/supplier.controller');
const { supplierCreateSchema, supplierUpdateSchema, supplierCodeSchema, validate } = require('../utils/validators');

const router = express.Router();

router.post('/', supplierCreateSchema, (req, res, next) => {
  try {
    validate(req);
    return create(req, res).catch(next);
  } catch (e) {
    return next(e);
  }
});

router.get('/', getAll);

router.get('/:supplierCode', supplierCodeSchema, (req, res, next) => {
  try {
    validate(req);
    return getOne(req, res).catch(next);
  } catch (e) {
    return next(e);
  }
});

// Optional: update supplier (not explicitly required, but helpful for completeness)
router.put('/:supplierCode', supplierCodeSchema, supplierUpdateSchema, (req, res, next) => {
  try {
    validate(req);
    return update(req, res).catch(next);
  } catch (e) {
    return next(e);
  }
});

module.exports = router;

