<?php

declare(strict_types=1);

/**
 * Web Routes
 *
 * Defines all routes for the Gmind Institute application.
 * Route format: $router->method('/path', 'Controller@method', [middleware])
 */

use App\Core\Router;

$router = new Router();

// ─── Middleware Registration ──────────────────────────────────────────────────

$router->addMiddleware('auth', function (): void {
    requireLogin();
});

$router->addMiddleware('admin', function (): void {
    requireAdmin();
});

$router->addMiddleware('super_admin', function (): void {
    requireSuperAdmin();
});

$router->addMiddleware('branch_admin', function (): void {
    requireBranchAdmin();
});

// ─── Public Pages ────────────────────────────────────────────────────────────

$router->get('/', 'HomeController@index');
$router->get('/about', 'PageController@about');
$router->get('/pages/{slug}', 'PageController@show');
$router->get('/courses', 'CourseController@index');
$router->get('/courses/{id}', 'CourseController@show');

// ─── Exams ───────────────────────────────────────────────────────────────────

$router->get('/exam', 'ExamController@index');
$router->get('/exam/{id}/result/{attemptId}', 'ExamController@result');
$router->get('/exam/{id}/result', 'ExamController@result');
$router->get('/exam/{id}', 'ExamController@take');
$router->post('/exam/{id}/submit', 'ExamController@submit');

// ─── Branch Exam Portal (Student-facing) ─────────────────────────────────────
// Public student exam portal — students log in with enrollment_no/email + password
// and can take any active, published exam created by their branch.

$router->get('/branch-exam', 'App\Controllers\BranchExamPortalController@loginForm');
$router->post('/branch-exam/login', 'App\Controllers\BranchExamPortalController@login');
$router->post('/branch-exam/logout', 'App\Controllers\BranchExamPortalController@logout');
$router->get('/branch-exam/dashboard', 'App\Controllers\BranchExamPortalController@dashboard');
$router->get('/branch-exam/take/{id}', 'App\Controllers\BranchExamPortalController@take');
$router->post('/branch-exam/submit/{id}', 'App\Controllers\BranchExamPortalController@submit');
$router->get('/branch-exam/result/{attemptId}', 'App\Controllers\BranchExamPortalController@result');

// ─── Admission & Franchisee ──────────────────────────────────────────────────

$router->get('/admission', 'AdmissionController@index');
$router->post('/admission', 'AdmissionController@store');

$router->get('/franchisee', 'FranchiseeController@index');
$router->post('/franchisee', 'FranchiseeController@store');

// ─── Blog & News ─────────────────────────────────────────────────────────────

$router->get('/blog', 'BlogController@index');
$router->get('/blog/{slug}', 'BlogController@show');

$router->get('/news', 'NewsController@index');
$router->get('/news/{slug}', 'NewsController@show');

// ─── FAQ & Contact ───────────────────────────────────────────────────────────

$router->get('/faq', 'FaqController@index');

$router->get('/contact', 'ContactController@index');
$router->post('/contact', 'ContactController@store');

// ─── Practice Tests ─────────────────────────────────────────────────────────

$router->get('/practice', 'PracticeSetController@index');
$router->get('/practice/history', 'PracticeSetController@history');
$router->get('/practice/{id}/check-attempts', 'PracticeSetController@checkAttempts');
$router->get('/practice/{id}', 'PracticeSetController@take');
$router->post('/practice/{id}/submit', 'PracticeSetController@submit');
$router->get('/practice/{id}/result/{attemptId}', 'PracticeSetController@result');

// ─── Demo Class Booking ──────────────────────────────────────────────────────

$router->post('/demo-class', 'DemoClassController@store');

// ─── Newsletter ──────────────────────────────────────────────────────────────

$router->post('/newsletter/subscribe', 'NewsletterController@subscribe');
$router->get('/newsletter/unsubscribe', 'NewsletterController@unsubscribe');

// ─── Gallery ──────────────────────────────────────────────────────────────────

$router->get('/gallery', 'GalleryController@index');

// ─── Public Branch Info Page ──────────────────────────────────────────────────
// (Attractive branch profile page accessible from homepage "Know More" link)

$router->get('/branch-info/{code}', 'App\Controllers\BranchInfoController@show');

// ─── Toppers ──────────────────────────────────────────────────────────────────

