<?php

use App\Http\Controllers\ProfileController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\RateController;
use App\Http\Controllers\BookingController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\FrontendController;
use Illuminate\Support\Facades\Route;

// Public Routes
Route::get('/', [FrontendController::class, 'index'])->name('home');
Route::get('/track-shipment', [FrontendController::class, 'track'])->name('frontend.track');
Route::get('/about-us', [FrontendController::class, 'about'])->name('frontend.about');

Route::middleware(['auth', 'verified'])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    
    // Profile
    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');

    // Customer Management - Manager & Receptionist
    Route::middleware('role:Branch Manager,Receptionist')->group(function () {
        Route::delete('customers/bulk-delete', [CustomerController::class, 'bulkDelete'])->name('customers.bulk-delete');
        Route::resource('customers', CustomerController::class);
    });

    // User Management - Manager only
    Route::middleware('role:Branch Manager')->resource('users', \App\Http\Controllers\UserController::class);

    // Rate Management - Manager only
    Route::middleware('role:Branch Manager')->resource('rates', RateController::class);

    // Booking Management
    // Bulk Status & Receive - Manager & Driver
    Route::middleware('role:Branch Manager,Driver')->group(function () {
        Route::get('bookings/bulk-status', [BookingController::class, 'bulkStatusUpdate'])->name('bookings.bulk-status');
        Route::post('bookings/bulk-status', [BookingController::class, 'processBulkStatusUpdate'])->name('bookings.bulk-status.process');
        Route::get('bookings/receive', [BookingController::class, 'receiveShipment'])->name('bookings.receive');
        Route::post('bookings/{booking}/receive', [BookingController::class, 'processReceiveShipment'])->name('bookings.process-receive');
    });

    // General Bookings
    Route::resource('bookings', BookingController::class)->except(['destroy']);
    
    // Delete Booking - Manager only
    Route::delete('bookings/{booking}', [BookingController::class, 'destroy'])
        ->middleware('role:Branch Manager')
        ->name('bookings.destroy');

    Route::get('bookings/{booking}/pdf', [BookingController::class, 'downloadPdf'])->name('bookings.pdf');
    Route::get('bookings/{booking}/invoice', [BookingController::class, 'printInvoice'])->name('bookings.invoice');
    Route::get('bookings/{booking}/cargo-slip', [BookingController::class, 'printCargoSlip'])->name('bookings.cargo-slip');
    Route::get('bookings/{booking}/delivery-receipt', [BookingController::class, 'printDeliveryReceipt'])->name('bookings.delivery-receipt');
    Route::get('bookings/{booking}/thermal-receipt', [BookingController::class, 'printThermalReceipt'])->name('bookings.thermal-receipt');
    
    // Quick Update Routes
    Route::patch('bookings/{booking}/update-status', [BookingController::class, 'updateStatus'])->name('bookings.update-status');
    Route::patch('bookings/{booking}/update-payment', [BookingController::class, 'updatePayment'])->name('bookings.update-payment');

    // Shipment Tracking
    Route::get('track', [BookingController::class, 'track'])->name('track');

    // Settings - Manager only
    Route::middleware('role:Branch Manager')->group(function () {
        Route::get('settings', [SettingsController::class, 'index'])->name('settings.index');
        Route::post('settings', [SettingsController::class, 'update'])->name('settings.update');
        Route::delete('settings/slider-image', [SettingsController::class, 'deleteSliderImage'])->name('settings.deleteSliderImage');
    });

    // Reports - Manager only
    Route::middleware('role:Branch Manager')->prefix('reports')->name('reports.')->group(function () {
        Route::get('daily-shipments', [ReportController::class, 'dailyShipments'])->name('daily-shipments');
        Route::get('customer-invoices', [ReportController::class, 'customerInvoices'])->name('customer-invoices');
        Route::get('outstanding-dues', [ReportController::class, 'outstandingDues'])->name('outstanding-dues');
        Route::get('cargo-volume', [ReportController::class, 'cargoVolume'])->name('cargo-volume');
    });
});

require __DIR__.'/auth.php';
