/**
 * Request validators based on express-validator.
 * Ensures incoming data is validated for API endpoints.
 */

const { body, param, validationResult } = require('express-validator');

function validate(req) {
  const result = validationResult(req);
  if (!result.isEmpty()) {
    const err = new Error('Validation failed');
    err.statusCode = 400;
    err.details = result.array();
    throw err;
  }
}

// Common helpers
const isStringTrimmed = (min = 1, max = 255) =>
  (value) => typeof value === 'string' && value.trim().length >= min && value.trim().length <= max;

const supplierCodeSchema = param('supplierCode')
  .isString().trim().notEmpty().isLength({ max: 50 });

const shipmentNumberSchema = param('shipmentNumber')
  .isString().trim().notEmpty().isLength({ max: 50 });

const deliveryCodeSchema = param('deliveryCode')
  .isString().trim().notEmpty().isLength({ max: 50 });

const registerSchema = [
  body('username').isString().trim().notEmpty().isLength({ max: 100 }),
  body('password').isString().trim().notEmpty().isLength({ min: 6, max: 255 })
];

const loginSchema = [
  body('username').isString().trim().notEmpty().isLength({ max: 100 }),
  body('password').isString().trim().notEmpty().isLength({ min: 6, max: 255 })
];

const supplierCreateSchema = [
  body('supplierCode').isString().trim().notEmpty().isLength({ max: 50 }),
  body('supplierName').isString().trim().notEmpty().isLength({ max: 200 }),
  body('telephone').optional().isString().trim().isLength({ max: 50 }),
  body('address').optional().isString().trim().isLength({ max: 255 }),
  body('email').optional().isEmail().normalizeEmail().isLength({ max: 200 })
];

const supplierUpdateSchema = [
  body('supplierName').optional().isString().trim().notEmpty().isLength({ max: 200 }),
  body('telephone').optional().isString().trim().isLength({ max: 50 }),
  body('address').optional().isString().trim().isLength({ max: 255 }),
  body('email').optional().isEmail().normalizeEmail().isLength({ max: 200 })
];

const shipmentCreateSchema = [
  body('shipmentNumber').isString().trim().notEmpty().isLength({ max: 50 }),
  body('shipmentDate').isISO8601().toDate(),
  body('shipmentStatus').isString().trim().notEmpty().isLength({ max: 50 }),
  body('destination').isString().trim().notEmpty().isLength({ max: 200 }),
  body('supplierCode').isString().trim().notEmpty().isLength({ max: 50 })
];

const shipmentUpdateSchema = [
  body('shipmentDate').optional().isISO8601().toDate(),
  body('shipmentStatus').optional().isString().trim().notEmpty().isLength({ max: 50 }),
  body('destination').optional().isString().trim().notEmpty().isLength({ max: 200 }),
  body('supplierCode').optional().isString().trim().notEmpty().isLength({ max: 50 })
];

const deliveryCreateSchema = [
  body('deliveryCode').isString().trim().notEmpty().isLength({ max: 50 }),
  body('deliveryDate').isISO8601().toDate(),
  body('quantityDelivered').isInt({ min: 0 }),
  body('deliveryStatus').isString().trim().notEmpty().isLength({ max: 50 }),
  body('shipmentNumber').isString().trim().notEmpty().isLength({ max: 50 })
];

const deliveryUpdateSchema = [
  body('deliveryDate').optional().isISO8601().toDate(),
  body('quantityDelivered').optional().isInt({ min: 0 }),
  body('deliveryStatus').optional().isString().trim().notEmpty().isLength({ max: 50 }),
  body('shipmentNumber').optional().isString().trim().notEmpty().isLength({ max: 50 })
];

module.exports = {
  validate,
  registerSchema,
  loginSchema,
  supplierCreateSchema,
  supplierUpdateSchema,
  shipmentCreateSchema,
  shipmentUpdateSchema,
  deliveryCreateSchema,
  deliveryUpdateSchema,
  supplierCodeSchema,
  shipmentNumberSchema,
  deliveryCodeSchema
};

