<?php
/**
 * BOAFO Front Controller
 * All requests are routed through here
 */

// Bootstrap the application
require_once dirname(__DIR__) . '/config/app.php';
require_once dirname(__DIR__) . '/config/database.php';
require_once dirname(__DIR__) . '/app/Core/helpers.php';
require_once dirname(__DIR__) . '/app/Core/BaseModel.php';
require_once dirname(__DIR__) . '/app/Core/BaseController.php';
require_once dirname(__DIR__) . '/app/Core/Auth.php';
require_once dirname(__DIR__) . '/app/Core/CSRF.php';
require_once dirname(__DIR__) . '/app/Core/Router.php';
require_once dirname(__DIR__) . '/app/Core/Middleware.php';

// Start session
ini_set('session.cookie_httponly', 1);
ini_set('session.use_strict_mode', 1);
ini_set('session.gc_maxlifetime', SESSION_LIFETIME);
session_name(SESSION_NAME);
session_start();

// Instantiate router
$router = new Router();

// ============================================================
// ROUTES
// ============================================================

// --- Public routes ---
$router->get('/', 'HomeController@index');
$router->get('/about', 'HomeController@about');
$router->get('/contact', 'HomeController@contact');
$router->post('/contact', 'HomeController@sendContact');

// --- Auth routes ---
$router->get('/login',    'AuthController@loginForm',    ['guest']);
$router->post('/login',   'AuthController@login',        ['guest']);
$router->get('/register', 'AuthController@registerForm', ['guest']);
$router->post('/register','AuthController@register',     ['guest']);
$router->get('/logout',   'AuthController@logout',       ['auth']);
$router->get('/verify-email/{token}', 'AuthController@verifyEmail');
$router->get('/forgot-password',  'AuthController@forgotForm',  ['guest']);
$router->post('/forgot-password', 'AuthController@forgot',      ['guest']);
$router->get('/reset-password/{token}',  'AuthController@resetForm');
$router->post('/reset-password/{token}', 'AuthController@reset');

// --- Dashboard ---
$router->get('/dashboard', 'DashboardController@index', ['auth']);

// --- Profile ---
$router->get('/profile',           'ProfileController@show',   ['auth']);
$router->get('/profile/edit',      'ProfileController@edit',   ['auth']);
$router->post('/profile/edit',     'ProfileController@update', ['auth']);
$router->post('/profile/avatar',   'ProfileController@uploadAvatar', ['auth']);
$router->get('/profile/wassce',    'ProfileController@wassce', ['auth', 'student']);
$router->post('/profile/wassce',   'ProfileController@saveWassce', ['auth', 'student']);

// --- Module 2: University Discovery ---
$router->get('/universities',                'UniversityController@index');
$router->get('/universities/{slug}',         'UniversityController@show');
$router->post('/universities/{id}/save',     'UniversityController@save',   ['auth']);
$router->post('/universities/{id}/unsave',   'UniversityController@unsave', ['auth']);
$router->get('/universities/{slug}/programs','UniversityController@programs');
$router->get('/universities/{slug}/mentors', 'UniversityController@mentors');
$router->get('/universities/{slug}/scholarships','UniversityController@scholarships');

// --- Module 3: Mentor Network ---
$router->get('/mentors',                             'MentorController@index');
$router->get('/mentors/matched',                     'MentorController@matched',        ['auth', 'student']);
$router->post('/mentors/requests/{id}/accept',       'MentorController@acceptRequest',  ['auth', 'mentor']);
$router->post('/mentors/requests/{id}/decline',      'MentorController@declineRequest', ['auth', 'mentor']);
$router->get('/mentors/{id}',                        'MentorController@profile');
$router->post('/mentors/{id}/request',               'MentorController@sendRequest',    ['auth', 'student']);

