const router = require('express').Router();

// GET /auth/login - Render login page
router.get('/login', (req, res) => {
    res.render('login', {
        title: 'Login - EMTAF',
        nonce: res.locals.nonce,
        error: null
    });
});

// POST /auth/login - Handle login form submission
router.post('/login', (req, res) => {
    const { email, password } = req.body;

    // Basic server-side validation
    if (!email || !password) {
        return res.render('login', {
            title: 'Login - EMTAF',
            nonce: res.locals.nonce,
            error: 'Email and password are required.'
        });
    }

    // Placeholder for login logic (e.g., verify with database)
    console.log('Login attempt:', { email, password });
    res.redirect('/'); // Redirect to homepage on success
});


// GET /auth/register - Render registration page
router.get('/register', (req, res) => {
    res.render('register', {
        title: 'Sign Up - EMTAF',
        nonce: res.locals.nonce,
        error: null // Placeholder for error messages
    });
});

// POST /auth/register - Handle registration form submission
router.post('/register', (req, res) => {
    const { firstName, lastName, email, password } = req.body;

    // Basic server-side validation
    if (!firstName || !lastName || !email || !password) {
        return res.render('register', {
            title: 'Sign Up - EMTAF',
            nonce: res.locals.nonce,
            error: 'All fields are required.'
        });
    }

    // Placeholder for actual registration logic (e.g., save to database)
    // Example: Hash password with bcryptjs and save to Sequelize model
    console.log('Registration attempt:', { firstName, lastName, email, password });

    // Redirect to login page on success (or render error on failure)
    res.redirect('/login');
});

// GET /auth/google - Placeholder for Google OAuth login
router.get('/google', (req, res) => {
    // Placeholder for Google OAuth redirect
    res.render('login', {
        title: 'Login - EMTAF',
        nonce: res.locals.nonce,
        error: 'Google login not implemented yet.'
    });
});

// GET /auth/forgot-password - Render forgot password page
router.get('/forgot-password', (req, res) => {
    res.render('forgot-password', {
        title: 'Forgot Password - EMTAF',
        nonce: res.locals.nonce,
        error: null,
        success: null
    });
});


// POST /auth/forgot-password - Handle forgot password form submission
router.post('/forgot-password', (req, res) => {
    const { email } = req.body;

    if (!email) {
        return res.render('forgot-password', {
            title: 'Forgot Password - EMTAF',
            nonce: res.locals.nonce,
            error: 'Email is required.',
            success: null
        });
    }

    // Placeholder for sending reset email
    console.log('Password reset request for:', { email });
    // Example: Generate token, save to database, and send email with nodemailer
    res.render('forgot-password', {
        title: 'Forgot Password - EMTAF',
        nonce: res.locals.nonce,
        error: null,
        success: 'If an account exists for this email, a password reset link has been sent.'
    });
});

// GET /auth/reset-password/:token - Render reset password page
router.get('/reset-password/:token', (req, res) => {
    const { token } = req.params;

    // Placeholder for token validation
    // Example: Check token against database
    console.log('Password reset token:', { token });

    res.render('reset-password', {
        title: 'Reset Password - EMTAF',
        nonce: res.locals.nonce,
        error: null,
        token
    });
});

// POST /auth/reset-password/:token - Handle reset password form submission
router.post('/reset-password/:token', (req, res) => {
    const { token } = req.params;
    const { password, confirmPassword } = req.body;

    if (!password || !confirmPassword) {
        return res.render('reset-password', {
            title: 'Reset Password - EMTAF',
            nonce: res.locals.nonce,
            error: 'All fields are required.',
            token
        });
    }

    if (password !== confirmPassword) {
        return res.render('reset-password', {
            title: 'Reset Password - EMTAF',
            nonce: res.locals.nonce,
            error: 'Passwords do not match.',
            token
        });
    }

    // Placeholder for password update logic
    console.log('Password reset attempt:', { token, password });
    // Example: Validate token, hash password with bcryptjs, update user in database
    res.redirect('/auth/login');
});

router.get('/verify-email', (req, res) => res.render('auth/verify-email', { title: 'Verify Email' }));

module.exports = router;
