const express = require('express');
const {
	accountCreate,
	memberLogin,
	getProfile,
	updateProfile,
	changePassword,
	deleteAccount,
	requestPasswordReset,
	resetPassword,
	allMembers,
	refreshToken,
	checkPassword,
} = require('../../controllers/members/accounts');
const { memberAuth, validation } = require('../../middlewares');
const { body } = require('express-validator');

const router = express.Router();

const registerValidation = [
	body('organizationName').optional().isString(),
	body('isJciSenator').isIn(['Yes', 'No']).withMessage('isJciSenator must be Yes or No'),
	body('localOrgName').optional().isString(),
	body('firstName').notEmpty().withMessage('First name is required'),
	body('lastName').notEmpty().withMessage('Last name is required'),
	body('emailAddress').isEmail().withMessage('Valid email is required'),
	// body('phoneNumber').optional().isString().isLength({ max: 15 }),
	body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters'),
	body('confirmPassword')
		.custom((value, { req }) => value === req.body.password)
		.withMessage('Passwords do not match'),
	body('isTncAgreed').isBoolean().withMessage('You must accept the terms and conditions'),
	validation,
];

const updateProfileValidation = [
	body('bio').optional().isString(),
	body('organization_name').optional().isString(),
	body('is_jci_senator').optional().isIn(['Yes', 'No']),
	body('local_org_name').optional().isString(),
	body('birth_date').optional().isISO8601().toDate(),
	body('phone_number').optional().isString().isLength({ max: 15 }),
	body('first_name').optional().isString(),
	body('middleName').optional({ values: 'falsy' }).isString(),
	body('last_name').optional().isString(),
	body('email_address').optional().isEmail(),
	body('position').optional({ values: 'falsy' }).isString(),
	body('nationality').optional({ values: 'falsy' }).isString(),
	body('foodType').optional({ values: 'falsy' }).isString(),
	body('gender').optional({ values: 'falsy' }).isIn(['Male', 'Female']),
	body('allergy').optional({ values: 'falsy' }).isString(),
	validation,
];

const changePasswordValidation = [
	body('oldPassword').notEmpty().withMessage('Old password is required'),
	body('newPassword').isLength({ min: 6 }).withMessage('New password must be at least 6 characters'),
	body('newPasswordConfirmation')
		.custom((value, { req }) => value === req.body.newPassword)
		.withMessage('New passwords do not match'),
	validation,
];

router.post('/register', registerValidation, accountCreate);
router.post('/login', memberLogin);
router.get('/profile', memberAuth, getProfile);
router.patch('/profile', updateProfileValidation, memberAuth, updateProfile);
router.patch('/change-password', changePasswordValidation, memberAuth, changePassword);

router.post('/password-reset', [body('email').isEmail().withMessage('Valid email is required')], requestPasswordReset);

router.patch(
	'/password-reset',
	[
		body('token').notEmpty().withMessage('Token is required'),
		body('password').isLength({ min: 6 }).withMessage('Password must be at least 6 characters'),
		body('passwordConfirmation')
			.custom((value, { req }) => value === req.body.password)
			.withMessage('Passwords do not match'),
	],
	resetPassword,
);

router.get('/members', memberAuth, allMembers);

router.delete(
	'/',
	[body('password').notEmpty().withMessage('Password is required'), validation],
	memberAuth,
	deleteAccount,
);

router.post('/password/check', memberAuth, checkPassword);

router.post('/refresh-token', refreshToken);

module.exports = router;
