const express = require('express');
// const { User } = require('../models'); // Assuming User model is in ../models
const router = express.Router();
const path = require('path');
const jwt = require('jsonwebtoken');

// Mock bursary data (replace with Bursary model if available)
const bursaries = [
    { id: '1', title: 'EMTAF STEM Bursary' },
    { id: '2', title: 'EMTAF Arts Bursary' },
    { id: '3', title: 'EMTAF General Bursary' }
];

// GET /student/dashboard - Render student dashboard
router.get('/dashboard', (req, res) => {
    res.render('dashboard', {
        title: 'Student Dashboard - EMTAF',
        nonce: res.locals.nonce
    });
});

// GET /student/get-profile - Render get profile page
router.get('/get-profile', (req, res) => {
    res.render('get-profile', {
        title: 'View Profile - EMTAF',
        nonce: res.locals.nonce
    });
});

// GET /student/update-profile - Render update profile page
router.get('/update-profile', async (req, res) => {
    try {
        // Fetch user data (replace req.user.id with actual user ID from session/JWT)
        // const user = await User.findByPk(req.user.id);
        // if (!user) {
        //     return res.render('error', {
        //         title: 'Error',
        //         message: 'User not found.',
        //         status: 404,
        //         nonce: res.locals.nonce
        //     });
        // }
        res.render('update-profile', {
            title: 'Update Profile - EMTAF',
            nonce: res.locals.nonce,
            // user: {
            //     fullName: user.fullName,
            //     email: user.email,
            //     address: user.address || '',
            //     phone: user.phone || '',
            //     gender: user.gender || '',
            //     race: user.race || '',
            //     idNumber: user.idNumber || '',
            //     dateOfBirth: user.dateOfBirth ? user.dateOfBirth.toISOString().split('T')[0] : '',
            //     institution: user.institution || '',
            //     qualification: user.qualification || '',
            //     guardianName: user.guardianName || '',
            //     guardianContact: user.guardianContact || '',
            //     disabilityStatus: user.disabilityStatus || false
            // },
            error: null
        });
    } catch (err) {
        console.error('Error fetching user:', err);
        res.render('error', {
            title: 'Server Error',
            message: 'Unable to load profile. Please try again.',
            status: 500,
            nonce: res.locals.nonce
        });
    }
});


// GET /student/create-application - Render create application page
router.get('/create-application', async (req, res) => {
    try {
        res.render('create-application', {
            title: 'Create Application - EMTAF',
            nonce: res.locals.nonce,
            bursaries,
            error: null
        });
    } catch (err) {
        console.error('Error fetching user:', err);
        res.render('error', {
            title: 'Server Error',
            message: 'Unable to load application form. Please try again.',
            status: 500,
            nonce: res.locals.nonce
        });
    }
});


router.get('/view-application-status', (req, res) => {
    res.render('error', {
        title: 'View Application Status',
        message: 'View Application Status page is under construction.',
        status: 200,
        nonce: res.locals.nonce
    });
});

// GET /student/upload-documents - Render upload documents page
router.get('/upload-documents', async (req, res) => {
    try {
        res.render('upload-documents', {
            title: 'Upload Documents - EMTAF',
            nonce: res.locals.nonce,
            error: null
        });
    } catch (err) {
        console.error('Error fetching applications:', err);
        res.render('error', {
            title: 'Server Error',
            message: 'Unable to load upload form. Please try again.',
            status: 500,
            nonce: res.locals.nonce
        });
    }
});

router.get('/get-profile', (req, res) => {
    res.render('error', {
        title: 'View Profile',
        message: 'View Profile page is under construction.',
        status: 200,
        nonce: res.locals.nonce
    });
});

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

router.get('/track-application', (req, res) => {
    res.render('error', {
        title: 'Track Application',
        message: 'Track Application page is under construction.',
        status: 200,
        nonce: res.locals.nonce
    });
});

module.exports = router;