// ============================================================================
// OrbiQuant IA - Auth Validators
// ============================================================================

import { body, param, query } from 'express-validator';

export const registerValidator = [
  body('email')
    .isEmail()
    .normalizeEmail()
    .withMessage('Please provide a valid email'),
  body('password')
    .isLength({ min: 8 })
    .withMessage('Password must be at least 8 characters')
    .matches(/[A-Z]/)
    .withMessage('Password must contain at least one uppercase letter')
    .matches(/[a-z]/)
    .withMessage('Password must contain at least one lowercase letter')
    .matches(/[0-9]/)
    .withMessage('Password must contain at least one number')
    .matches(/[!@#$%^&*(),.?":{}|<>]/)
    .withMessage('Password must contain at least one special character'),
  body('firstName')
    .optional()
    .trim()
    .isLength({ min: 1, max: 100 })
    .withMessage('First name must be between 1 and 100 characters'),
  body('lastName')
    .optional()
    .trim()
    .isLength({ min: 1, max: 100 })
    .withMessage('Last name must be between 1 and 100 characters'),
  body('acceptTerms')
    .isBoolean()
    .equals('true')
    .withMessage('You must accept the terms and conditions'),
];

export const loginValidator = [
  body('email')
    .isEmail()
    .normalizeEmail()
    .withMessage('Please provide a valid email'),
  body('password')
    .notEmpty()
    .withMessage('Password is required'),
  body('totpCode')
    .optional()
    .isLength({ min: 6, max: 6 })
    .withMessage('TOTP code must be 6 digits'),
  body('rememberMe')
    .optional()
    .isBoolean(),
];

export const emailValidator = [
  body('email')
    .isEmail()
    .normalizeEmail()
    .withMessage('Please provide a valid email'),
];

export const tokenValidator = [
  body('token')
    .notEmpty()
    .isLength({ min: 32 })
    .withMessage('Invalid token'),
];

export const resetPasswordValidator = [
  body('token')
    .notEmpty()
    .isLength({ min: 32 })
    .withMessage('Invalid token'),
  body('password')
    .isLength({ min: 8 })
    .withMessage('Password must be at least 8 characters')
    .matches(/[A-Z]/)
    .withMessage('Password must contain at least one uppercase letter')
    .matches(/[a-z]/)
    .withMessage('Password must contain at least one lowercase letter')
    .matches(/[0-9]/)
    .withMessage('Password must contain at least one number')
    .matches(/[!@#$%^&*(),.?":{}|<>]/)
    .withMessage('Password must contain at least one special character'),
];

export const changePasswordValidator = [
  body('currentPassword')
    .notEmpty()
    .withMessage('Current password is required'),
  body('newPassword')
    .isLength({ min: 8 })
    .withMessage('Password must be at least 8 characters')
    .matches(/[A-Z]/)
    .withMessage('Password must contain at least one uppercase letter')
    .matches(/[a-z]/)
    .withMessage('Password must contain at least one lowercase letter')
    .matches(/[0-9]/)
    .withMessage('Password must contain at least one number')
    .matches(/[!@#$%^&*(),.?":{}|<>]/)
    .withMessage('Password must contain at least one special character'),
];

export const phoneOTPValidator = [
  body('phoneNumber')
    .notEmpty()
    .matches(/^[0-9+\-\s()]+$/)
    .withMessage('Please provide a valid phone number'),
  body('countryCode')
    .notEmpty()
    .matches(/^[0-9]{1,4}$/)
    .withMessage('Please provide a valid country code'),
  body('channel')
    .optional()
    .isIn(['sms', 'whatsapp', 'call'])
    .withMessage('Invalid channel'),
];

export const verifyPhoneOTPValidator = [
  body('phoneNumber')
    .notEmpty()
    .matches(/^[0-9+\-\s()]+$/)
    .withMessage('Please provide a valid phone number'),
  body('countryCode')
    .notEmpty()
    .matches(/^[0-9]{1,4}$/)
    .withMessage('Please provide a valid country code'),
  body('otpCode')
    .notEmpty()
    .isLength({ min: 6, max: 6 })
    .matches(/^[0-9]+$/)
    .withMessage('OTP code must be 6 digits'),
];

export const oauthProviderValidator = [
  param('provider')
    .isIn(['google', 'facebook', 'twitter', 'apple', 'github'])
    .withMessage('Invalid OAuth provider'),
];

export const refreshTokenValidator = [
  body('refreshToken')
    .notEmpty()
    .withMessage('Refresh token is required'),
];

export const totpCodeValidator = [
  body('code')
    .notEmpty()
    .matches(/^[0-9A-Z\-]{6,10}$/)
    .withMessage('Invalid verification code'),
];

export const sessionIdValidator = [
  param('sessionId')
    .isUUID()
    .withMessage('Invalid session ID'),
];
