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

// adminLogin validation
export const validateAdminLogin: ValidationChain[] = [
    body('email')
        .trim()
        .notEmpty().withMessage('Email is required')
        .isEmail().withMessage('Invalid email format')
        .normalizeEmail(),
    body('password')
        .notEmpty().withMessage('Password is required')
];

export const adminLoginValidation = [
    strictBodyValidation(['email', 'password']),
    ...validateAdminLogin,
    handleValidationErrors
];

// createSalesPerson validation
export const validateCreateSalesPerson: ValidationChain[] = [
    body('name')
        .trim()
        .notEmpty().withMessage('Name is required')
        .isLength({ min: 2, max: 100 }).withMessage('Name must be between 2 and 100 characters'),
    body('email')
        .trim()
        .notEmpty().withMessage('Email is required')
        .isEmail().withMessage('Invalid email format')
        .normalizeEmail(),
    body('phone')
        .trim()
        .notEmpty().withMessage('Phone is required')
        .isLength({ max: 20 }).withMessage('Phone number must not exceed 20 characters'),
    body('referralCode')
        .trim()
        .notEmpty().withMessage('Referral code is required')
        .isLength({ max: 50 }).withMessage('Referral code must not exceed 50 characters'),
    body('commission')
        .notEmpty().withMessage('Commission is required')
        .custom(isNonNegativeNumber).withMessage('Commission must be a non-negative number')
];

export const createSalesPersonValidation = [
    strictBodyValidation(['name', 'email', 'phone', 'referralCode', 'commission']),
    ...validateCreateSalesPerson,
    handleValidationErrors
];

// updateSalesPerson validation
export const validateUpdateSalesPerson: ValidationChain[] = [
    body('id')
        .notEmpty().withMessage('Sales person ID is required')
        .isUUID().withMessage('Invalid sales person ID format'),
    body('name')
        .optional()
        .trim()
        .isLength({ min: 2, max: 100 }).withMessage('Name must be between 2 and 100 characters'),
    body('email')
        .optional()
        .trim()
        .isEmail().withMessage('Invalid email format')
        .normalizeEmail(),
    body('phone')
        .optional()
        .trim()
        .isLength({ max: 20 }).withMessage('Phone number must not exceed 20 characters'),
    body('referralCode')
        .optional()
        .trim()
        .isLength({ max: 50 }).withMessage('Referral code must not exceed 50 characters'),
    body('commission')
        .optional()
        .custom(isNonNegativeNumber).withMessage('Commission must be a non-negative number')
];

export const updateSalesPersonValidation = [
    strictBodyValidation(['id', 'name', 'email', 'phone', 'referralCode', 'commission']),
    ...validateUpdateSalesPerson,
    handleValidationErrors
];

// updateNotification validation
export const validateUpdateNotification: ValidationChain[] = [
    body('id')
        .notEmpty().withMessage('Notification ID is required')
        .isUUID().withMessage('Invalid notification ID format')
];

export const updateNotificationValidation = [
    strictBodyValidation(['id']),
    ...validateUpdateNotification,
    handleValidationErrors
];

// investAmount validation
export const validateInvestAmount: ValidationChain[] = [
    body('investedAmount')
        .notEmpty().withMessage('Invested amount is required')
        .custom(isNonNegativeNumber).withMessage('Invested amount must be a non-negative number'),
    body('dayEndPortfolio')
        .notEmpty().withMessage('Day end portfolio is required')
        .custom(isNonNegativeNumber).withMessage('Day end portfolio must be a non-negative number'),
    body('daily_leadger_date')
        .notEmpty().withMessage('Daily ledger date is required')
        .isISO8601().withMessage('Invalid date format for daily ledger date')
];

export const investAmountValidation = [
    strictBodyValidation(['investedAmount', 'dayEndPortfolio', 'daily_leadger_date']),
    ...validateInvestAmount,
    handleValidationErrors
];

// updateDailyLedger validation
export const validateUpdateDailyLedger: ValidationChain[] = [
    body('id')
        .notEmpty().withMessage('Daily ledger ID is required')
        .isUUID().withMessage('Invalid daily ledger ID format'),
    body('investedAmount')
        .notEmpty().withMessage('Invested amount is required')
        .custom(isNonNegativeNumber).withMessage('Invested amount must be a non-negative number'),
    body('dayEndPortfolio')
        .notEmpty().withMessage('Day end portfolio is required')
        .custom(isNonNegativeNumber).withMessage('Day end portfolio must be a non-negative number'),
    body('createdAt')
        .notEmpty().withMessage('Created at date is required')
        .isISO8601().withMessage('Invalid date format for created at')
];

export const updateDailyLedgerValidation = [
    strictBodyValidation(['id', 'investedAmount', 'dayEndPortfolio', 'createdAt']),
    ...validateUpdateDailyLedger,
    handleValidationErrors
];

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

export const depositAmountByUserValidation = [
    strictBodyValidation(['amount']),
    ...validateDepositAmountByUser,
    handleValidationErrors
];