$router->get('/toppers', 'TopperController@index');

// ─── Batch Schedule (Public) ─────────────────────────────────────────────────

$router->get('/batch-schedule', 'BatchScheduleController@index');

// ─── Authentication ──────────────────────────────────────────────────────────

$router->get('/login', 'AuthController@loginForm');
$router->post('/login', 'AuthController@login');

$router->get('/register', 'AuthController@registerForm');
$router->post('/register', 'AuthController@register');

$router->post('/logout', 'AuthController@logout');

// ─── Student Dashboard (requires auth) ───────────────────────────────────────

$router->get('/dashboard', 'DashboardController@index', ['auth']);
$router->get('/profile', 'ProfileController@index', ['auth']);
$router->post('/profile', 'ProfileController@update', ['auth']);

// ─── Admin Authentication ────────────────────────────────────────────────────

$router->get('/admin/login', 'Admin\Controllers\AdminAuthController@loginForm');
$router->post('/admin/login', 'Admin\Controllers\AdminAuthController@login');
$router->post('/admin/logout', 'Admin\Controllers\AdminAuthController@logout');

// ─── Admin Dashboard ─────────────────────────────────────────────────────────

$router->get('/admin', 'Admin\Controllers\DashboardController@index', ['auth', 'admin']);
$router->get('/admin/dashboard', 'Admin\Controllers\DashboardController@index', ['auth', 'admin']);

// ─── Admin — Sliders ─────────────────────────────────────────────────────────

$router->get('/admin/sliders', 'Admin\Controllers\AdminSliderController@index', ['auth', 'admin']);
$router->get('/admin/sliders/create', 'Admin\Controllers\AdminSliderController@create', ['auth', 'admin']);
$router->post('/admin/sliders', 'Admin\Controllers\AdminSliderController@store', ['auth', 'admin']);
$router->get('/admin/sliders/{id}/edit', 'Admin\Controllers\AdminSliderController@edit', ['auth', 'admin']);
$router->post('/admin/sliders/{id}', 'Admin\Controllers\AdminSliderController@update', ['auth', 'admin']);
$router->post('/admin/sliders/{id}/delete', 'Admin\Controllers\AdminSliderController@delete', ['auth', 'admin']);

// ─── Admin — Courses ─────────────────────────────────────────────────────────

$router->get('/admin/courses', 'Admin\Controllers\AdminCourseController@index', ['auth', 'admin']);
$router->get('/admin/courses/create', 'Admin\Controllers\AdminCourseController@create', ['auth', 'admin']);
$router->post('/admin/courses', 'Admin\Controllers\AdminCourseController@store', ['auth', 'admin']);
$router->get('/admin/courses/{id}/edit', 'Admin\Controllers\AdminCourseController@edit', ['auth', 'admin']);
$router->post('/admin/courses/{id}', 'Admin\Controllers\AdminCourseController@update', ['auth', 'admin']);
$router->post('/admin/courses/{id}/delete', 'Admin\Controllers\AdminCourseController@delete', ['auth', 'admin']);

// ─── Admin — Branches ────────────────────────────────────────────────────────

$router->get('/admin/branches', 'Admin\Controllers\AdminBranchController@index', ['auth', 'admin']);
$router->get('/admin/branches/create', 'Admin\Controllers\AdminBranchController@create', ['auth', 'admin']);
$router->post('/admin/branches', 'Admin\Controllers\AdminBranchController@store', ['auth', 'admin']);
$router->get('/admin/branches/{id}/edit', 'Admin\Controllers\AdminBranchController@edit', ['auth', 'admin']);
$router->post('/admin/branches/{id}', 'Admin\Controllers\AdminBranchController@update', ['auth', 'admin']);
$router->post('/admin/branches/{id}/delete', 'Admin\Controllers\AdminBranchController@delete', ['auth', 'admin']);

// ─── Admin — FAQs ────────────────────────────────────────────────────────────

