<?php
use Core\Router;

function registerRoutes(Router $r): void {
    // Auth
    $r->get('/',              'AuthController@showLogin');
    $r->get('/login',         'AuthController@showLogin');
    $r->post('/login',        'AuthController@login');
    $r->get('/2fa',           'AuthController@show2fa');
    $r->post('/2fa',          'AuthController@verify2fa');
    $r->get('/logout',        'AuthController@logout');
    $r->post('/session/ping', 'AuthController@ping');

    // Dashboard redirect (fallback)
    $r->get('/dashboard',     'DashboardController@index');
    $r->get('/home',          'DashboardController@index');

    // ── ADMIN ──────────────────────────────────────────────
    $r->get('/admin/dashboard',          'Admin\DashboardController@index');

    // Students
    $r->get('/admin/students',                        'Admin\StudentController@index');
    $r->get('/admin/students/print',                  'Admin\StudentController@printList');
    $r->get('/admin/students/download-template',      'Admin\StudentController@downloadTemplate');
    $r->post('/admin/students/bulk-import',           'Admin\StudentController@bulkImport');
    $r->get('/admin/students/create',                 'Admin\StudentController@create');
    $r->post('/admin/students/store',                 'Admin\StudentController@store');
    $r->get('/admin/students/{id}',                   'Admin\StudentController@show');
    $r->get('/admin/students/{id}/edit',              'Admin\StudentController@edit');
    $r->post('/admin/students/{id}/update',           'Admin\StudentController@update');
    $r->post('/admin/students/{id}/delete',           'Admin\StudentController@destroy');

    // Teachers (Admin)
    $r->get('/admin/teachers',                           'Admin\TeacherAdminController@index');
    $r->post('/admin/teachers/store',                    'Admin\TeacherAdminController@store');
    $r->get('/admin/teachers/{id}',                      'Admin\TeacherAdminController@show');
    $r->post('/admin/teachers/{id}/update',              'Admin\TeacherAdminController@update');
    $r->post('/admin/teachers/{id}/toggle',              'Admin\TeacherAdminController@toggle');
    $r->post('/admin/teachers/{id}/assign-class-teacher','Admin\TeacherAdminController@assignClassTeacher');
    $r->post('/admin/teachers/{id}/remove-class-teacher','Admin\TeacherAdminController@removeClassTeacher');
    $r->post('/admin/teachers/{id}/assign-subject',      'Admin\TeacherAdminController@assignSubject');
    $r->post('/admin/teachers/{id}/remove-subject',      'Admin\TeacherAdminController@removeSubject');
    $r->post('/admin/teachers/{id}/reset-password',      'Admin\TeacherAdminController@resetPassword');

    // Users
    $r->get('/admin/users',              'Admin\UserController@index');
    $r->post('/admin/users/store',       'Admin\UserController@store');
    $r->post('/admin/users/{id}/toggle', 'Admin\UserController@toggle');
    $r->post('/admin/users/{id}/reset-password', 'Admin\UserController@resetPassword');

    // Exams
    $r->get('/admin/exams',              'Admin\ExamController@index');
    $r->post('/admin/exams/store',       'Admin\ExamController@store');
    $r->post('/admin/exams/{id}/update',  'Admin\ExamController@update');
    $r->get('/admin/exams/{id}/marks',   'Admin\ExamController@marksEntry');
    $r->post('/admin/exams/{id}/marks',  'Admin\ExamController@saveMarks');
    $r->get('/admin/exams/{id}/results', 'Admin\ExamController@results');
    $r->post('/admin/exams/{id}/send-results', 'Admin\ExamController@sendResultsToParents');
    $r->post('/admin/exams/{id}/publish','Admin\ExamController@publish');
    $r->get('/admin/exams/{id}/report/{studentId}', 'Admin\ExamController@reportCard');

    // Attendance
    $r->get('/admin/attendance',                    'Admin\AttendanceController@index');
    $r->get('/admin/attendance/{streamId}/take',    'Admin\AttendanceController@take');
    $r->post('/admin/attendance/{streamId}/save',   'Admin\AttendanceController@save');
    $r->get('/admin/attendance/report',             'Admin\AttendanceController@report');

    // Fees
    $r->get('/admin/finance',                        'Admin\FeeController@dashboard');
    $r->get('/admin/fees/structure',                 'Admin\FeeController@structure');
    $r->post('/admin/fees/structure/store',          'Admin\FeeController@storeStructure');
    $r->post('/admin/fees/structure/{id}/update',    'Admin\FeeController@updateStructure');
    $r->post('/admin/fees/structure/{id}/delete',    'Admin\FeeController@deleteStructure');
    $r->post('/admin/fees/generate',                 'Admin\FeeController@generateInvoices');
    $r->get('/admin/fees/invoices',                  'Admin\FeeController@invoices');
    $r->get('/admin/fees/invoices/print',             'Admin\FeeController@printInvoices');
    $r->post('/admin/fees/invoice/{id}/update',      'Admin\FeeController@updateInvoice');
    $r->post('/admin/fees/invoice/{id}/delete',      'Admin\FeeController@deleteInvoice');
    $r->post('/admin/fees/payment',                  'Admin\FeeController@recordPayment');
    $r->get('/admin/fees/receipt/{id}',              'Admin\FeeController@receipt');
    $r->get('/admin/fees/invoice/{id}/history',      'Admin\FeeController@paymentHistory');
    $r->get('/admin/fees/aging',                     'Admin\FeeController@aging');
    $r->get('/admin/fees/scholarships',              'Admin\FeeController@scholarships');
    $r->post('/admin/fees/scholarships/store',       'Admin\FeeController@storeScholarship');
    $r->post('/admin/fees/scholarships/{id}/delete', 'Admin\FeeController@deleteScholarship');
    $r->get('/admin/fees/expenses',                  'Admin\FeeController@expenses');
    $r->post('/admin/fees/expenses/store',           'Admin\FeeController@storeExpense');
    $r->post('/admin/fees/expenses/{id}/delete',     'Admin\FeeController@deleteExpense');
    $r->get('/admin/fees/reports',                   'Admin\FeeController@reports');
    $r->get('/admin/fees/cashflow',                  'Admin\FeeController@cashflow');
    $r->get('/admin/fees/balancesheet',              'Admin\FeeController@balanceSheet');
    $r->get('/admin/fees/voteheads',                 'Admin\FeeController@voteHeads');
    $r->post('/admin/fees/voteheads/store',          'Admin\FeeController@storeVoteHead');
    $r->post('/admin/fees/voteheads/{id}/update',    'Admin\FeeController@updateVoteHead');
    $r->post('/admin/fees/voteheads/{id}/delete',    'Admin\FeeController@deleteVoteHead');
    $r->get('/admin/fees/lpos',                      'Admin\FeeController@lpos');
    $r->post('/admin/fees/lpos/store',               'Admin\FeeController@storeLpo');
    $r->post('/admin/fees/lpos/{id}/status',         'Admin\FeeController@updateLpoStatus');
    $r->post('/admin/fees/lpos/{id}/delete',         'Admin\FeeController@deleteLpo');
    $r->get('/admin/fees/cashbook',                  'Admin\FeeController@cashbook');
    $r->post('/admin/fees/cashbook/store',           'Admin\FeeController@storeCashTransaction');
    $r->get('/admin/fees/accounts',                  'Admin\FeeController@accounts');
    $r->post('/admin/fees/accounts/store',           'Admin\FeeController@storeAccount');

    // Academic setup
    $r->get('/admin/academic',               'Admin\AcademicController@index');
    $r->post('/admin/academic/year/store',   'Admin\AcademicController@storeYear');
    $r->post('/admin/academic/term/store',   'Admin\AcademicController@storeTerm');
    $r->post('/admin/academic/grade/store',          'Admin\AcademicController@storeGrade');
    $r->post('/admin/academic/grade/{id}/update',    'Admin\AcademicController@updateGrade');
    $r->post('/admin/academic/grade/{id}/delete',    'Admin\AcademicController@deleteGrade');
    $r->post('/admin/academic/subject/{id}/update',  'Admin\AcademicController@updateSubject');
    $r->post('/admin/academic/subject/{id}/delete',  'Admin\AcademicController@deleteSubject');
    $r->post('/admin/academic/stream/store',         'Admin\AcademicController@storeStream');
    $r->post('/admin/academic/stream/{id}/update',  'Admin\AcademicController@updateStream');
    $r->post('/admin/academic/stream/{id}/delete',  'Admin\AcademicController@deleteStream');
    $r->post('/admin/academic/subject/store','Admin\AcademicController@storeSubject');

    // Analytics
    $r->get('/admin/analytics',                      'Admin\AnalyticsController@index');
    $r->get('/admin/analytics/learner/{id}',          'Admin\AnalyticsController@learnerProfile');

    // Competency Ratings
    $r->get('/admin/competency',                      'Admin\CompetencyController@index');
    $r->post('/admin/competency/save',                'Admin\CompetencyController@save');

    // Notifications
    $r->get('/admin/notifications',                          'Admin\NotificationController@index');
    $r->post('/admin/notifications/send',                    'Admin\NotificationController@send');
    $r->post('/admin/notifications/templates/save',          'Admin\NotificationController@saveTemplate');
    $r->post('/admin/notifications/templates/{id}/delete',   'Admin\NotificationController@deleteTemplate');
    $r->get('/admin/notifications/unread',                   'Admin\NotificationController@unreadCount');
    $r->post('/admin/notifications/mark-read',               'Admin\NotificationController@markRead');
    $r->get('/admin/notifications/{id}/view',                'Admin\NotificationController@viewNotification');
    $r->post('/admin/notifications/{id}/delete',             'Admin\NotificationController@delete');
    $r->post('/admin/notifications/bulk-delete',             'Admin\NotificationController@bulkDelete');

    // Settings
    $r->get('/admin/settings',              'Admin\SettingsController@index');
    $r->post('/admin/settings/school',      'Admin\SettingsController@saveSchool');
    $r->post('/admin/settings/sms',         'Admin\SettingsController@saveSms');
    $r->post('/admin/settings/sms/test',    'Admin\SettingsController@testSms');
    $r->post('/admin/settings/mpesa',       'Admin\SettingsController@saveMpesa');
    $r->post('/admin/settings/email',       'Admin\SettingsController@saveEmail');
    $r->post('/admin/settings/recaptcha',   'Admin\SettingsController@saveRecaptcha');

    // Library
    $r->get('/admin/library',                        'Admin\LibraryController@index');
    $r->post('/admin/library/store',                 'Admin\LibraryController@storeBook');
    $r->post('/admin/library/{id}/update',           'Admin\LibraryController@updateBook');
    $r->post('/admin/library/{id}/delete',           'Admin\LibraryController@deleteBook');
    $r->post('/admin/library/borrow',                'Admin\LibraryController@borrow');
    $r->get('/admin/library/borrowings',             'Admin\LibraryController@borrowings');
    $r->post('/admin/library/borrowings/{id}/return','Admin\LibraryController@returnBook');

    // SMS
    $r->get('/admin/sms',                            'Admin\SmsController@index');
    $r->post('/admin/sms/send',                      'Admin\SmsController@send');

    // Activity logs
    $r->get('/admin/logs',                   'Admin\DashboardController@logs');

    // ── ACCOUNTANT ──────────────────────────────────────
    $r->get('/accountant/dashboard', 'Accountant\AccountantController@dashboard');

    // ── DEPUTY ──────────────────────────────────────────
    $r->get('/deputy/dashboard', 'Deputy\DeputyController@dashboard');

    // ── LIBRARIAN ───────────────────────────────────────
    $r->get('/librarian/dashboard', 'Librarian\LibrarianController@dashboard');

    // ── TEACHER ────────────────────────────────────────────
    $r->get('/teacher/dashboard',            'Teacher\TeacherController@dashboard');
    $r->get('/teacher/my-learners',          'Teacher\TeacherController@myLearners');
    $r->post('/teacher/my-learners/store',   'Teacher\TeacherController@storeLearner');
    $r->get('/teacher/class-fees',           'Teacher\TeacherController@classFees');
    $r->post('/teacher/class-fees/reminder', 'Teacher\TeacherController@sendFeeReminder');
    $r->get('/teacher/timetable',            'Teacher\TeacherController@timetable');
    $r->get('/teacher/marks',                'Teacher\TeacherController@marksEntry');
    $r->post('/teacher/marks/save',          'Teacher\TeacherController@saveMarks');
    $r->get('/teacher/attendance',           'Teacher\TeacherController@attendance');
    $r->post('/teacher/attendance/save',     'Teacher\TeacherController@saveAttendance');
    $r->get('/teacher/assignments',          'Teacher\TeacherController@assignments');
    $r->post('/teacher/assignments/store',   'Teacher\TeacherController@storeAssignment');

    // ── STUDENT ────────────────────────────────────────────
    $r->get('/student/dashboard',            'Student\StudentController@dashboard');
    $r->get('/student/results',              'Student\StudentController@results');
    $r->get('/student/fees',                 'Student\StudentController@fees');
    $r->get('/student/attendance',           'Student\StudentController@attendance');
    $r->get('/student/assignments',          'Student\StudentController@assignments');

    // ── PARENT ─────────────────────────────────────────────
    $r->get('/parent/dashboard',                     'Parent\ParentController@dashboard');
    $r->get('/parent/student/{id}',                  'Parent\ParentController@studentDetail');
    $r->get('/parent/student/{id}/results/{examId}', 'Parent\ParentController@downloadResults');
    $r->post('/parent/student/{id}/pay',             'Parent\ParentController@initiatePayment');
    $r->get('/parent/student/{id}/pay/status',       'Parent\ParentController@paymentStatus');
    $r->get('/parent/fees/history/{studentId}',      'Parent\ParentController@feeHistory');
    // Admin prompt-to-pay
    $r->post('/admin/fees/prompt-pay',               'Admin\FeeController@promptToPay');
    // M-Pesa callback (no auth)
    $r->post('/mpesa/callback',                      'MpesaCallbackController@handle');
    $r->get('/parent/student/{studentId}/results/{examId}', 'Parent\ParentController@downloadResults');

    // Storage files
    $r->get('/storage/uploads/{folder}/{file}',  'StorageController@serveSubdir');
    $r->get('/storage/uploads/{file}',            'StorageController@serve');

    // Profile
    $r->get('/profile',                      'ProfileController@index');
    $r->post('/profile/update',              'ProfileController@update');
    $r->post('/profile/password',            'ProfileController@changePassword');
}
