<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Admin\DashboardController;
use App\Http\Controllers\Admin\StudentController;
use App\Http\Controllers\Admin\ExamController;
use App\Http\Controllers\Admin\FeeController;
use App\Http\Controllers\Admin\AttendanceController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Teacher\TeacherController;
use App\Http\Controllers\Student\StudentController as StudentPortal;
use App\Http\Controllers\Parent\ParentController;

// Auth
Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'showLogin'])->name('login');
    Route::post('/login', [LoginController::class, 'login'])->name('login.post');
    Route::get('/2fa', [LoginController::class, 'show2fa'])->name('auth.2fa');
    Route::post('/2fa', [LoginController::class, 'verify2fa'])->name('auth.2fa.verify');
});

Route::post('/logout', [LoginController::class, 'logout'])->name('logout')->middleware('auth');

// Redirect root
Route::get('/', fn() => redirect()->route('login'))->name('home');

// Admin & Super Admin
Route::middleware(['auth', 'role:super-admin|admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');

    // Students
    Route::resource('students', StudentController::class);

    // Exams
    Route::get('/exams', [ExamController::class, 'index'])->name('exams.index');
    Route::post('/exams', [ExamController::class, 'store'])->name('exams.store');
    Route::get('/exams/{exam}/marks/{stream}/{subject}', [ExamController::class, 'marksEntry'])->name('exams.marks-entry');
    Route::post('/exams/{exam}/marks/{stream}/{subject}', [ExamController::class, 'saveMarks'])->name('exams.marks-save');
    Route::get('/exams/{exam}/results/{stream}', [ExamController::class, 'results'])->name('exams.results');
    Route::get('/exams/{exam}/report-card/{student}', [ExamController::class, 'reportCard'])->name('exams.report-card');
    Route::post('/exams/{exam}/publish', [ExamController::class, 'publish'])->name('exams.publish');

    // Fees
    Route::get('/fees/structure', [FeeController::class, 'structure'])->name('fees.structure');
    Route::post('/fees/structure', [FeeController::class, 'storeStructure'])->name('fees.structure.store');
    Route::post('/fees/generate-invoices', [FeeController::class, 'generateInvoices'])->name('fees.generate');
    Route::get('/fees/invoices', [FeeController::class, 'invoices'])->name('fees.invoices');
    Route::post('/fees/payment', [FeeController::class, 'recordPayment'])->name('fees.payment');
    Route::get('/fees/receipt/{payment}', [FeeController::class, 'receipt'])->name('fees.receipt');

    // Attendance
    Route::get('/attendance', [AttendanceController::class, 'index'])->name('attendance.index');
    Route::get('/attendance/{stream}/take', [AttendanceController::class, 'take'])->name('attendance.take');
    Route::post('/attendance/{stream}/save', [AttendanceController::class, 'save'])->name('attendance.save');
    Route::get('/attendance/report', [AttendanceController::class, 'report'])->name('attendance.report');

    // Users
    Route::get('/users', [UserController::class, 'index'])->name('users.index');
    Route::post('/users', [UserController::class, 'store'])->name('users.store');
    Route::post('/users/{user}/toggle-active', [UserController::class, 'toggleActive'])->name('users.toggle');
    Route::post('/users/{user}/reset-password', [UserController::class, 'resetPassword'])->name('users.reset-password');
});

// Teacher Portal
Route::middleware(['auth', 'role:teacher'])->prefix('teacher')->name('teacher.')->group(function () {
    Route::get('/dashboard', [TeacherController::class, 'dashboard'])->name('dashboard');
    Route::get('/timetable', [TeacherController::class, 'timetable'])->name('timetable');
    Route::get('/marks', [TeacherController::class, 'marksEntry'])->name('marks');
    Route::post('/marks', [TeacherController::class, 'saveMarks'])->name('marks.save');
    Route::get('/attendance', [TeacherController::class, 'attendance'])->name('attendance');
    Route::post('/assignments', [TeacherController::class, 'storeAssignment'])->name('assignments.store');
});

// Student Portal
Route::middleware(['auth', 'role:student'])->prefix('student')->name('student.')->group(function () {
    Route::get('/dashboard', [StudentPortal::class, 'dashboard'])->name('dashboard');
    Route::get('/results', [StudentPortal::class, 'results'])->name('results');
    Route::get('/fees', [StudentPortal::class, 'fees'])->name('fees');
    Route::get('/attendance', [StudentPortal::class, 'attendance'])->name('attendance');
    Route::get('/assignments', [StudentPortal::class, 'assignments'])->name('assignments');
});

// Parent Portal
Route::middleware(['auth', 'role:parent'])->prefix('parent')->name('parent.')->group(function () {
    Route::get('/dashboard', [ParentController::class, 'dashboard'])->name('dashboard');
    Route::get('/student/{studentId}', [ParentController::class, 'studentDetail'])->name('student');
});

// Shared dashboard redirect
Route::middleware('auth')->get('/dashboard', function () {
    $user = auth()->user();
    if ($user->hasRole(['super-admin', 'admin'])) return redirect()->route('admin.dashboard');
    if ($user->hasRole('teacher')) return redirect()->route('teacher.dashboard');
    if ($user->hasRole('student')) return redirect()->route('student.dashboard');
    if ($user->hasRole('parent')) return redirect()->route('parent.dashboard');
    return redirect()->route('login');
})->name('dashboard');
