<?php

use Illuminate\Support\Facades\Route;
use Illuminate\Support\Facades\Auth;

// Student Controllers
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\StudentDashboardController;
use App\Http\Controllers\StudentRegistrationController;
use App\Http\Controllers\StudentResultsController;
use App\Http\Controllers\StudentCourseworkController;
use App\Http\Controllers\StudentStatementController;
use App\Http\Controllers\StudentTimetableController;
use App\Http\Controllers\StudentProfileController;
use App\Http\Controllers\StudentDefermentController;
use App\Http\Controllers\StudentAccommodationController;

// Admin Controllers
use App\Http\Controllers\AdminDashboardController;
use App\Http\Controllers\AdminCourseController;
use App\Http\Controllers\AdminRegistrationController;
use App\Http\Controllers\AdminProgrammeController;
use App\Http\Controllers\AdminResultController;
use App\Http\Controllers\AdminDefermentController;
use App\Http\Controllers\AdminAccommodationController;
use App\Http\Controllers\AdminNoticeController;
use App\Http\Controllers\AdminDownloadController;
use App\Http\Controllers\AdminCourseworkController;
use App\Http\Controllers\AdminExamController;
use App\Http\Controllers\AdminFeeController;

// ✅ Home Page
Route::get('/', fn () => view('welcome'))->name('home');

// ✅ Main Dashboard for authenticated users
Route::get('/dashboard', [StudentDashboardController::class, 'index'])
    ->middleware(['auth', 'verified'])->name('dashboard');

// ✅ Profile Routes (shared)
Route::middleware(['auth'])->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});

// ✅ Role-Based Dashboards
Route::middleware(['auth'])->group(function () {
    Route::get('/superadmin/dashboard', fn () => view('dashboards.superadmin'))->name('superadmin.dashboard');
    Route::get('/admin/dashboard', [AdminDashboardController::class, 'index'])->name('admin.dashboard');
    Route::get('/lecturer/dashboard', fn () => view('dashboards.lecturer'))->name('lecturer.dashboard');
    Route::get('/admissions/dashboard', fn () => view('dashboards.admissions'))->name('admissions.dashboard');
    Route::get('/bursar/dashboard', fn () => view('dashboards.bursar'))->name('bursar.dashboard');
    Route::get('/library/dashboard', fn () => view('dashboards.library'))->name('library.dashboard');
    Route::get('/registrar/dashboard', fn () => view('dashboards.registrar'))->name('registrar.dashboard');
});

// ✅ Admin Panel Routes
Route::prefix('admin')->middleware(['auth'])->name('admin.')->group(function () {
    Route::resource('courses', AdminCourseController::class);
    Route::resource('registrations', AdminRegistrationController::class);
    Route::resource('programmes', AdminProgrammeController::class);
    Route::resource('results', AdminResultController::class);
    Route::resource('deferments', AdminDefermentController::class);
    Route::resource('accommodation', AdminAccommodationController::class);
    Route::resource('notices', AdminNoticeController::class);
    Route::resource('downloads', AdminDownloadController::class);

    // ✅ Additional admin routes for student-equivalent features
    Route::resource('coursework', AdminCourseworkController::class);
    Route::resource('exams', AdminExamController::class);
    Route::resource('fees', AdminFeeController::class);
});

// ✅ Student Panel Routes
Route::prefix('student')->middleware(['auth'])->name('student.')->group(function () {

    Route::get('/dashboard', [StudentDashboardController::class, 'index'])->name('dashboard');

    // Registration
    Route::get('/registration', [StudentRegistrationController::class, 'create'])->name('registration');
    Route::post('/registration', [StudentRegistrationController::class, 'store'])->name('registration.store');

    Route::get('/courses', [StudentRegistrationController::class, 'registeredCourses'])->name('courses');
    Route::delete('/courses/{id}', [StudentRegistrationController::class, 'deregister'])->name('courses.deregister');

    // Results and Coursework
    Route::get('/results', [StudentResultsController::class, 'index'])->name('results');
    Route::get('/coursework', [StudentCourseworkController::class, 'index'])->name('coursework');

    // Fee Statement
    Route::get('/statement', [StudentStatementController::class, 'index'])->name('statement');
    Route::get('/statement/download', [StudentStatementController::class, 'download'])->name('statement.download');

    // Notices & Downloads
    Route::get('/notices', fn () => view('student.notices'))->name('notices');
    Route::get('/downloads', fn () => view('student.downloads'))->name('downloads');

    // Profile
    Route::get('/profile', [StudentProfileController::class, 'show'])->name('profile');
    Route::put('/profile', [StudentProfileController::class, 'update'])->name('profile.update');
    Route::get('/programmes', [StudentProfileController::class, 'showProgramme'])->name('programmes');

    // Timetable
    Route::get('/timetable', [StudentTimetableController::class, 'index'])->name('timetable');

    // Accommodation
    Route::get('/accommodation', [StudentAccommodationController::class, 'index'])->name('accommodation');
    Route::post('/accommodation', [StudentAccommodationController::class, 'store'])->name('accommodation.store');

    // Deferment
    Route::get('/deferment', [StudentDefermentController::class, 'index'])->name('deferment');
    Route::post('/deferment', [StudentDefermentController::class, 'store'])->name('deferment.store');
});

// ✅ Auth scaffolding
require __DIR__.'/auth.php';