// --- Module 4: Scholarship Discovery & Recommendation ---
$router->get('/scholarships',            'ScholarshipController@index');
$router->get('/scholarships/recommended',  'ScholarshipController@recommended',  ['auth', 'student']);
$router->post('/api/scholarships/recommend','ScholarshipController@ajaxRecommend',['auth', 'student']);
$router->get('/scholarships/{slug}',     'ScholarshipController@show');
$router->post('/scholarships/{id}/save', 'ScholarshipController@save',   ['auth']);
$router->post('/scholarships/{id}/unsave','ScholarshipController@unsave',['auth']);

// --- Module 6: Admission Analyzer ---
$router->get('/admission-analyzer',         'AdmissionController@index',  ['auth', 'student']);
$router->post('/api/admission/analyze',     'AdmissionController@analyze',['auth', 'student']);

// --- Module 7: Scholarship Success Predictor ---
$router->post('/api/scholarship/predict',   'ScholarshipController@predict',['auth', 'student']);

// --- Module 8: Mentor Matching ---
$router->post('/api/mentors/match',         'MentorController@ajaxMatch', ['auth', 'student']);

// --- Module 9: Communication ---
$router->get('/messages',                   'MessageController@index',    ['auth']);
$router->get('/messages/{conversationId}',  'MessageController@conversation', ['auth']);
$router->post('/api/messages/send',         'MessageController@send',     ['auth']);
$router->post('/api/messages/mark-read',    'MessageController@markRead', ['auth']);
$router->get('/api/messages/unread-count',  'MessageController@unreadCount',['auth']);
$router->get('/sessions',                   'SessionController@index',    ['auth']);
$router->get('/api/sessions/available-slots','SessionController@availableSlots', ['auth']);
$router->get('/sessions/book/{mentorId}',   'SessionController@bookForm', ['auth', 'student']);
$router->post('/sessions/book',             'SessionController@book',     ['auth', 'student']);
$router->post('/sessions/{id}/confirm',     'SessionController@confirm',  ['auth', 'mentor']);
$router->post('/sessions/{id}/cancel',      'SessionController@cancel',   ['auth']);
$router->post('/sessions/{id}/done',        'SessionController@done',     ['auth']);
$router->post('/sessions/{id}/rate',        'SessionController@rate',     ['auth', 'student']);
$router->get('/sessions/{id}/meeting',      'SessionController@meeting',  ['auth']);
$router->get('/sessions/{id}/ics',          'SessionController@ics',      ['auth']);
$router->get('/notifications',              'NotificationController@index',['auth']);
$router->post('/api/notifications/read/{id}','NotificationController@markRead',['auth']);
$router->get('/api/notifications/count',    'NotificationController@count',['auth']);

// --- Module 10: Transition Center ---
$router->get('/transition',              'TransitionController@index', ['auth']);
$router->get('/transition/checklist',    'TransitionController@checklist', ['auth', 'student']);
$router->post('/transition/checklist',   'TransitionController@updateChecklist', ['auth', 'student']);

// --- Module 11: Career Guidance ---
$router->get('/careers',             'CareerController@index');
$router->get('/careers/{slug}',      'CareerController@show');
$router->get('/career-assessment',   'CareerController@assessment',   ['auth', 'student']);
$router->post('/career-assessment',  'CareerController@submitAssessment',['auth','student']);

// --- Module 12: Resource Center ---
$router->get('/resources',                  'ResourceController@index');
$router->get('/resources/{slug}',           'ResourceController@show');
$router->get('/resources/{slug}/download',  'ResourceController@download');