$router->get('/admin/faqs', 'Admin\Controllers\AdminFaqController@index', ['auth', 'admin']);
$router->get('/admin/faqs/create', 'Admin\Controllers\AdminFaqController@create', ['auth', 'admin']);
$router->post('/admin/faqs', 'Admin\Controllers\AdminFaqController@store', ['auth', 'admin']);
$router->get('/admin/faqs/{id}/edit', 'Admin\Controllers\AdminFaqController@edit', ['auth', 'admin']);
$router->post('/admin/faqs/{id}', 'Admin\Controllers\AdminFaqController@update', ['auth', 'admin']);
$router->post('/admin/faqs/{id}/delete', 'Admin\Controllers\AdminFaqController@delete', ['auth', 'admin']);

// ─── Admin — Testimonials ────────────────────────────────────────────────────

$router->get('/admin/testimonials', 'Admin\Controllers\AdminTestimonialController@index', ['auth', 'admin']);
$router->post('/admin/testimonials', 'Admin\Controllers\AdminTestimonialController@store', ['auth', 'admin']);
$router->post('/admin/testimonials/{id}/approve', 'Admin\Controllers\AdminTestimonialController@approve', ['auth', 'admin']);
$router->post('/admin/testimonials/{id}/reject', 'Admin\Controllers\AdminTestimonialController@reject', ['auth', 'admin']);
$router->post('/admin/testimonials/{id}/delete', 'Admin\Controllers\AdminTestimonialController@delete', ['auth', 'admin']);

// ─── Admin — Students ────────────────────────────────────────────────────────

$router->get('/admin/students', 'Admin\Controllers\AdminStudentController@index', ['auth', 'admin']);
$router->get('/admin/students/{id}', 'Admin\Controllers\AdminStudentController@show', ['auth', 'admin']);
$router->get('/admin/students/{id}/edit', 'Admin\Controllers\AdminStudentController@edit', ['auth', 'admin']);
$router->post('/admin/students/{id}', 'Admin\Controllers\AdminStudentController@update', ['auth', 'admin']);
$router->post('/admin/students/{id}/delete', 'Admin\Controllers\AdminStudentController@delete', ['auth', 'admin']);

// ─── Admin — Exams ───────────────────────────────────────────────────────────

$router->get('/admin/exams', 'Admin\Controllers\AdminExamController@index', ['auth', 'admin']);
$router->get('/admin/exams/create', 'Admin\Controllers\AdminExamController@create', ['auth', 'admin']);
$router->post('/admin/exams', 'Admin\Controllers\AdminExamController@store', ['auth', 'admin']);
$router->get('/admin/exams/{id}/edit', 'Admin\Controllers\AdminExamController@edit', ['auth', 'admin']);
$router->post('/admin/exams/{id}', 'Admin\Controllers\AdminExamController@update', ['auth', 'admin']);
$router->post('/admin/exams/{id}/delete', 'Admin\Controllers\AdminExamController@delete', ['auth', 'admin']);
$router->get('/admin/exams/{id}/questions', 'Admin\Controllers\AdminExamController@questions', ['auth', 'admin']);
$router->post('/admin/exams/{id}/questions', 'Admin\Controllers\AdminExamController@addQuestion', ['auth', 'admin']);
$router->post('/admin/exams/{id}/questions/{question_id}/delete', 'Admin\Controllers\AdminExamController@deleteQuestion', ['auth', 'admin']);

// ─── Admin — Settings ────────────────────────────────────────────────────────

$router->get('/admin/settings', 'Admin\Controllers\AdminSettingsController@index', ['auth', 'admin']);
$router->post('/admin/settings', 'Admin\Controllers\AdminSettingsController@update', ['auth', 'admin']);

// ─── Admin — SMTP / Email Settings ──────────────────────────────────────────

$router->get('/admin/smtp-settings', 'Admin\Controllers\AdminSmtpController@index', ['auth', 'admin']);
$router->post('/admin/smtp-settings/save', 'Admin\Controllers\AdminSmtpController@save', ['auth', 'admin']);
$router->post('/admin/smtp-settings/test-connection', 'Admin\Controllers\AdminSmtpController@testConnection', ['auth', 'admin']);
$router->post('/admin/smtp-settings/send-test', 'Admin\Controllers\AdminSmtpController@sendTestEmail', ['auth', 'admin']);

// ─── Admin — Offer Popup ──────────────────────────────────────────────────────

$router->get('/admin/offer-popup', 'Admin\Controllers\AdminOfferPopupController@index', ['auth', 'admin']);
$router->post('/admin/offer-popup', 'Admin\Controllers\AdminOfferPopupController@update', ['auth', 'admin']);

