import { body, ValidationChain } from 'express-validator';
import { handleValidationErrors, strictBodyValidation, isPositiveNumber } from './common';

// transferMoney (Wise) validation
export const validateTransferMoney: ValidationChain[] = [
    body('details.accountHolderName')
        .trim()
        .notEmpty().withMessage('Account holder name is required')
        .isLength({ max: 100 }).withMessage('Account holder name must not exceed 100 characters'),
    body('details.abartn')
        .trim()
        .notEmpty().withMessage('ABA routing number is required')
        .isLength({ max: 20 }).withMessage('ABA routing number must not exceed 20 characters'),
    body('details.accountNumber')
        .trim()
        .notEmpty().withMessage('Account number is required')
        .isLength({ max: 50 }).withMessage('Account number must not exceed 50 characters'),
    body('details.address.country')
        .trim()
        .notEmpty().withMessage('Country is required')
        .isLength({ max: 100 }).withMessage('Country must not exceed 100 characters'),
    body('details.address.city')
        .trim()
        .notEmpty().withMessage('City is required')
        .isLength({ max: 100 }).withMessage('City must not exceed 100 characters'),
    body('details.address.state')
        .trim()
        .notEmpty().withMessage('State is required')
        .isLength({ max: 100 }).withMessage('State must not exceed 100 characters'),
    body('details.address.postCode')
        .trim()
        .notEmpty().withMessage('Post code is required')
        .isLength({ max: 20 }).withMessage('Post code must not exceed 20 characters'),
    body('details.address.firstLine')
        .trim()
        .notEmpty().withMessage('Address first line is required')
        .isLength({ max: 200 }).withMessage('Address first line must not exceed 200 characters'),
    body('details.transactionId')
        .trim()
        .notEmpty().withMessage('Transaction ID is required')
        .isUUID().withMessage('Invalid transaction ID format'),
    body('details.userId')
        .trim()
        .notEmpty().withMessage('User ID is required')
        .isUUID().withMessage('Invalid user ID format'),
    body('quoteData.sourceAmount')
        .notEmpty().withMessage('Source amount is required')
        .custom(isPositiveNumber).withMessage('Source amount must be a positive number')
];

export const transferMoneyValidation = [
    // Note: Nested objects make strict validation complex, so we'll validate required fields
    ...validateTransferMoney,
    handleValidationErrors
];

// pay (PayPal) validation
export const validatePay: ValidationChain[] = [
    body('amount')
        .notEmpty().withMessage('Amount is required')
        .custom(isPositiveNumber).withMessage('Amount must be a positive number')
];

export const payValidation = [
    strictBodyValidation(['amount']),
    ...validatePay,
    handleValidationErrors
];

// withdraw (PayPal) validation
export const validateWithdraw: ValidationChain[] = [
    body('withdrawAmount')
        .notEmpty().withMessage('Withdraw amount is required')
        .custom(isPositiveNumber).withMessage('Withdraw amount must be a positive number'),
    body('userId')
        .notEmpty().withMessage('User ID is required')
        .isUUID().withMessage('Invalid user ID format'),
    body('transactionId')
        .notEmpty().withMessage('Transaction ID is required')
        .isUUID().withMessage('Invalid transaction ID format')
];

export const withdrawValidation = [
    strictBodyValidation(['withdrawAmount', 'userId', 'transactionId']),
    ...validateWithdraw,
    handleValidationErrors
];
