<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\TripController;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return redirect()->route('login');
});

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    // Admin Only
    Route::middleware(['role:Admin'])->group(function () {
        Route::resource('vehicles', \App\Http\Controllers\VehicleController::class);
        Route::resource('drivers', \App\Http\Controllers\DriverController::class);
        Route::resource('maintenances', \App\Http\Controllers\MaintenanceController::class);
        Route::get('admin/login-history', [\App\Http\Controllers\LoginHistoryController::class, 'index'])->name('login-history.index');
        Route::get('reports', [\App\Http\Controllers\ReportController::class, 'index'])->name('reports.index');

        // Backups
        Route::get('backups', [\App\Http\Controllers\BackupController::class, 'index'])->name('backups.index');
        Route::post('backups', [\App\Http\Controllers\BackupController::class, 'create'])->name('backups.create');
        Route::get('backups/download/{filename}', [\App\Http\Controllers\BackupController::class, 'download'])->name('backups.download');
        Route::delete('backups/{filename}', [\App\Http\Controllers\BackupController::class, 'destroy'])->name('backups.destroy');

        // Settings
        Route::get('settings', [\App\Http\Controllers\SettingsController::class, 'index'])->name('settings.index');
        Route::post('settings', [\App\Http\Controllers\SettingsController::class, 'update'])->name('settings.update');

        // Activity Logs
        Route::get('activity-logs', [\App\Http\Controllers\ActivityLogController::class, 'index'])->name('activity-logs.index');

        // Ledgers
        Route::get('ledgers/vehicle', [\App\Http\Controllers\LedgerController::class, 'vehicle'])->name('ledgers.vehicle');
        Route::get('ledgers/driver', [\App\Http\Controllers\LedgerController::class, 'driver'])->name('ledgers.driver');

        // Driver User Account Management
        Route::get('admin/driver-users', [\App\Http\Controllers\DriverUserController::class, 'index'])->name('admin.driver-users.index');
        Route::post('admin/driver-users', [\App\Http\Controllers\DriverUserController::class, 'store'])->name('admin.driver-users.store');
        Route::put('admin/driver-users/{user}', [\App\Http\Controllers\DriverUserController::class, 'update'])->name('admin.driver-users.update');
        Route::delete('admin/driver-users/{user}', [\App\Http\Controllers\DriverUserController::class, 'destroy'])->name('admin.driver-users.destroy');
    });

    // Everyone (Admin, Staff, Driver) can create/edit trips
    Route::middleware(['role:Admin|Staff|Driver'])->group(function () {
        Route::get('trips/create', [TripController::class, 'create'])->name('trips.create');
        Route::post('trips', [TripController::class, 'store'])->name('trips.store');
        Route::get('trips/{trip}/edit', [TripController::class, 'edit'])->name('trips.edit');
        Route::put('trips/{trip}', [TripController::class, 'update'])->name('trips.update');
    });

    // Driver can view their own trips
    Route::middleware(['role:Driver'])->group(function () {
        Route::get('my-trips', [TripController::class, 'myTrips'])->name('trips.my');
    });

    // Admin & Staff
    Route::middleware(['role:Admin|Staff'])->group(function () {
        Route::resource('trips', TripController::class)->except(['create', 'store', 'edit', 'update', 'show']);
        Route::resource('fuel', \App\Http\Controllers\FuelEntryController::class)->except(['show']);
        Route::resource('payments', \App\Http\Controllers\TripPaymentController::class)->except(['show']);
    });
});

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';
