<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;
use App\Http\Controllers\HomeController;

// Move this to the top - this will be your landing page
Route::get('/', [HomeController::class, 'index'])->name('home');

// Auth routes (login, register, etc.)
require __DIR__.'/auth.php';

// Protected routes (require authentication)
Route::middleware('auth')->group(function () {
    // Super Admin Routes
    Route::prefix('superadmin')->middleware('role:super_admin')->name('superadmin.')->group(function () {
        Route::get('/dashboard', [App\Http\Controllers\SuperAdmin\DashboardController::class, 'index'])
             ->name('dashboard');

        // Schools CRUD
        Route::resource('schools', \App\Http\Controllers\SuperAdmin\SchoolController::class);
    });

    // School Admin Routes
    Route::prefix('schooladmin')->middleware('role:school_admin')->name('schooladmin.')->group(function () {
        Route::get('/dashboard', function () {
            return view('schooladmin.dashboard');
        })->name('dashboard');

        // Students
        Route::resource('students', \App\Http\Controllers\SchoolAdmin\StudentController::class);

        // Attendance
        Route::get('attendance', [\App\Http\Controllers\SchoolAdmin\AttendanceController::class, 'index'])->name('attendance.index');
        Route::post('attendance', [\App\Http\Controllers\SchoolAdmin\AttendanceController::class, 'store'])->name('attendance.store');

        // Library Management
        Route::prefix('library')->name('library.')->group(function () {
            Route::resource('books', \App\Http\Controllers\SchoolAdmin\Library\BookController::class);
            Route::resource('loans', \App\Http\Controllers\SchoolAdmin\Library\BookLoanController::class);
            Route::post('loans/{loan}/return', [\App\Http\Controllers\SchoolAdmin\Library\BookLoanController::class, 'returnBook'])->name('loans.return');
            Route::get('fines', [\App\Http\Controllers\SchoolAdmin\Library\LibraryFineController::class, 'index'])->name('fines.index');
            Route::post('fines/{fine}/pay', [\App\Http\Controllers\SchoolAdmin\Library\LibraryFineController::class, 'pay'])->name('fines.pay');
        });

        // Staff / Teacher & Staff Management
        Route::prefix('staff')->name('staff.')->group(function () {
            Route::get('/',         [\App\Http\Controllers\SchoolAdmin\Staff\StaffController::class, 'index'])->name('index');
            Route::get('/create',   [\App\Http\Controllers\SchoolAdmin\Staff\StaffController::class, 'create'])->name('create');
            Route::post('/',        [\App\Http\Controllers\SchoolAdmin\Staff\StaffController::class, 'store'])->name('store');
            Route::get('/{staff}',  [\App\Http\Controllers\SchoolAdmin\Staff\StaffController::class, 'show'])->name('show');
            Route::get('/{staff}/edit',   [\App\Http\Controllers\SchoolAdmin\Staff\StaffController::class, 'edit'])->name('edit');
            Route::put('/{staff}',        [\App\Http\Controllers\SchoolAdmin\Staff\StaffController::class, 'update'])->name('update');
            Route::delete('/{staff}',     [\App\Http\Controllers\SchoolAdmin\Staff\StaffController::class, 'destroy'])->name('destroy');

            // Attendance
            Route::prefix('attendance')->name('attendance.')->group(function () {
                Route::get('/',       [\App\Http\Controllers\SchoolAdmin\Staff\StaffAttendanceController::class, 'index'])->name('index');
                Route::get('/mark',   [\App\Http\Controllers\SchoolAdmin\Staff\StaffAttendanceController::class, 'mark'])->name('mark');
                Route::post('/mark',  [\App\Http\Controllers\SchoolAdmin\Staff\StaffAttendanceController::class, 'store'])->name('store');
                Route::get('/report', [\App\Http\Controllers\SchoolAdmin\Staff\StaffAttendanceController::class, 'report'])->name('report');
            });

            // Leave Requests
            Route::prefix('leaves')->name('leaves.')->group(function () {
                Route::get('/',                    [\App\Http\Controllers\SchoolAdmin\Staff\LeaveController::class, 'index'])->name('index');
                Route::get('/{leave}',             [\App\Http\Controllers\SchoolAdmin\Staff\LeaveController::class, 'show'])->name('show');
                Route::post('/{leave}/approve',    [\App\Http\Controllers\SchoolAdmin\Staff\LeaveController::class, 'approve'])->name('approve');
                Route::post('/{leave}/reject',     [\App\Http\Controllers\SchoolAdmin\Staff\LeaveController::class, 'reject'])->name('reject');
            });

            // Workload & Timetable Allocation
            Route::get('/workload',          [\App\Http\Controllers\SchoolAdmin\Staff\WorkloadController::class, 'index'])->name('workload.index');
            Route::get('/workload/{staff}',  [\App\Http\Controllers\SchoolAdmin\Staff\WorkloadController::class, 'show'])->name('workload.show');
            Route::post('/workload/assign',  [\App\Http\Controllers\SchoolAdmin\Staff\WorkloadController::class, 'assign'])->name('workload.assign');
            Route::post('/workload/unassign',[\App\Http\Controllers\SchoolAdmin\Staff\WorkloadController::class, 'unassign'])->name('workload.unassign');
        });

        // Finance Management
        Route::prefix('finance')->name('finance.')->group(function () {
            Route::resource('fee-structures', \App\Http\Controllers\SchoolAdmin\Finance\FeeStructureController::class);
            Route::resource('invoices', \App\Http\Controllers\SchoolAdmin\Finance\InvoiceController::class);
            Route::resource('payments', \App\Http\Controllers\SchoolAdmin\Finance\PaymentController::class);
            Route::resource('expenses', \App\Http\Controllers\SchoolAdmin\Finance\ExpenseController::class);
            Route::get('reports', [\App\Http\Controllers\SchoolAdmin\Finance\ReportController::class, 'index'])->name('reports.index');
        });

        // Reporting & Analytics
        Route::prefix('reports')->name('reports.')->group(function () {
            Route::get('/', [\App\Http\Controllers\SchoolAdmin\ReportController::class, 'index'])->name('index');
            Route::get('/academic', [\App\Http\Controllers\SchoolAdmin\ReportController::class, 'academic'])->name('academic');
            Route::get('/financial', [\App\Http\Controllers\SchoolAdmin\ReportController::class, 'financial'])->name('financial');
            Route::get('/attendance', [\App\Http\Controllers\SchoolAdmin\ReportController::class, 'attendance'])->name('attendance');
            Route::get('/custom', [\App\Http\Controllers\SchoolAdmin\ReportController::class, 'custom'])->name('custom');
            Route::post('/custom/generate', [\App\Http\Controllers\SchoolAdmin\ReportController::class, 'generateCustom'])->name('custom.generate');
        });

        // Communication System
        Route::prefix('communication')->name('communication.')->group(function () {
            Route::get('/', [\App\Http\Controllers\SchoolAdmin\CommunicationController::class, 'index'])->name('index');
            Route::get('/notifications', [\App\Http\Controllers\SchoolAdmin\CommunicationController::class, 'notifications'])->name('notifications');
            Route::post('/send', [\App\Http\Controllers\SchoolAdmin\CommunicationController::class, 'send'])->name('send');
            Route::resource('announcements', \App\Http\Controllers\SchoolAdmin\AnnouncementController::class);
            Route::get('/messages', [\App\Http\Controllers\SchoolAdmin\MessageController::class, 'index'])->name('messages.index');
            Route::get('/messages/{user}', [\App\Http\Controllers\SchoolAdmin\MessageController::class, 'chat'])->name('messages.chat');
            Route::post('/messages', [\App\Http\Controllers\SchoolAdmin\MessageController::class, 'store'])->name('messages.store');
        });
    });

    // Teacher Routes
    Route::prefix('teacher')->middleware('role:teacher')->name('teacher.')->group(function () {
        Route::get('/dashboard', [App\Http\Controllers\Teacher\DashboardController::class, 'index'])
            ->name('dashboard');

        Route::get('/classes', [App\Http\Controllers\Teacher\ClassController::class, 'index'])
            ->name('classes.index');

        Route::get('/grades', [App\Http\Controllers\Teacher\GradeController::class, 'index'])
            ->name('grades.index');

        Route::get('/grades/{class}/{subject}/enter', [App\Http\Controllers\Teacher\GradeEntryController::class, 'create'])
            ->name('grades.create');

        Route::post('/grades/{class}/{subject}', [App\Http\Controllers\Teacher\GradeEntryController::class, 'store'])
            ->name('grades.store');
            
        Route::get('/grades/{class}/{subject}/bulk-upload', [App\Http\Controllers\Teacher\GradeEntryController::class, 'showBulkUploadForm'])->name('grades.bulk-upload');
        Route::post('/grades/{class}/{subject}/process-bulk-upload', [App\Http\Controllers\Teacher\GradeEntryController::class, 'processBulkUpload'])->name('grades.process-bulk-upload');
        Route::get('/grades/{class}/{subject}/download-template', [App\Http\Controllers\Teacher\GradeEntryController::class, 'downloadTemplate'])->name('grades.download-template');
        
        Route::get('/grades/{class}/{subject}/audit-log', [App\Http\Controllers\Teacher\GradeEntryController::class, 'auditLog'])->name('grades.audit-log');
        
        Route::resource('curriculum', \App\Http\Controllers\Teacher\CurriculumController::class);
        Route::resource('assignments', \App\Http\Controllers\Teacher\AssignmentController::class);
        Route::post('assignments/{assignment}/submissions/{submission}/grade', [\App\Http\Controllers\Teacher\AssignmentController::class, 'gradeSubmission'])
            ->name('assignments.submissions.grade');

        Route::resource('timetable', \App\Http\Controllers\Teacher\TimetableController::class);
        Route::get('/report-cards', [\App\Http\Controllers\Teacher\ReportCardController::class, 'index'])->name('report-cards.index');
        Route::get('/analytics', [\App\Http\Controllers\Teacher\AnalyticsController::class, 'index'])->name('analytics.index');
        Route::get('/my-leaves',  [\App\Http\Controllers\Teacher\LeaveRequestController::class, 'index'])->name('leaves.index');
        Route::post('/my-leaves', [\App\Http\Controllers\Teacher\LeaveRequestController::class, 'store'])->name('leaves.store');
    });

    // Parent Routes
    Route::prefix('parent')->middleware('role:parent')->name('parent.')->group(function () {
        Route::get('/dashboard', [\App\Http\Controllers\Parent\DashboardController::class, 'index'])->name('dashboard');
        Route::get('/messages', [\App\Http\Controllers\Parent\MessageController::class, 'index'])->name('messages.index');
        Route::post('/messages', [\App\Http\Controllers\Parent\MessageController::class, 'store'])->name('messages.store');
        Route::get('/finance', [\App\Http\Controllers\Parent\FinanceController::class, 'index'])->name('finance.index');
    });

    // Student Routes
    Route::prefix('student')->middleware('role:student')->name('student.')->group(function () {
        Route::get('/dashboard', function () {
            return view('student.dashboard');
        })->name('dashboard');
    });

    // Default dashboard redirect (only accessible after login)
    Route::get('/dashboard', function () {
        $user = Auth::user();
        
        if ($user->isSuperAdmin()) {
            return redirect()->route('superadmin.dashboard');
        } elseif ($user->isSchoolAdmin()) {
            return redirect()->route('schooladmin.dashboard');
        } elseif ($user->isTeacher()) {
            return redirect()->route('teacher.dashboard');
        } elseif ($user->isParent()) {
            return redirect()->route('parent.dashboard');
        } elseif ($user->isStudent()) {
            return redirect()->route('student.dashboard');
        }
        
        abort(403, 'No dashboard assigned to your role.');
    })->name('dashboard');
});