<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\WebsiteController;
use App\Http\Controllers\Student\DashboardController as StudentDashboard;
use App\Http\Controllers\Teacher\DashboardController as TeacherDashboard;
use App\Http\Controllers\Admin\DashboardController as AdminDashboard;
use App\Http\Controllers\Admin\StudentController as AdminStudent;
use App\Http\Controllers\Admin\TeacherController as AdminTeacher;
use App\Http\Controllers\Admin\AnnouncementController as AdminAnnouncement;
use Illuminate\Support\Facades\Route;

// Public marketing site
Route::get('/',          [WebsiteController::class, 'index'])->name('home');
Route::get('/about',     [WebsiteController::class, 'about'])->name('about');
Route::get('/teachers',  [WebsiteController::class, 'teachers'])->name('teachers');
Route::get('/timetable', [WebsiteController::class, 'timetable'])->name('timetable');
Route::get('/contact',   [WebsiteController::class, 'contact'])->name('contact');

// Role-based dashboard redirect
Route::middleware('auth')->get('/dashboard', function () {
    return match (auth()->user()->role) {
        'admin'   => redirect()->route('admin.dashboard'),
        'teacher' => redirect()->route('teacher.dashboard'),
        default   => redirect()->route('student.dashboard'),
    };
})->name('dashboard');

// Student routes
Route::middleware(['auth', 'role:student'])->prefix('student')->name('student.')->group(function () {
    Route::get('/dashboard', [StudentDashboard::class, 'index'])->name('dashboard');
    Route::get('/lesson/{lesson}', [StudentDashboard::class, 'lesson'])->name('lesson');
});

// Teacher routes
Route::middleware(['auth', 'role:teacher'])->prefix('teacher')->name('teacher.')->group(function () {
    Route::get('/dashboard', [TeacherDashboard::class, 'index'])->name('dashboard');
});

// Admin routes
Route::middleware(['auth', 'role:admin'])->prefix('admin')->name('admin.')->group(function () {
    Route::get('/dashboard', [AdminDashboard::class, 'index'])->name('dashboard');
    Route::resource('students', AdminStudent::class);
    Route::resource('teachers', AdminTeacher::class);
    Route::resource('announcements', AdminAnnouncement::class)->only(['store', 'destroy']);
});

// Profile
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');
});

require __DIR__.'/auth.php';