// ─── Admin — Gallery ──────────────────────────────────────────────────────────

$router->get('/admin/gallery', 'Admin\Controllers\AdminGalleryController@index', ['auth', 'admin']);
$router->post('/admin/gallery', 'Admin\Controllers\AdminGalleryController@store', ['auth', 'admin']);
$router->get('/admin/gallery/{id}/edit', 'Admin\Controllers\AdminGalleryController@edit', ['auth', 'admin']);
$router->post('/admin/gallery/{id}', 'Admin\Controllers\AdminGalleryController@update', ['auth', 'admin']);
$router->post('/admin/gallery/{id}/toggle', 'Admin\Controllers\AdminGalleryController@toggle', ['auth', 'admin']);
$router->post('/admin/gallery/{id}/delete', 'Admin\Controllers\AdminGalleryController@delete', ['auth', 'admin']);

// ─── Admin — Toppers ──────────────────────────────────────────────────────────

$router->get('/admin/toppers', 'Admin\Controllers\AdminTopperController@index', ['auth', 'admin']);
$router->post('/admin/toppers', 'Admin\Controllers\AdminTopperController@store', ['auth', 'admin']);
$router->get('/admin/toppers/{id}/edit', 'Admin\Controllers\AdminTopperController@edit', ['auth', 'admin']);
$router->post('/admin/toppers/{id}', 'Admin\Controllers\AdminTopperController@update', ['auth', 'admin']);
$router->post('/admin/toppers/{id}/toggle', 'Admin\Controllers\AdminTopperController@toggle', ['auth', 'admin']);
$router->post('/admin/toppers/{id}/delete', 'Admin\Controllers\AdminTopperController@delete', ['auth', 'admin']);

// ─── Admin — Affiliations & Partners ──────────────────────────────────────────

$router->get('/admin/affiliations', 'Admin\Controllers\AdminAffiliationController@index', ['auth', 'admin']);
$router->post('/admin/affiliations', 'Admin\Controllers\AdminAffiliationController@store', ['auth', 'admin']);
$router->post('/admin/affiliations/{id}', 'Admin\Controllers\AdminAffiliationController@update', ['auth', 'admin']);
$router->post('/admin/affiliations/{id}/delete', 'Admin\Controllers\AdminAffiliationController@delete', ['auth', 'admin']);

// ─── Admin — Blog Posts ──────────────────────────────────────────────────────

$router->get('/admin/blog-posts', 'Admin\Controllers\AdminBlogController@index', ['auth', 'admin']);
$router->get('/admin/blog-posts/create', 'Admin\Controllers\AdminBlogController@create', ['auth', 'admin']);
$router->post('/admin/blog-posts', 'Admin\Controllers\AdminBlogController@store', ['auth', 'admin']);
$router->get('/admin/blog-posts/{id}/edit', 'Admin\Controllers\AdminBlogController@edit', ['auth', 'admin']);
$router->post('/admin/blog-posts/{id}', 'Admin\Controllers\AdminBlogController@update', ['auth', 'admin']);
$router->post('/admin/blog-posts/{id}/delete', 'Admin\Controllers\AdminBlogController@delete', ['auth', 'admin']);
$router->post('/admin/blog-posts/{id}/toggle-publish', 'Admin\Controllers\AdminBlogController@togglePublish', ['auth', 'admin']);

// ─── Admin — Demo Class Requests ──────────────────────────────────────────────

$router->get('/admin/demo-class', 'Admin\Controllers\AdminDemoClassController@index', ['auth', 'admin']);
$router->post('/admin/demo-class/{id}/status', 'Admin\Controllers\AdminDemoClassController@updateStatus', ['auth', 'admin']);
$router->post('/admin/demo-class/{id}/read', 'Admin\Controllers\AdminDemoClassController@markRead', ['auth', 'admin']);
$router->post('/admin/demo-class/{id}/delete', 'Admin\Controllers\AdminDemoClassController@delete', ['auth', 'admin']);

// ─── Admin — Practice Sets ──────────────────────────────────────────────────

