<?php

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

// Public / Welcome (optional for now)
Route::get('/', function () {
    return view('welcome');
});

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

// Protected Dashboard Routes with Role Middleware
Route::middleware('auth')->group(function () {

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

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

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

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

    // 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 based on role (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);
    })->name('dashboard');
});