const Joi = require('joi');

const enquirySchema = Joi.object({
    fullname: Joi.string()
        .pattern(/^[a-zA-Z\s'-]+$/)
        .min(3)
        .max(50)
        .required()
        .messages({
            'string.base': 'Full name must be a text.',
            'string.empty': 'Full name is required.',
            'string.min': 'Full name should have at least 3 characters.',
            'string.max': 'Full name should have at most 50 characters.',
            'string.pattern.base': 'Full name can only contain letters, spaces, apostrophes, and hyphens.',
        }),
    email: Joi.string()
        .email()
        .required()
        .messages({
            'string.base': 'Email must be a text.',
            'string.email': 'Please provide a valid email address.',
            'string.empty': 'Email is required.',
        }),
    contact: Joi.string()
        .min(10)
        .max(10)
        .pattern(/^[0-9]+$/)
        .required()
        .messages({
            'string.base': 'Contact number must be a number.',
            'string.empty': 'Contact number is required.',
            'string.min': 'Contact number should have at least 10 digits.',
            'string.max': 'Contact number should have at most 10 digits.',
            'string.pattern.base': 'Contact number must be a valid 10-digit number.',
        }),
    instagram_url: Joi.string()
        .uri()
        .required()
        .messages({
            'string.base': 'Instagram URL must be a text.',
            'string.empty': 'Instagram URL is required.',
            'string.uri': 'Please provide a valid Instagram URL.',
        }),
    date_of_birth: Joi.date()
        .required()
        .messages({
            'date.base': 'Date of birth must be a valid date.',
            'date.empty': 'Date of birth is required.',
        }),
    marital_status: Joi.string()
        .min(1)
        .optional()
        .allow('')
        .messages({
            'string.base': 'Marital status must be a text.',
            'string.min': 'Marital status should be at least 1 character.',
        }),
    source: Joi.string()
        .min(2)
        .max(50)
        .required()
        .messages({
            'string.base': 'source must be a text.',
            'string.empty': 'source is required.',
            'string.min': 'source should have at least 2 characters.',
            'string.max': 'source should have at most 50 characters.',
        }),
    state: Joi.string()
        .min(2)
        .max(50)
        .required()
        .messages({
            'string.base': 'State must be a text.',
            'string.empty': 'State is required.',
            'string.min': 'State should have at least 2 characters.',
            'string.max': 'State should have at most 50 characters.',
        }),
    city: Joi.string()
        .min(2)
        .max(50)
        .required()
        .messages({
            'string.base': 'City must be a text.',
            'string.empty': 'City is required.',
            'string.min': 'City should have at least 2 characters.',
            'string.max': 'City should have at most 50 characters.',
        }),

    // portfolio is uploaded as PDF file (validated in validatePortfolioFile)
    // marriage_certificate is uploaded as PDF file (validated in validateMarriageCertificateFile)
    // age_proof is uploaded as PDF file (validated in validateAgeProofFile)

    created_at: Joi.date()
        .default(() => new Date())
        .messages({
            'date.base': 'Created date must be a valid date.',
        }),
});




function validateEnquiry(req, res, next) {
    // convert: true for multipart form string values; stripUnknown: true so "portfolio" (file field) in body is allowed and stripped
    const { error, value } = enquirySchema.validate(req.body, {
        convert: true,
        stripUnknown: true,
    });

    if (error) {
        return res.status(400).json({
            status: "error",
            message: error.details.map((err) => err.message),
        });
    }
    req.body = value;
    next();
}

function validatePortfolioFile(req, res, next) {
    if (!req.files || !req.files.portfolio || !req.files.portfolio[0]) {
        return res.status(400).json({
            status: "error",
            message: ["Portfolio PDF file is required."],
        });
    }
    if (req.files.portfolio[0].mimetype !== "application/pdf") {
        return res.status(400).json({
            status: "error",
            message: ["Portfolio must be a PDF file."],
        });
    }
    next();
}

function validateMarriageCertificateFile(req, res, next) {
    // Marriage certificate is optional, so only validate if provided
    if (req.files && req.files.marriage_certificate && req.files.marriage_certificate[0]) {
        if (req.files.marriage_certificate[0].mimetype !== "application/pdf") {
            return res.status(400).json({
                status: "error",
                message: ["Marriage certificate must be a PDF file."],
            });
        }
    }
    next();
}

function validateAgeProofFile(req, res, next) {
    if (!req.files || !req.files.age_proof || !req.files.age_proof[0]) {
        return res.status(400).json({
            status: "error",
            message: ["Age proof PDF file is required."],
        });
    }
    if (req.files.age_proof[0].mimetype !== "application/pdf") {
        return res.status(400).json({
            status: "error",
            message: ["Age proof must be a PDF file."],
        });
    }
    next();
}

module.exports = {
    validateEnquiry,
    validatePortfolioFile,
    validateMarriageCertificateFile,
    validateAgeProofFile,
};








