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

// Validation rules for organization creation
const validateOrganizationCreation = [
  body('name')
    .notEmpty()
    .withMessage('Organization name is required')
    .isLength({ min: 2, max: 255 })
    .withMessage('Organization name must be between 2 and 255 characters')
    .matches(/^[a-zA-Z0-9\s\-_.&()']+$/)
    .withMessage('Organization name can only contain letters, numbers, spaces, and common punctuation'),

  body('code')
    .notEmpty()
    .withMessage('Organization code is required')
    .isLength({ min: 2, max: 50 })
    .withMessage('Organization code must be between 2 and 50 characters')
    .matches(/^[A-Z0-9_-]+$/)
    .withMessage('Organization code must contain only uppercase letters, numbers, underscores, and hyphens'),

  body('type')
    .optional({ checkFalsy: true })
    .isLength({ max: 100 })
    .withMessage('Organization type must be less than 100 characters'),

  body('description')
    .optional({ checkFalsy: true })
    .isLength({ max: 2000 })
    .withMessage('Organization description must be less than 2000 characters'),

  body('address')
    .optional({ checkFalsy: true })
    .isLength({ max: 500 })
    .withMessage('Organization address must be less than 500 characters'),

  body('phone')
    .optional({ checkFalsy: true })
    .isLength({ max: 20 })
    .withMessage('Phone number must be less than 20 characters'),

  body('email')
    .optional({ checkFalsy: true })
    .isLength({ max: 255 })
    .withMessage('Email must be less than 255 characters'),

  body('website')
    .optional({ checkFalsy: true })
    .isLength({ max: 255 })
    .withMessage('Website URL must be less than 255 characters'),

  body('logo')
    .optional({ checkFalsy: true })
    .isLength({ max: 500 })
    .withMessage('Logo path must be less than 500 characters'),

  body('theme')
    .optional({ checkFalsy: true })
]

// Validation rules for organization update
const validateOrganizationUpdate = [
  param('id')
    .isInt({ min: 1 })
    .withMessage('Organization ID must be a positive integer'),

  body('name')
    .optional({ checkFalsy: true })
    .isLength({ min: 2, max: 255 })
    .withMessage('Organization name must be between 2 and 255 characters')
    .matches(/^[a-zA-Z0-9\s\-_.&()']+$/)
    .withMessage('Organization name can only contain letters, numbers, spaces, and common punctuation'),

  body('code')
    .optional({ checkFalsy: true })
    .isLength({ min: 2, max: 50 })
    .withMessage('Organization code must be between 2 and 50 characters')
    .matches(/^[A-Z0-9_-]+$/)
    .withMessage('Organization code must contain only uppercase letters, numbers, underscores, and hyphens'),

  body('type')
    .optional({ checkFalsy: true })
    .isLength({ max: 100 })
    .withMessage('Organization type must be less than 100 characters'),

  body('description')
    .optional({ checkFalsy: true })
    .isLength({ max: 2000 })
    .withMessage('Organization description must be less than 2000 characters'),

  body('address')
    .optional({ checkFalsy: true })
    .isLength({ max: 500 })
    .withMessage('Organization address must be less than 500 characters'),

  body('phone')
    .optional({ checkFalsy: true })
    .isLength({ max: 20 })
    .withMessage('Phone number must be less than 20 characters'),

  body('email')
    .optional({ checkFalsy: true })
    .isLength({ max: 255 })
    .withMessage('Email must be less than 255 characters'),

  body('website')
    .optional({ checkFalsy: true })
    .isLength({ max: 255 })
    .withMessage('Website URL must be less than 255 characters'),

  body('logo')
    .optional({ checkFalsy: true })
    .isLength({ max: 500 })
    .withMessage('Logo path must be less than 500 characters'),

  body('theme')
    .optional({ checkFalsy: true }),

  body('is_active')
    .optional({ checkFalsy: true })
    .isBoolean()
    .withMessage('is_active must be a boolean value')
]

// Validation rules for organization ID parameter
const validateOrganizationId = [
  param('id')
    .isInt({ min: 1 })
    .withMessage('Organization ID must be a positive integer')
]

module.exports = {
  validateOrganizationCreation,
  validateOrganizationUpdate,
  validateOrganizationId
}
