<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\ExportController;
use App\Http\Controllers\ImportController;
use App\Http\Controllers\LandingController;
use App\Http\Controllers\Modules\SchoolModuleController;
use App\Http\Controllers\ProfileCompletionController;
use App\Http\Controllers\School\AbsenceReportController;
use App\Http\Controllers\School\AuditLogController as SchoolAuditLogController;
use App\Http\Controllers\School\UserActivityController;
use App\Http\Controllers\School\AttendanceApprovalController;
use App\Http\Controllers\School\AttendanceController;
use App\Http\Controllers\School\AnnouncementController;
use App\Http\Controllers\School\GradeController as SchoolGradeController;
use App\Http\Controllers\School\MessageController;
use App\Http\Controllers\School\OnlineClassController;
use App\Http\Controllers\School\SettingsController as SchoolSettingsController;
use App\Http\Controllers\School\FilesController;
use App\Http\Controllers\School\MoavenPermissionsController;
use App\Http\Controllers\School\UserController as SchoolUserController;
use App\Http\Controllers\School\SubjectController;
use App\Http\Controllers\SuperAdmin\SchoolController as SuperAdminSchoolController;
use App\Http\Controllers\School\ClassroomController;
use App\Http\Controllers\School\DashboardController as SchoolDashboardController;
use App\Http\Controllers\School\ReportController;
use App\Http\Controllers\School\ScheduleController;
use App\Http\Controllers\School\StudentController;
use App\Http\Controllers\School\TeacherController;
use App\Http\Controllers\Sms\SmsMessageController;
use App\Http\Controllers\Sms\SmsTemplateController;
use App\Http\Controllers\Sms\SmsWalletController;
use App\Http\Controllers\Student\DashboardController as StudentDashboardController;
use App\Http\Controllers\Student\AttendanceController as StudentAttendanceController;
use App\Http\Controllers\Student\ExamController as StudentExamController;
use App\Http\Controllers\Student\ScheduleController as StudentScheduleController;
use App\Http\Controllers\Student\GradesController as StudentGradesController;
use App\Http\Controllers\Student\HomeworkController as StudentHomeworkController;
use App\Http\Controllers\Student\OnlineClassController as StudentOnlineClassController;
use App\Http\Controllers\SuperAdmin\AuditLogController as SuperAdminAuditLogController;
use App\Http\Controllers\SuperAdmin\ModuleController;
use App\Http\Controllers\SuperAdmin\ReportsController as SuperAdminReportsController;
use App\Http\Controllers\SuperAdmin\SmsController as SuperAdminSmsController;
use App\Http\Controllers\SuperAdmin\DashboardController as SuperAdminDashboardController;
use App\Http\Controllers\Teacher\ExamController as TeacherExamController;
use App\Http\Controllers\Teacher\ExamQuestionController as TeacherExamQuestionController;
use App\Http\Controllers\Teacher\GradeController as TeacherGradeController;
use App\Http\Controllers\Teacher\DashboardController as TeacherDashboardController;
use App\Http\Controllers\Teacher\HomeworkController as TeacherHomeworkController;
use App\Http\Controllers\Voting\VotingController;
use App\Http\Controllers\Voting\CandidateController;
use Illuminate\Support\Facades\Route;

Route::get('/', [LandingController::class, 'home'])->name('landing.home');
Route::get('/pricing', [LandingController::class, 'pricing'])->name('landing.pricing');
Route::get('/faq', [LandingController::class, 'faq'])->name('landing.faq');
Route::get('/contact', [LandingController::class, 'contact'])->name('landing.contact');
Route::post('/contact', [LandingController::class, 'storeContact'])->name('landing.contact.store')->middleware('throttle:5,1');