$router->get('/admin/practice-sets', 'Admin\Controllers\AdminPracticeSetController@index', ['auth', 'admin']);
$router->get('/admin/practice-sets/create', 'Admin\Controllers\AdminPracticeSetController@create', ['auth', 'admin']);
$router->get('/admin/practice-sets/download-template', 'Admin\Controllers\AdminPracticeSetController@downloadTemplate', ['auth', 'admin']);
$router->post('/admin/practice-sets', 'Admin\Controllers\AdminPracticeSetController@store', ['auth', 'admin']);
$router->get('/admin/practice-sets/{id}/edit', 'Admin\Controllers\AdminPracticeSetController@edit', ['auth', 'admin']);
$router->post('/admin/practice-sets/{id}', 'Admin\Controllers\AdminPracticeSetController@update', ['auth', 'admin']);
$router->post('/admin/practice-sets/{id}/delete', 'Admin\Controllers\AdminPracticeSetController@delete', ['auth', 'admin']);
$router->get('/admin/practice-sets/{id}/questions', 'Admin\Controllers\AdminPracticeSetController@questions', ['auth', 'admin']);
$router->post('/admin/practice-sets/{id}/questions', 'Admin\Controllers\AdminPracticeSetController@addQuestion', ['auth', 'admin']);
$router->post('/admin/practice-sets/{id}/questions/{question_id}/delete', 'Admin\Controllers\AdminPracticeSetController@deleteQuestion', ['auth', 'admin']);
$router->post('/admin/practice-sets/{id}/import', 'Admin\Controllers\AdminPracticeSetController@importQuestions', ['auth', 'admin']);
$router->get('/admin/practice-sets/{id}/attempts', 'Admin\Controllers\AdminPracticeSetController@attempts', ['auth', 'admin']);
$router->post('/admin/practice-sets/{id}/attempts/{attempt_id}/delete', 'Admin\Controllers\AdminPracticeSetController@deleteAttempt', ['auth', 'admin']);
$router->post('/admin/practice-sets/{id}/toggle-publish', 'Admin\Controllers\AdminPracticeSetController@togglePublish', ['auth', 'admin']);
$router->post('/admin/practice-sets/{id}/toggle-result', 'Admin\Controllers\AdminPracticeSetController@toggleResultPublish', ['auth', 'admin']);

// ─── Admin — Newsletter ──────────────────────────────────────────────────────

$router->get('/admin/newsletter', 'Admin\Controllers\AdminNewsletterController@index', ['auth', 'admin']);
$router->post('/admin/newsletter/{id}/toggle-status', 'Admin\Controllers\AdminNewsletterController@toggleStatus', ['auth', 'admin']);
$router->post('/admin/newsletter/{id}/delete', 'Admin\Controllers\AdminNewsletterController@delete', ['auth', 'admin']);
$router->get('/admin/newsletter/compose', 'Admin\Controllers\AdminNewsletterController@compose', ['auth', 'admin']);
$router->post('/admin/newsletter/send', 'Admin\Controllers\AdminNewsletterController@send', ['auth', 'admin']);
$router->get('/admin/newsletter/logs', 'Admin\Controllers\AdminNewsletterController@logs', ['auth', 'admin']);

// ─── Admin — Video Testimonials ──────────────────────────────────────────────

$router->get('/admin/video-testimonials', 'Admin\Controllers\AdminVideoTestimonialController@index', ['auth', 'admin']);
$router->get('/admin/video-testimonials/create', 'Admin\Controllers\AdminVideoTestimonialController@create', ['auth', 'admin']);
$router->post('/admin/video-testimonials', 'Admin\Controllers\AdminVideoTestimonialController@store', ['auth', 'admin']);
$router->get('/admin/video-testimonials/{id}/edit', 'Admin\Controllers\AdminVideoTestimonialController@edit', ['auth', 'admin']);
$router->post('/admin/video-testimonials/{id}', 'Admin\Controllers\AdminVideoTestimonialController@update', ['auth', 'admin']);
$router->post('/admin/video-testimonials/{id}/toggle-status', 'Admin\Controllers\AdminVideoTestimonialController@toggleStatus', ['auth', 'admin']);
$router->post('/admin/video-testimonials/{id}/delete', 'Admin\Controllers\AdminVideoTestimonialController@delete', ['auth', 'admin']);

// ─── Admin — Batch Schedules ────────────────────────────────────────────────

