<?php

use App\Http\Controllers\Accounting\JournalController;
use App\Http\Controllers\Admin\AuditLogController;
use App\Http\Controllers\Admin\MasterDataController;
use App\Http\Controllers\Admin\PartyController;
use App\Http\Controllers\Admin\PeriodController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Banking\BankReconciliationController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\Finance\AdjustmentNoteController;
use App\Http\Controllers\Finance\ContraSettlementController;
use App\Http\Controllers\Finance\CustomerInvoiceController;
use App\Http\Controllers\Finance\ExpenseController;
use App\Http\Controllers\Finance\DriverAdvanceController;
use App\Http\Controllers\Finance\PaymentController;
use App\Http\Controllers\Finance\QuotationController;
use App\Http\Controllers\Finance\ReceiptController;
use App\Http\Controllers\Finance\VendorBillController;
use App\Http\Controllers\Install\InstallerController;
use App\Http\Controllers\Operations\TransportJobController;
use App\Http\Controllers\Operations\TripController;
use App\Http\Controllers\Reports\ReportController;
use Illuminate\Support\Facades\Route;

Route::get('/install', [InstallerController::class, 'index'])->name('install.index');
Route::post('/install', [InstallerController::class, 'store'])->name('install.store')->middleware('throttle:5,1');

Route::middleware('installed')->group(function (): void {
    Route::middleware('guest')->group(function (): void {
        Route::get('/login', [LoginController::class, 'create'])->name('login');
        Route::post('/login', [LoginController::class, 'store'])->name('login.store')->middleware('throttle:10,1');
    });

    Route::middleware('auth')->group(function (): void {
        Route::get('/', fn () => redirect()->route('dashboard'))->name('home');
        Route::post('/logout', [LoginController::class, 'destroy'])->name('logout');
        Route::get('/dashboard', DashboardController::class)->name('dashboard');

        Route::prefix('masters/{module}')->where(['module' => '[a-z0-9-]+'])->name('masters.')->controller(MasterDataController::class)->group(function (): void {
            Route::get('/', 'index')->name('index');
            Route::get('/create', 'create')->name('create');
            Route::post('/', 'store')->name('store');
            Route::get('/{id}', 'show')->whereNumber('id')->name('show');
            Route::get('/{id}/edit', 'edit')->whereNumber('id')->name('edit');
            Route::put('/{id}', 'update')->whereNumber('id')->name('update');
            Route::delete('/{id}', 'destroy')->whereNumber('id')->name('destroy');
        });

        Route::resource('parties', PartyController::class);

        Route::prefix('jobs')->name('jobs.')->controller(TransportJobController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/rate/suggest', 'suggestRate')->name('rate.suggest');
            Route::get('/{job}', 'show')->name('show'); Route::get('/{job}/edit', 'edit')->name('edit'); Route::put('/{job}', 'update')->name('update'); Route::delete('/{job}', 'destroy')->name('destroy');
            Route::post('/{job}/status', 'status')->name('status');
            Route::post('/{job}/charges', 'storeCharge')->name('charges.store'); Route::delete('/{job}/charges/{charge}', 'destroyCharge')->name('charges.destroy');
            Route::post('/{job}/costs', 'storeCost')->name('costs.store'); Route::delete('/{job}/costs/{cost}', 'destroyCost')->name('costs.destroy');
            Route::post('/{job}/pods', 'storePod')->name('pods.store'); Route::get('/{job}/pods/{pod}/download', 'downloadPod')->name('pods.download'); Route::delete('/{job}/pods/{pod}', 'destroyPod')->name('pods.destroy');
        });

        Route::prefix('trips')->name('trips.')->controller(TripController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/{trip}', 'show')->name('show'); Route::get('/{trip}/edit', 'edit')->name('edit'); Route::put('/{trip}', 'update')->name('update'); Route::delete('/{trip}', 'destroy')->name('destroy'); Route::post('/{trip}/status', 'status')->name('status');
        });

        Route::prefix('quotations')->name('quotations.')->controller(QuotationController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/{quotation}', 'show')->name('show'); Route::get('/{quotation}/edit', 'edit')->name('edit'); Route::put('/{quotation}', 'update')->name('update'); Route::delete('/{quotation}', 'destroy')->name('destroy');
            Route::post('/{quotation}/submit', 'submit')->name('submit'); Route::post('/{quotation}/approve', 'approve')->name('approve'); Route::post('/{quotation}/reject', 'reject')->name('reject'); Route::post('/{quotation}/send', 'send')->name('send'); Route::post('/{quotation}/accept', 'accept')->name('accept'); Route::post('/{quotation}/convert', 'convert')->name('convert');
            Route::get('/{quotation}/print', 'print')->name('print'); Route::get('/{quotation}/pdf', 'pdf')->name('pdf');
        });

        Route::prefix('invoices')->name('invoices.')->controller(CustomerInvoiceController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/{invoice}', 'show')->name('show'); Route::get('/{invoice}/edit', 'edit')->name('edit'); Route::put('/{invoice}', 'update')->name('update'); Route::delete('/{invoice}', 'destroy')->name('destroy');
            Route::post('/{invoice}/submit', 'submit')->name('submit'); Route::post('/{invoice}/approve', 'approve')->name('approve'); Route::post('/{invoice}/reject', 'reject')->name('reject'); Route::post('/{invoice}/post', 'post')->name('post');
            Route::get('/{invoice}/print', 'print')->name('print'); Route::get('/{invoice}/pdf', 'pdf')->name('pdf');
        });

        Route::prefix('bills')->name('bills.')->controller(VendorBillController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/{bill}', 'show')->name('show'); Route::get('/{bill}/edit', 'edit')->name('edit'); Route::put('/{bill}', 'update')->name('update'); Route::delete('/{bill}', 'destroy')->name('destroy');
            Route::post('/{bill}/submit', 'submit')->name('submit'); Route::post('/{bill}/approve', 'approve')->name('approve'); Route::post('/{bill}/reject', 'reject')->name('reject'); Route::post('/{bill}/post', 'post')->name('post');
            Route::get('/{bill}/print', 'print')->name('print'); Route::get('/{bill}/pdf', 'pdf')->name('pdf');
        });

        Route::prefix('receipts')->name('receipts.')->controller(ReceiptController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/{receipt}', 'show')->name('show'); Route::get('/{receipt}/edit', 'edit')->name('edit'); Route::put('/{receipt}', 'update')->name('update'); Route::delete('/{receipt}', 'destroy')->name('destroy');
            Route::post('/{receipt}/submit', 'submit')->name('submit'); Route::post('/{receipt}/approve', 'approve')->name('approve'); Route::post('/{receipt}/reject', 'reject')->name('reject'); Route::post('/{receipt}/post', 'post')->name('post');
            Route::get('/{receipt}/print', 'print')->name('print'); Route::get('/{receipt}/pdf', 'pdf')->name('pdf');
        });

        Route::prefix('payments')->name('payments.')->controller(PaymentController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/{payment}', 'show')->name('show'); Route::get('/{payment}/edit', 'edit')->name('edit'); Route::put('/{payment}', 'update')->name('update'); Route::delete('/{payment}', 'destroy')->name('destroy');
            Route::post('/{payment}/submit', 'submit')->name('submit'); Route::post('/{payment}/approve', 'approve')->name('approve'); Route::post('/{payment}/reject', 'reject')->name('reject'); Route::post('/{payment}/post', 'post')->name('post');
            Route::get('/{payment}/print', 'print')->name('print'); Route::get('/{payment}/pdf', 'pdf')->name('pdf');
        });


        Route::prefix('driver-advances')->name('driver-advances.')->controller(DriverAdvanceController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/{advance}', 'show')->name('show'); Route::get('/{advance}/edit', 'edit')->name('edit'); Route::put('/{advance}', 'update')->name('update'); Route::delete('/{advance}', 'destroy')->name('destroy');
            Route::post('/{advance}/submit', 'submit')->name('submit'); Route::post('/{advance}/approve', 'approve')->name('approve'); Route::post('/{advance}/reject', 'reject')->name('reject'); Route::post('/{advance}/post', 'post')->name('post');
            Route::get('/{advance}/settlements/create', 'settlementCreate')->name('settlements.create'); Route::post('/{advance}/settlements', 'settlementStore')->name('settlements.store');
            Route::get('/{advance}/settlements/{settlement}', 'settlementShow')->name('settlements.show'); Route::get('/{advance}/settlements/{settlement}/edit', 'settlementEdit')->name('settlements.edit'); Route::put('/{advance}/settlements/{settlement}', 'settlementUpdate')->name('settlements.update'); Route::delete('/{advance}/settlements/{settlement}', 'settlementDestroy')->name('settlements.destroy');
            Route::post('/{advance}/settlements/{settlement}/submit', 'settlementSubmit')->name('settlements.submit'); Route::post('/{advance}/settlements/{settlement}/approve', 'settlementApprove')->name('settlements.approve'); Route::post('/{advance}/settlements/{settlement}/reject', 'settlementReject')->name('settlements.reject'); Route::post('/{advance}/settlements/{settlement}/post', 'settlementPost')->name('settlements.post');
        });

        Route::prefix('expenses')->name('expenses.')->controller(ExpenseController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/{expense}', 'show')->name('show'); Route::get('/{expense}/edit', 'edit')->name('edit'); Route::put('/{expense}', 'update')->name('update'); Route::delete('/{expense}', 'destroy')->name('destroy');
            Route::post('/{expense}/submit', 'submit')->name('submit'); Route::post('/{expense}/approve', 'approve')->name('approve'); Route::post('/{expense}/reject', 'reject')->name('reject'); Route::post('/{expense}/post', 'post')->name('post');
            Route::get('/{expense}/print', 'print')->name('print'); Route::get('/{expense}/pdf', 'pdf')->name('pdf');
        });

        Route::prefix('notes')->name('notes.')->controller(AdjustmentNoteController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/{note}', 'show')->name('show'); Route::get('/{note}/edit', 'edit')->name('edit'); Route::put('/{note}', 'update')->name('update'); Route::delete('/{note}', 'destroy')->name('destroy');
            Route::post('/{note}/submit', 'submit')->name('submit'); Route::post('/{note}/approve', 'approve')->name('approve'); Route::post('/{note}/reject', 'reject')->name('reject'); Route::post('/{note}/post', 'post')->name('post');
            Route::get('/{note}/print', 'print')->name('print'); Route::get('/{note}/pdf', 'pdf')->name('pdf');
        });

        Route::prefix('contra')->name('contra.')->controller(ContraSettlementController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store');
            Route::get('/{contra}', 'show')->name('show'); Route::get('/{contra}/edit', 'edit')->name('edit'); Route::put('/{contra}', 'update')->name('update'); Route::delete('/{contra}', 'destroy')->name('destroy');
            Route::post('/{contra}/submit', 'submit')->name('submit'); Route::post('/{contra}/approve', 'approve')->name('approve'); Route::post('/{contra}/reject', 'reject')->name('reject'); Route::post('/{contra}/post', 'post')->name('post');
        });

        Route::prefix('journals')->name('journals.')->controller(JournalController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/create', 'create')->name('create'); Route::post('/', 'store')->name('store'); Route::get('/posted/{journal}', 'show')->name('show'); Route::post('/posted/{journal}/reverse', 'reverse')->name('reverse');
            Route::get('/vouchers/{voucher}', 'voucherShow')->name('vouchers.show'); Route::get('/vouchers/{voucher}/edit', 'edit')->name('vouchers.edit'); Route::put('/vouchers/{voucher}', 'update')->name('vouchers.update'); Route::delete('/vouchers/{voucher}', 'destroy')->name('vouchers.destroy');
            Route::post('/vouchers/{v}/submit', 'submit')->name('vouchers.submit'); Route::post('/vouchers/{v}/approve', 'approve')->name('vouchers.approve'); Route::post('/vouchers/{v}/reject', 'reject')->name('vouchers.reject'); Route::post('/vouchers/{v}/post', 'post')->name('vouchers.post');
        });

        Route::prefix('bank')->name('bank.')->controller(BankReconciliationController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/accounts/{bank}', 'show')->name('show');
            Route::post('/accounts/{bank}/transactions', 'storeTransaction')->name('transactions.store'); Route::delete('/accounts/{bank}/transactions/{transaction}', 'destroyTransaction')->name('transactions.destroy'); Route::post('/accounts/{bank}/import', 'import')->name('import');
            Route::get('/accounts/{bank}/reconcile', 'createReconciliation')->name('reconcile.create'); Route::post('/accounts/{bank}/reconcile', 'storeReconciliation')->name('reconcile.store');
            Route::get('/reconciliations/{reconciliation}', 'reconciliationShow')->name('reconciliations.show'); Route::post('/reconciliations/{reconciliation}/close', 'close')->name('reconciliations.close');
        });

        Route::prefix('reports')->name('reports.')->controller(ReportController::class)->group(function (): void {
            Route::get('/', 'index')->name('index'); Route::get('/trial-balance', 'trialBalance')->name('trial-balance'); Route::get('/profit-loss', 'profitLoss')->name('profit-loss'); Route::get('/balance-sheet', 'balanceSheet')->name('balance-sheet'); Route::get('/general-ledger', 'generalLedger')->name('general-ledger'); Route::get('/aging/{side}', 'aging')->name('aging'); Route::get('/vat', 'vat')->name('vat'); Route::get('/job-profitability', 'jobProfitability')->name('job-profitability'); Route::get('/export/{report}', 'export')->name('export');
        });

        Route::prefix('admin')->group(function (): void {
            Route::resource('users', UserController::class)->except('show');
            Route::get('/roles/create', [UserController::class, 'createRole'])->name('roles.create'); Route::post('/roles', [UserController::class, 'storeRole'])->name('roles.store'); Route::get('/roles/{role}/edit', [UserController::class, 'editRole'])->name('roles.edit'); Route::put('/roles/{role}', [UserController::class, 'updateRole'])->name('roles.update'); Route::delete('/roles/{role}', [UserController::class, 'destroyRole'])->name('roles.destroy');
            Route::get('/settings', [SettingsController::class, 'edit'])->name('settings.edit'); Route::put('/settings', [SettingsController::class, 'update'])->name('settings.update'); Route::put('/settings/einvoice', [SettingsController::class, 'updateEInvoice'])->name('settings.einvoice');
            Route::get('/periods', [PeriodController::class, 'index'])->name('periods.index'); Route::post('/periods/{period}/lock', [PeriodController::class, 'lock'])->name('periods.lock'); Route::post('/periods/{period}/reopen', [PeriodController::class, 'reopen'])->name('periods.reopen');
            Route::get('/audit', [AuditLogController::class, 'index'])->name('audit.index'); Route::get('/audit/export', [AuditLogController::class, 'export'])->name('audit.export'); Route::get('/audit/{audit}', [AuditLogController::class, 'show'])->name('audit.show');
        });
    });
});