Route::middleware('guest')->group(function (): void {
    Route::get('/login', [AuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [AuthController::class, 'login'])->name('login.store')->middleware('throttle:5,1');
    Route::get('/forgot-password', [AuthController::class, 'forgotPassword'])->name('password.request');
});

Route::middleware(['auth', 'no.private.cache'])->group(function (): void {
    Route::post('/logout', [AuthController::class, 'logout'])->name('logout');
    Route::post('/heartbeat', function (\Illuminate\Http\Request $request) {
        $userId = $request->user()?->id;
        if ($userId) {
            \Illuminate\Support\Facades\DB::table('user_heartbeats')->updateOrInsert(
                ['user_id' => $userId],
                ['user_id' => $userId, 'last_seen_at' => now()]
            );
        }
        return response()->json(['ok' => true]);
    })->name('heartbeat');
    Route::get('/complete-profile', [ProfileCompletionController::class, 'edit'])->name('profile.complete.edit');
    Route::post('/complete-profile', [ProfileCompletionController::class, 'update'])->name('profile.complete.update');

    Route::middleware(['profile.complete', 'school.active'])->group(function (): void {
        Route::get('/dashboard', [SchoolDashboardController::class, 'redirectByRole'])->name('dashboard');

        // Super Admin routes — role: super_admin
        Route::middleware('role:super_admin')->group(function (): void {
            Route::get('/super-admin', [SuperAdminDashboardController::class, 'index'])->name('super-admin.dashboard');
            Route::get('/super-admin/schools', [SuperAdminSchoolController::class, 'index'])->name('super-admin.schools');
            Route::post('/super-admin/schools', [SuperAdminSchoolController::class, 'store'])->name('super-admin.schools.store');
            Route::patch('/super-admin/schools/{id}/toggle', [SuperAdminSchoolController::class, 'toggleStatus'])->name('super-admin.schools.toggle');
            Route::post('/super-admin/schools/{id}/charge-sms', [SuperAdminSchoolController::class, 'chargeSmsWallet'])->name('super-admin.schools.charge-sms');
            Route::patch('/super-admin/schools/{id}/renew-license', [SuperAdminSchoolController::class, 'renewLicense'])->name('super-admin.schools.renew-license');
            Route::get('/super-admin/schools/{id}/modules', [SuperAdminSchoolController::class, 'manageModules'])->name('super-admin.schools.modules');
            Route::patch('/super-admin/schools/{id}/modules/{moduleId}/toggle', [SuperAdminSchoolController::class, 'toggleModule'])->name('super-admin.schools.modules.toggle');
            Route::get('/super-admin/modules', [ModuleController::class, 'index'])->name('super-admin.modules');
            Route::get('/super-admin/sms-usage', [SuperAdminSmsController::class, 'index'])->name('super-admin.sms');
            Route::post('/super-admin/sms-charge-requests/{id}/approve', [SuperAdminSmsController::class, 'approveChargeRequest'])->name('super-admin.sms.charge.approve');
            Route::post('/super-admin/sms-charge-requests/{id}/reject', [SuperAdminSmsController::class, 'rejectChargeRequest'])->name('super-admin.sms.charge.reject');
            Route::get('/super-admin/audit-logs', [SuperAdminAuditLogController::class, 'index'])->name('super-admin.audit-logs');
            Route::get('/super-admin/reports', [SuperAdminReportsController::class, 'index'])->name('super-admin.reports');
            Route::get('/super-admin/schools/{id}/export', [SuperAdminSchoolController::class, 'exportZip'])->name('super-admin.schools.export');
        });

        // School admin routes — roles: owner, principal, moaven, support
        Route::middleware('role:owner,principal,moaven,support')->group(function (): void {
            Route::get('/school', [SchoolDashboardController::class, 'index'])->name('school.dashboard');
            Route::get('/school/users', [SchoolUserController::class, 'index'])->name('school.users');
            Route::post('/school/users', [SchoolUserController::class, 'store'])->name('school.users.store');
            Route::put('/school/users/{id}', [SchoolUserController::class, 'update'])->name('school.users.update');
            Route::post('/school/users/{id}/reset-password', [SchoolUserController::class, 'resetPassword'])->name('school.users.reset-password');
            Route::get('/school/students', [StudentController::class, 'index'])->name('school.students');
            Route::post('/school/students', [StudentController::class, 'store'])->name('school.students.store');
            Route::put('/school/students/{id}', [StudentController::class, 'update'])->name('school.students.update');
            Route::delete('/school/students/{id}', [StudentController::class, 'destroy'])->name('school.students.destroy');
            Route::get('/school/teachers', [TeacherController::class, 'index'])->name('school.teachers');
            Route::post('/school/teachers', [TeacherController::class, 'store'])->name('school.teachers.store');
            Route::put('/school/teachers/{id}', [TeacherController::class, 'update'])->name('school.teachers.update');
            Route::delete('/school/teachers/{id}', [TeacherController::class, 'destroy'])->name('school.teachers.destroy');
            Route::get('/school/classes', [ClassroomController::class, 'index'])->name('school.classes');
            Route::post('/school/classes', [ClassroomController::class, 'store'])->name('school.classes.store');
            Route::put('/school/classes/{id}', [ClassroomController::class, 'update'])->name('school.classes.update');
            Route::delete('/school/classes/{id}', [ClassroomController::class, 'destroy'])->name('school.classes.destroy');
            Route::get('/school/grades', [SchoolGradeController::class, 'index'])->name('school.grades');
            Route::patch('/school/grades/{id}/approve', [SchoolGradeController::class, 'approve'])->name('school.grades.approve');
            Route::patch('/school/grades/{id}/reject', [SchoolGradeController::class, 'reject'])->name('school.grades.reject');
            Route::get('/school/report-card', [SchoolGradeController::class, 'reportCard'])->name('school.report-card');
            Route::get('/school/report-card/pdf', [SchoolGradeController::class, 'reportCardPdf'])->name('school.report-card.pdf');
            Route::get('/school/subjects', [SubjectController::class, 'index'])->name('school.subjects');
            Route::post('/school/subjects', [SubjectController::class, 'store'])->name('school.subjects.store');
            Route::put('/school/subjects/{id}', [SubjectController::class, 'update'])->name('school.subjects.update');
            Route::delete('/school/subjects/{id}', [SubjectController::class, 'destroy'])->name('school.subjects.destroy');
            Route::get('/school/schedule', [ScheduleController::class, 'index'])->name('school.schedule');
            Route::get('/school/attendance/approvals', [AttendanceApprovalController::class, 'index'])->name('school.attendance.approvals');
            Route::get('/school/announcements', [AnnouncementController::class, 'index'])->name('school.announcements');
            Route::post('/school/announcements', [AnnouncementController::class, 'store'])->name('school.announcements.store');
            Route::delete('/school/announcements/{id}', [AnnouncementController::class, 'destroy'])->name('school.announcements.destroy');
            Route::get('/school/messages', [MessageController::class, 'index'])->name('school.messages');
            Route::post('/school/messages', [MessageController::class, 'store'])->name('school.messages.store');
            Route::patch('/school/messages/{id}/read', [MessageController::class, 'markRead'])->name('school.messages.read');
            Route::get('/school/online-classes', [OnlineClassController::class, 'index'])->name('school.online-classes');
            Route::post('/school/online-classes', [OnlineClassController::class, 'store'])->name('school.online-classes.store');
            Route::get('/school/online-classes/{id}/logs', [OnlineClassController::class, 'logs'])->name('school.online-classes.logs');
            Route::delete('/school/online-classes/{id}', [OnlineClassController::class, 'destroy'])->name('school.online-classes.destroy');
            Route::get('/school/files', [FilesController::class, 'index'])->name('school.files');
            Route::post('/school/files', [FilesController::class, 'store'])->name('school.files.store');
            Route::get('/school/files/{id}/download', [FilesController::class, 'download'])->name('school.files.download');
            Route::delete('/school/files/{id}', [FilesController::class, 'destroy'])->name('school.files.destroy');
            Route::get('/school/absence-report', [AbsenceReportController::class, 'index'])->name('school.absence-report');
            Route::get('/school/user-activity', [UserActivityController::class, 'index'])->name('school.user-activity');
            Route::get('/school/audit-logs', [SchoolAuditLogController::class, 'index'])->name('school.audit-logs');
            Route::get('/school/settings', [SchoolSettingsController::class, 'index'])->name('school.settings');
            Route::put('/school/settings', [SchoolSettingsController::class, 'update'])->name('school.settings.update');
            Route::get('/school/reports', [ReportController::class, 'index'])->name('school.reports');
            Route::get('/school/modules', [SchoolModuleController::class, 'index'])->name('school.modules');
            Route::get('/school/module-store', [SchoolModuleController::class, 'store'])->name('school.module-store');
            Route::get('/school/sms-wallet', [SmsWalletController::class, 'index'])->name('school.sms.wallet');
            Route::post('/school/sms-wallet/request-charge', [SmsWalletController::class, 'requestCharge'])->name('school.sms.wallet.request-charge');
            Route::get('/school/sms-absents', [SmsMessageController::class, 'absents'])->name('school.sms.absents');
            Route::get('/school/sms-templates', [SmsTemplateController::class, 'index'])->name('school.sms.templates');
            Route::post('/school/sms-templates', [SmsTemplateController::class, 'store'])->name('school.sms.templates.store');
            Route::put('/school/sms-templates/{id}', [SmsTemplateController::class, 'update'])->name('school.sms.templates.update');
            Route::delete('/school/sms-templates/{id}', [SmsTemplateController::class, 'destroy'])->name('school.sms.templates.destroy');
            Route::post('/school/sms-absents', [SmsMessageController::class, 'storeAbsenceRequests'])->name('school.sms.absents.store');
            Route::get('/school/voting', [VotingController::class, 'index'])->name('school.voting');
            Route::get('/school/voting/create', [VotingController::class, 'create'])->name('school.voting.create');
            Route::post('/school/voting', [VotingController::class, 'store'])->name('school.voting.store');
            Route::get('/school/voting/{voting}/manage', [VotingController::class, 'manage'])->name('school.voting.manage');
            Route::patch('/school/voting/{voting}/status', [VotingController::class, 'updateStatus'])->name('school.voting.status');
            Route::delete('/school/voting/{voting}', [VotingController::class, 'destroy'])->name('school.voting.destroy');
            Route::get('/school/voting/results', [VotingController::class, 'results'])->name('school.voting.results');
            Route::post('/school/voting/{voting}/candidates', [CandidateController::class, 'store'])->name('school.voting.candidates.store');
            Route::delete('/school/voting/{voting}/candidates/{candidate}', [CandidateController::class, 'destroy'])->name('school.voting.candidates.destroy');
            Route::get('/school/users/{user}/permissions', [MoavenPermissionsController::class, 'show'])->name('school.users.permissions');
            Route::put('/school/users/{user}/permissions', [MoavenPermissionsController::class, 'update'])->name('school.users.permissions.update');
            Route::get('/school/import/students', [ImportController::class, 'students'])->name('school.import.students');
            Route::post('/school/import/students', [ImportController::class, 'storeStudents'])->name('school.import.students.store');
            Route::get('/school/import/teachers', [ImportController::class, 'teachers'])->name('school.import.teachers');
            Route::post('/school/import/teachers', [ImportController::class, 'storeTeachers'])->name('school.import.teachers.store');
            Route::get('/school/import/users', [ImportController::class, 'users'])->name('school.import.users');
            Route::post('/school/import/users', [ImportController::class, 'storeUsers'])->name('school.import.users.store');
            Route::get('/school/exports', [ExportController::class, 'index'])->name('school.exports');
            Route::get('/school/exports/users.csv', [ExportController::class, 'usersCsv'])->name('school.exports.users');
            Route::get('/school/exports/students.csv', [ExportController::class, 'studentsCsv'])->name('school.exports.students');
            Route::get('/school/exports/teachers.csv', [ExportController::class, 'teachersCsv'])->name('school.exports.teachers');
            Route::get('/school/exports/sms-usage.csv', [ExportController::class, 'smsUsageCsv'])->name('school.exports.sms');
            Route::get('/school/exports/voting-results.csv', [ExportController::class, 'votingResultsCsv'])->name('school.exports.voting');
            Route::get('/school/exports/attendance.csv', [ExportController::class, 'attendanceCsv'])->name('school.exports.attendance');
            Route::get('/school/exports/students.xlsx', [ExportController::class, 'studentsXlsx'])->name('school.exports.students.xlsx');
            Route::get('/school/exports/teachers.xlsx', [ExportController::class, 'teachersXlsx'])->name('school.exports.teachers.xlsx');
            Route::get('/school/exports/users.xlsx', [ExportController::class, 'usersXlsx'])->name('school.exports.users.xlsx');
            Route::get('/school/exports/school.zip', [ExportController::class, 'schoolZip'])->name('school.exports.zip');
        });

        // Teacher routes — roles: teacher, owner, principal, moaven
        Route::middleware('role:teacher,owner,principal,moaven')->group(function (): void {
            Route::get('/teacher', [TeacherDashboardController::class, 'index'])->name('teacher.dashboard');
            Route::get('/school/attendance', [AttendanceController::class, 'index'])->name('school.attendance');
            Route::get('/teacher/exams', [TeacherExamController::class, 'index'])->name('teacher.exams');
            Route::post('/teacher/exams', [TeacherExamController::class, 'store'])->name('teacher.exams.store');
            Route::get('/teacher/exams/{id}/questions', [TeacherExamQuestionController::class, 'index'])->name('teacher.exam.questions');
            Route::post('/teacher/exams/{id}/questions', [TeacherExamQuestionController::class, 'store'])->name('teacher.exam.questions.store');
            Route::delete('/teacher/exams/{id}/questions/{qid}', [TeacherExamQuestionController::class, 'destroy'])->name('teacher.exam.questions.destroy');
            Route::put('/teacher/exams/{id}/status', [TeacherExamQuestionController::class, 'updateStatus'])->name('teacher.exam.status');
            Route::get('/teacher/grades', [TeacherGradeController::class, 'index'])->name('teacher.grades');
            Route::post('/teacher/grades', [TeacherGradeController::class, 'store'])->name('teacher.grades.store');
            Route::put('/teacher/grades/{id}', [TeacherGradeController::class, 'update'])->name('teacher.grades.update');
            Route::get('/teacher/homeworks', [TeacherHomeworkController::class, 'index'])->name('teacher.homeworks');
            Route::post('/teacher/homeworks', [TeacherHomeworkController::class, 'store'])->name('teacher.homeworks.store');
            Route::delete('/teacher/homeworks/{id}', [TeacherHomeworkController::class, 'destroy'])->name('teacher.homeworks.destroy');
        });

        // Student routes — role: student
        Route::middleware('role:student')->group(function (): void {
            Route::get('/student', [StudentDashboardController::class, 'index'])->name('student.dashboard');
            Route::get('/student/online-classes/{id}/join', [StudentOnlineClassController::class, 'join'])->name('student.online-classes.join');
            Route::get('/student/voting', [VotingController::class, 'studentList'])->name('student.voting');
            Route::get('/student/voting/{voting}', [VotingController::class, 'vote'])->name('student.voting.vote');
            Route::post('/student/voting/{voting}/vote', [VotingController::class, 'castVote'])->name('student.voting.cast');
            Route::get('/student/homeworks', [StudentHomeworkController::class, 'index'])->name('student.homeworks');
            Route::post('/student/homeworks/{id}', [StudentHomeworkController::class, 'store'])->name('student.homeworks.store');
            Route::get('/student/grades', [StudentGradesController::class, 'index'])->name('student.grades');
            Route::get('/student/exams', [StudentExamController::class, 'index'])->name('student.exams');
            Route::get('/student/exams/{id}', [StudentExamController::class, 'show'])->name('student.exams.show');
            Route::post('/student/exams/{id}/answer', [StudentExamController::class, 'saveAnswer'])->name('student.exams.answer');
            Route::post('/student/exams/{id}/submit', [StudentExamController::class, 'submit'])->name('student.exams.submit');
            Route::get('/student/attendance', [StudentAttendanceController::class, 'index'])->name('student.attendance');
            Route::get('/student/schedule', [StudentScheduleController::class, 'index'])->name('student.schedule');
        });
    });
});
