<?php

use App\Http\Controllers\AccountingController;
use App\Http\Controllers\AccountingExportController;
use App\Http\Controllers\AccountingPageController;
use App\Http\Controllers\AccountingSummaryController;
use App\Http\Controllers\ActivityLogController;
use App\Http\Controllers\Auth\TwoFactorController;
use App\Http\Controllers\CompanyController;
use App\Http\Controllers\CompanyFileController;
use App\Http\Controllers\DispatchLookupController;
use App\Http\Controllers\DispatchPageController;
use App\Http\Controllers\DriverController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\LoadController;
use App\Http\Controllers\LoadFileController;
use App\Http\Controllers\PermissionController;
use App\Http\Controllers\ReportController;
use App\Http\Controllers\RoleController;
use App\Http\Controllers\SettingsController;
use App\Http\Controllers\TrailerController;
use App\Http\Controllers\TruckController;
use App\Http\Controllers\UserController;
use App\Http\Middleware\TwoFactorMiddleware;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return redirect('/login');
});
Auth::routes();

Route::group(['middleware' => ['auth']], function () {
    // 2FA Verification routes (must be accessible before 2FA check)
    Route::get('/2fa/verify', [TwoFactorController::class, 'showVerifyForm'])->name('2fa.verify');
    Route::post('/2fa/verify', [TwoFactorController::class, 'verify'])->name('2fa.verify.submit');
});

