import Joi from 'joi';

export const loginSchema = Joi.object({
    email: Joi.string()
        .email({ tlds: { allow: false } })  // Allow any TLD including .local
        .required()
        .messages({
            'string.email': 'Please provide a valid email address',
            'any.required': 'Email is required'
        }),
    password: Joi.string()
        .min(6)
        .required()
        .messages({
            'string.min': 'Password must be at least 6 characters',
            'any.required': 'Password is required'
        })
});

export const registerSchema = Joi.object({
    username: Joi.string()
        .min(3)
        .max(50)
        .pattern(/^[a-zA-Z0-9_]+$/)
        .required()
        .messages({
            'string.pattern.base': 'Username can only contain letters, numbers, and underscores',
            'string.min': 'Username must be at least 3 characters',
            'string.max': 'Username must not exceed 50 characters',
            'any.required': 'Username is required'
        }),
    email: Joi.string()
        .email()
        .required()
        .messages({
            'string.email': 'Please provide a valid email address',
            'any.required': 'Email is required'
        }),
    password: Joi.string()
        .min(6)
        .max(100)
        .required()
        .messages({
            'string.min': 'Password must be at least 6 characters',
            'string.max': 'Password must not exceed 100 characters',
            'any.required': 'Password is required'
        }),
    prefix: Joi.object({
        th: Joi.string().optional(),
        en: Joi.string().optional()
    }).optional(),
    first_name: Joi.string()
        .min(1)
        .max(100)
        .required()
        .messages({
            'any.required': 'First name is required'
        }),
    last_name: Joi.string()
        .min(1)
        .max(100)
        .required()
        .messages({
            'any.required': 'Last name is required'
        }),
    phone: Joi.string()
        .min(10)
        .max(15)
        .required()
        .messages({
            'any.required': 'Phone number is required'
        })
});

export const refreshTokenSchema = Joi.object({
    refreshToken: Joi.string()
        .required()
        .messages({
            'any.required': 'Refresh token is required'
        })
});

export const resetPasswordSchema = Joi.object({
    token: Joi.string()
        .required()
        .messages({
            'any.required': 'Reset token is required'
        }),
    password: Joi.string()
        .min(6)
        .max(100)
        .required()
        .messages({
            'string.min': 'Password must be at least 6 characters',
            'string.max': 'Password must not exceed 100 characters',
            'any.required': 'Password is required'
        })
});

export const changePasswordSchema = Joi.object({
    oldPassword: Joi.string()
        .min(6)
        .max(100)
        .required()
        .messages({
            'string.min': 'Password must be at least 6 characters',
            'string.max': 'Password must not exceed 100 characters',
            'any.required': 'Password is required'
        }),
    newPassword: Joi.string()
        .min(6)
        .max(100)
        .required()
        .messages({
            'string.min': 'Password must be at least 6 characters',
            'string.max': 'Password must not exceed 100 characters',
            'any.required': 'Password is required'
        })
});

export const resetRequestSchema = Joi.object({
    email: Joi.string()
        .email({ tlds: { allow: false } })  // Allow any TLD including .local
        .required()
        .messages({
            'string.email': 'Please provide a valid email address',
            'any.required': 'Email is required'
        })
});