$router->get('/admin/batch-schedules', 'Admin\Controllers\AdminBatchScheduleController@index', ['auth', 'admin']);
$router->get('/admin/batch-schedules/create', 'Admin\Controllers\AdminBatchScheduleController@create', ['auth', 'admin']);
$router->post('/admin/batch-schedules', 'Admin\Controllers\AdminBatchScheduleController@store', ['auth', 'admin']);
$router->get('/admin/batch-schedules/{id}/edit', 'Admin\Controllers\AdminBatchScheduleController@edit', ['auth', 'admin']);
$router->post('/admin/batch-schedules/{id}', 'Admin\Controllers\AdminBatchScheduleController@update', ['auth', 'admin']);
$router->post('/admin/batch-schedules/{id}/delete', 'Admin\Controllers\AdminBatchScheduleController@delete', ['auth', 'admin']);

// ─── Admin — News ──────────────────────────────────────────────────────────

$router->get('/admin/news', 'Admin\Controllers\AdminNewsController@index', ['auth', 'admin']);
$router->get('/admin/news/create', 'Admin\Controllers\AdminNewsController@create', ['auth', 'admin']);
$router->post('/admin/news', 'Admin\Controllers\AdminNewsController@store', ['auth', 'admin']);
$router->get('/admin/news/{id}/edit', 'Admin\Controllers\AdminNewsController@edit', ['auth', 'admin']);
$router->post('/admin/news/{id}', 'Admin\Controllers\AdminNewsController@update', ['auth', 'admin']);
$router->post('/admin/news/{id}/toggle-publish', 'Admin\Controllers\AdminNewsController@togglePublish', ['auth', 'admin']);
$router->post('/admin/news/{id}/delete', 'Admin\Controllers\AdminNewsController@delete', ['auth', 'admin']);

// ─── Frontend Chat API (Public — for visitor chat widget) ──────────────────

$router->post('/chat/init', 'ChatController@init');
$router->post('/chat/send', 'ChatController@send');
$router->get('/chat/poll', 'ChatController@poll');

// ─── Admin — Live Chat ──────────────────────────────────────────────────────

$router->get('/admin/chat', 'Admin\Controllers\AdminChatController@index', ['auth', 'admin']);
$router->get('/admin/chat/{id}', 'Admin\Controllers\AdminChatController@show', ['auth', 'admin']);
$router->post('/admin/chat/{id}/reply', 'Admin\Controllers\AdminChatController@reply', ['auth', 'admin']);
$router->post('/admin/chat/{id}/close', 'Admin\Controllers\AdminChatController@close', ['auth', 'admin']);
$router->post('/admin/chat/{id}/delete', 'Admin\Controllers\AdminChatController@delete', ['auth', 'admin']);
$router->get('/admin/chat/{id}/poll', 'Admin\Controllers\AdminChatController@poll', ['auth', 'admin']);
$router->get('/admin/chat/unread-count', 'Admin\Controllers\AdminChatController@unreadCount', ['auth', 'admin']);

// ════════════════════════════════════════════════════════════════════════════
// SUPER ADMIN ROUTES — /super-admin/*
// Loaded from routes/super_admin.php (separate file for clarity)
// ════════════════════════════════════════════════════════════════════════════

$superAdminRoutes = dirname(__FILE__) . '/super_admin.php';
if (file_exists($superAdminRoutes)) {
    require $superAdminRoutes;
}

// ════════════════════════════════════════════════════════════════════════════
// BRANCH ADMIN ROUTES — /branch/*
// Loaded from routes/branch.php (separate file for clarity)
// ════════════════════════════════════════════════════════════════════════════

$branchRoutes = dirname(__FILE__) . '/branch.php';
if (file_exists($branchRoutes)) {
    require $branchRoutes;
}

// ══════════════════════════════════════════════════════════════════════════════
// PUBLIC VERIFICATION PORTAL — /verify/*
// (Anyone can verify certificates/marksheets by document number or QR scan)
// ══════════════════════════════════════════════════════════════════════════════

$router->get('/verify', 'App\Controllers\VerificationController@index');
$router->get('/verify/{documentNumber}', 'App\Controllers\VerificationController@verify');
$router->post('/verify', 'App\Controllers\VerificationController@search');

// ─── Return the router instance ──────────────────────────────────────────────

return $router;