// --- Module 13: Admin ---
$router->get('/admin',                       'AdminController@dashboard', ['admin']);
$router->get('/admin/users',                 'AdminController@users',     ['admin']);
$router->get('/admin/users/create',          'AdminController@createUserForm', ['admin']);
$router->post('/admin/users/create',         'AdminController@createUser',     ['admin']);
$router->get('/admin/users/{id}',            'AdminController@viewUser',  ['admin']);
$router->get('/admin/users/{id}/edit',       'AdminController@editUserForm',   ['admin']);
$router->post('/admin/users/{id}/edit',      'AdminController@editUser',       ['admin']);
$router->post('/admin/users/{id}/status',    'AdminController@updateUserStatus', ['admin']);
$router->post('/admin/users/{id}/toggle',    'AdminController@toggleUserStatus', ['admin']);
$router->post('/admin/users/{id}/delete',    'AdminController@deleteUser',     ['admin']);
$router->post('/admin/users/{id}/notes',     'AdminController@saveUserNotes',  ['admin']);
$router->post('/admin/users/{id}/approve',   'AdminController@approveUser',    ['admin']);
$router->post('/admin/users/{id}/reject',    'AdminController@rejectUser',     ['admin']);
$router->post('/admin/users/bulk',           'AdminController@bulkAction',     ['admin']);
$router->get('/admin/universities',          'AdminController@universities',['admin']);
$router->get('/admin/universities/create',   'AdminController@createUniversityForm',['admin']);
$router->post('/admin/universities/create',  'AdminController@createUniversity',['admin']);
$router->get('/admin/universities/{id}/edit','AdminController@editUniversityForm',['admin']);
$router->post('/admin/universities/{id}/edit','AdminController@editUniversity',['admin']);
$router->post('/admin/universities/{id}/delete','AdminController@deleteUniversity',['admin']);
$router->get('/admin/universities/{id}/programmes',            'AdminController@universityProgrammes',  ['admin']);
$router->get('/admin/universities/{id}/programmes/create',     'AdminController@createProgrammeForm',   ['admin']);
$router->post('/admin/universities/{id}/programmes/create',    'AdminController@createProgramme',       ['admin']);
$router->get('/admin/universities/{id}/programmes/{pid}/edit', 'AdminController@editProgrammeForm',     ['admin']);
$router->post('/admin/universities/{id}/programmes/{pid}/edit','AdminController@editProgramme',         ['admin']);
$router->post('/admin/universities/{id}/programmes/{pid}/delete','AdminController@deleteProgramme',     ['admin']);
$router->get('/admin/scholarships',          'AdminController@scholarships',['admin']);
$router->get('/admin/scholarships/create',   'AdminController@createScholarshipForm',['admin']);
$router->post('/admin/scholarships/create',  'AdminController@createScholarship',['admin']);
$router->get('/admin/scholarships/{id}/edit','AdminController@editScholarshipForm',['admin']);
$router->post('/admin/scholarships/{id}/edit','AdminController@editScholarship',['admin']);
$router->post('/admin/scholarships/{id}/delete','AdminController@deleteScholarship',['admin']);
$router->get('/admin/mentors',               'AdminController@mentors',   ['admin']);
$router->post('/admin/mentors/{id}/verify',  'AdminController@verifyMentor',['admin']);
$router->get('/admin/analytics',             'AdminController@analytics', ['admin']);
$router->get('/admin/resources',             'AdminController@resources', ['admin']);
$router->get('/admin/resources/create',      'AdminController@createResourceForm',['admin']);
$router->post('/admin/resources/create',     'AdminController@createResource',['admin']);
$router->get('/admin/resources/{id}/edit',   'AdminController@editResourceForm',['admin']);
$router->post('/admin/resources/{id}/edit',  'AdminController@editResource',['admin']);
$router->post('/admin/resources/{id}/delete','AdminController@deleteResource',['admin']);
$router->get('/admin/audit-logs',            'AdminController@auditLogs', ['admin']);

// --- AJAX APIs ---
$router->get('/api/universities/search',     'UniversityController@ajaxSearch');
$router->get('/api/scholarships/search',     'ScholarshipController@ajaxSearch');
$router->get('/api/mentors/search',          'MentorController@ajaxSearch');
$router->get('/api/stats',                   'HomeController@stats');

// ============================================================
// DISPATCH
// ============================================================
$router->dispatch($_SERVER['REQUEST_URI'], $_SERVER['REQUEST_METHOD']);
