const Joi = require('joi');

const validate = require('./validate');

const schemaCreateNewUser = Joi.object({
	number: Joi.string().min(8).max(14).required(),
	country: Joi.string().min(1).max(40).required(),
}).min(2);

const schemaUpdateCredentials = Joi.object({
	name: Joi.string().min(1).max(30).trim().required(),
	lastName: Joi.string().min(1).max(30).trim().required(),
	originalName: Joi.string().min(1).max(30).trim().required(),
	originalLastName: Joi.string().min(1).max(30).trim().required(),
}).min(4);

const schemaUpdateUser = Joi.object({
	nightMode: Joi.boolean().optional(),
	sort: Joi.boolean().optional(),
}).min(1);

const schemaLogIn = Joi.object({
	number: Joi.string().min(8).max(14).required(),
	code: Joi.string().min(3).max(12).required(),
}).min(2);

module.exports.registration = (req, _res, next) => {
	return validate(schemaCreateNewUser, req.body, next);
};
module.exports.updateCredentials = (req, _res, next) => {
	return validate(schemaUpdateCredentials, req.body, next);
};
module.exports.updateUser = (req, _res, next) => {
	return validate(schemaUpdateUser, req.body, next);
};
module.exports.logIn = (req, _res, next) => {
	return validate(schemaLogIn, req.body, next);
};

module.exports.validateUploadFile = (req, res, next) => {
	if (!req.file)
		return res.status(400).json({
			status: 'error',
			code: 400,
			data: 'Bad request',
			message: 'File not found',
		});
	next();
};
