<?php

use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\PartnerController;
use App\Http\Controllers\Admin\RequestController as AdminRequestController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Courier\DashboardController as CourierDashboardController;
use App\Http\Controllers\Courier\RequestController as CourierRequestController;
use App\Http\Controllers\Partner\DashboardController as PartnerDashboardController;
use App\Http\Controllers\Partner\RequestController as PartnerRequestController;
use Illuminate\Support\Facades\Route;

// --- Authentification ---
Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
    Route::post('/login', [LoginController::class, 'login']);
});
Route::post('/logout', [LoginController::class, 'logout'])->middleware('auth')->name('logout');

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

Route::middleware('auth')->group(function () {
    Route::get('/dashboard', [LoginController::class, 'dashboard'])->name('dashboard');

    // --- Espace Admin ---
    Route::prefix('admin')->name('admin.')->middleware('role:admin')->group(function () {
        Route::get('/', [AdminDashboardController::class, 'index'])->name('dashboard');

        Route::get('/partners', [PartnerController::class, 'index'])->name('partners.index');
        Route::get('/partners/create', [PartnerController::class, 'create'])->name('partners.create');
        Route::post('/partners', [PartnerController::class, 'store'])->name('partners.store');
        Route::post('/partners/{partner}/toggle-active', [PartnerController::class, 'toggleActive'])->name('partners.toggle');

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

        Route::get('/requests', [AdminRequestController::class, 'index'])->name('requests.index');
        Route::get('/requests/{serviceRequest}', [AdminRequestController::class, 'show'])->name('requests.show');
        Route::post('/requests/{serviceRequest}/assign', [AdminRequestController::class, 'assign'])->name('requests.assign');
    });

    // --- Espace Partenaire (laboratoire / clinique) ---
    Route::prefix('partner')->name('partner.')->middleware('role:partner')->group(function () {
        Route::get('/', [PartnerDashboardController::class, 'index'])->name('dashboard');

        Route::get('/requests/{type}', [PartnerRequestController::class, 'index'])->name('requests.index');
        Route::get('/requests/{type}/create', [PartnerRequestController::class, 'create'])->name('requests.create');
        Route::post('/requests/{type}', [PartnerRequestController::class, 'store'])->name('requests.store');
    });

    // --- Espace Coursier ---
    Route::prefix('courier')->name('courier.')->middleware('role:courier')->group(function () {
        Route::get('/', [CourierDashboardController::class, 'index'])->name('dashboard');

        Route::get('/requests', [CourierRequestController::class, 'index'])->name('requests.index');
        Route::get('/requests/{serviceRequest}', [CourierRequestController::class, 'show'])->name('requests.show');
        Route::post('/requests/{serviceRequest}/collect', [CourierRequestController::class, 'collect'])->name('requests.collect');
        Route::post('/requests/{serviceRequest}/unavailable', [CourierRequestController::class, 'unavailable'])->name('requests.unavailable');
        Route::post('/requests/{serviceRequest}/deliver', [CourierRequestController::class, 'deliver'])->name('requests.deliver');
    });
});