Route::group(['middleware' => ['auth', TwoFactorMiddleware::class]], function () {
    // 2FA Setup routes
    Route::get('/2fa/setup', [TwoFactorController::class, 'setup'])->name('2fa.setup');
    Route::post('/2fa/enable', [TwoFactorController::class, 'enable'])->name('2fa.enable');
    Route::post('/2fa/disable', [TwoFactorController::class, 'disable'])->name('2fa.disable');
    Route::post('/2fa/toggle/{user}', [TwoFactorController::class, 'toggleForUser'])->name('2fa.toggle');

    Route::resource('roles', RoleController::class);
    Route::resource('users', UserController::class);
    Route::post('/user/update-theme', [UserController::class, 'updateTheme'])->name('user.update-theme');
    Route::get('/profile', [UserController::class, 'profile'])->name('profile.edit');
    Route::put('/profile', [UserController::class, 'updateProfile'])->name('profile.update');
    Route::put('/profile/password', [UserController::class, 'updatePassword'])->name('profile.password.update');
    Route::resource('permissions', PermissionController::class);
    Route::get('/home', [HomeController::class, 'index'])->name('home');
    Route::get('/activity-logs', [ActivityLogController::class, 'index'])->name('activity-logs.index');
    Route::get('/settings', [SettingsController::class, 'index'])->name('settings.index');
    Route::put('/settings', [SettingsController::class, 'update'])->name('settings.update');
    Route::get('/dispatch-board', [HomeController::class, 'dispatchBoard'])->name('dispatch.board');

    Route::prefix('accounting')->group(function () {
        Route::get('/', [AccountingPageController::class, 'index'])->name('accounting.index');

        // Accounting Manager record actions
        Route::post('/loads/batch-invoice', [AccountingController::class, 'batchInvoice'])->name('accounting.loads.batch-invoice');
        Route::post('/loads/{load}/complete', [AccountingController::class, 'complete'])->name('accounting.loads.complete');
        Route::post('/loads/{load}/mark-paid', [AccountingController::class, 'markPaid'])->name('accounting.loads.mark-paid');
        Route::post('/loads/{load}/mark-unpaid', [AccountingController::class, 'markUnpaid'])->name('accounting.loads.mark-unpaid');
        Route::post('/loads/{load}/mark-exported', [AccountingController::class, 'markExported'])->name('accounting.loads.mark-exported');
        Route::patch('/loads/{load}/currency', [AccountingController::class, 'updateCurrency'])->name('accounting.loads.update-currency');
        Route::post('/loads/{load}/reset-invoice', [AccountingController::class, 'resetInvoice'])->name('accounting.loads.reset-invoice');
        Route::get('/loads/{load}/invoice/download', [AccountingController::class, 'downloadInvoice'])->name('accounting.loads.invoice-download');
        Route::get('/loads/{load}/invoice/preview', [AccountingController::class, 'previewInvoice'])->name('accounting.loads.invoice-preview');
        Route::get('/loads/{load}/notes', [AccountingController::class, 'loadNotes'])->name('accounting.loads.notes');
        Route::post('/loads/{load}/notes', [AccountingController::class, 'saveNotes'])->name('accounting.loads.save-notes');

        Route::get('/invoice-pdf', function () {
            return view('accounting.invoice-pdf');
        })->name('accounting.invoice-pdf');
        Route::get('/carrier-pay-manager', [AccountingPageController::class, 'carrierPayManager'])->name('accounting.carrier-pay-manager');
        Route::get('/carrier-pay-manager/excel', [AccountingPageController::class, 'carrierPayManagerExcel'])->name('accounting.carrier-pay-manager.excel');
        Route::post('/carrier-pay/{load}/receive-bill', [AccountingController::class, 'receiveCarrierBill'])->name('accounting.carrier-pay.receive-bill');
        Route::post('/carrier-pay/{load}/unreceive-bill', [AccountingController::class, 'unreceiveCarrierBill'])->name('accounting.carrier-pay.unreceive-bill');
        Route::post('/carrier-pay/generate-batch', [AccountingController::class, 'generateCarrierBatch'])->name('accounting.carrier-pay.generate-batch');
        Route::get('/carrier-pay-export', [AccountingPageController::class, 'carrierPayExport'])->name('accounting.carrier-pay-export');
        Route::get('/summary', [AccountingSummaryController::class, 'index'])->name('accounting.summary');
        Route::post('/summary/generate', [AccountingSummaryController::class, 'generate'])->name('accounting.summary.generate');
        Route::get('/summary/preview', [AccountingSummaryController::class, 'preview'])->name('accounting.summary.preview');
        Route::get('/summary/preview/pdf', [AccountingSummaryController::class, 'previewPdf'])->name('accounting.summary.preview-pdf');
        Route::get('/summary/{summary}', [AccountingSummaryController::class, 'show'])->name('accounting.summary.show');
        Route::get('/summary/{summary}/pdf', [AccountingSummaryController::class, 'pdf'])->name('accounting.summary.pdf');

        Route::get('/export', [AccountingExportController::class, 'index'])->name('accounting.export');
        Route::post('/export/generate', [AccountingExportController::class, 'generate'])->name('accounting.export.generate');
        Route::post('/export/settings', [AccountingExportController::class, 'saveSettings'])->name('accounting.export.settings');
        Route::get('/export/preview', [AccountingExportController::class, 'preview'])->name('accounting.export.preview');
        Route::get('/export/preview/download', [AccountingExportController::class, 'previewDownload'])->name('accounting.export.preview-download');
        Route::get('/export/{export}/download', [AccountingExportController::class, 'download'])->name('accounting.export.download');
        Route::get('/sales-rep-commission', [AccountingPageController::class, 'salesRepCommission'])->name('accounting.sales-rep-commission');
        Route::get('/batch-invoicing', function () {
            return view('accounting.batch-invoicing');
        })->name('accounting.batch-invoicing');
    });

    Route::prefix('reports')->group(function () {
        Route::get('/', [ReportController::class, 'index'])->name('reports.index');
        Route::get('/download', [ReportController::class, 'download'])->name('reports.download');
        Route::get('/statistics', [ReportController::class, 'statistics'])->name('reports.statistics');
        Route::get('/tax', [ReportController::class, 'taxIndex'])->name('reports.tax');
        Route::get('/tax/generate', [ReportController::class, 'taxGenerate'])->name('reports.tax.generate');
    });

    Route::prefix('dispatch')->group(function () {
        Route::get('lookups', [DispatchLookupController::class, 'dropdowns'])->name('dispatch.lookups');
        Route::get('lookups/expiring-carrier-insurance', [DispatchLookupController::class, 'expiringCarrierInsurance'])->name('dispatch.lookups.expiring-carrier-insurance');
        Route::get('lookups/factoring-credit-alerts', [DispatchLookupController::class, 'factoringCreditAlerts'])->name('dispatch.lookups.factoring-credit-alerts');
        Route::get('lookups/countries/{country}/states', [DispatchLookupController::class, 'statesByCountry'])->name('dispatch.lookups.states');
        Route::get('lookups/locations', [DispatchLookupController::class, 'searchLocations'])->name('dispatch.lookups.locations');
        Route::post('quick-companies/{role}', [DispatchLookupController::class, 'quickCompany'])->name('dispatch.quick-companies.store');

        Route::resource('companies', CompanyController::class)->except(['create', 'edit']);
        Route::get('companies/{company}/files', [CompanyFileController::class, 'index'])->name('companies.files.index');
        Route::post('companies/{company}/files', [CompanyFileController::class, 'store'])->name('companies.files.store');
        Route::get('companies/{company}/files/{file}/download', [CompanyFileController::class, 'download'])->name('companies.files.download');
        Route::delete('companies/{company}/files/{file}', [CompanyFileController::class, 'destroy'])->name('companies.files.destroy');
        Route::patch('loads/{load}/status', [LoadController::class, 'updateStatus'])->name('loads.update-status');
        Route::get('loads/{load}/confirmation', [LoadController::class, 'printConfirmation'])->name('loads.confirmation');
        Route::get('loads/{load}/confirmation-preview', [LoadController::class, 'confirmationPreview'])->name('loads.confirmation-preview');
        Route::post('loads/{load}/email', [LoadController::class, 'emailConfirmation'])->name('loads.email');
        Route::get('loads/{load}/files', [LoadFileController::class, 'index'])->name('loads.files.index');
        Route::post('loads/{load}/files', [LoadFileController::class, 'store'])->name('loads.files.store');
        Route::get('loads/{load}/files/{file}/download', [LoadFileController::class, 'download'])->name('loads.files.download');
        Route::delete('loads/{load}/files/{file}', [LoadFileController::class, 'destroy'])->name('loads.files.destroy');
        Route::resource('loads', LoadController::class)->except(['create', 'edit']);
        Route::resource('drivers', DriverController::class)->except(['create', 'edit']);
        Route::resource('trucks', TruckController::class)->except(['create', 'edit']);
        Route::resource('trailers', TrailerController::class)->except(['create', 'edit']);
    });

    Route::prefix('ui')->group(function () {
        Route::get('/forms', function () {
            return view('ui.forms');
        })->name('ui.forms');

        Route::get('/active-load/{load?}', [DispatchPageController::class, 'activeLoad'])->name('ui.active-load');
        Route::get('/loads', [DispatchPageController::class, 'loads'])->name('ui.loads');
        Route::get('/load-finder', [DispatchPageController::class, 'loadFinder'])->name('ui.load-finder');
        Route::get('/customers', [DispatchPageController::class, 'customers'])->name('ui.customers');
        Route::get('/shippers', [DispatchPageController::class, 'shippers'])->name('ui.shippers');
        Route::get('/consignees', [DispatchPageController::class, 'consignees'])->name('ui.consignees');
        Route::get('/external-carriers', [DispatchPageController::class, 'externalCarriers'])->name('ui.external-carriers');
        Route::get('/factoring-companies', [DispatchPageController::class, 'factoringCompanies'])->name('ui.factoring-companies');
        Route::get('/factoring', [DispatchPageController::class, 'factoring'])->name('ui.factoring');
        Route::get('/drivers', [DispatchPageController::class, 'drivers'])->name('ui.drivers');
        Route::get('/trucks', [DispatchPageController::class, 'trucks'])->name('ui.trucks');
        Route::get('/trailers', [DispatchPageController::class, 'trailers'])->name('ui.trailers');
    });
});
