<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\SuperAdmin\{AuthController as SAAuthController, DashboardController as SADashController};
use App\Http\Controllers\Auth\{LoginController, SelectCompanyController};
use App\Http\Controllers\Company\SubscriptionController;
use App\Http\Controllers\SuperAdmin\CompanyGroupController;
use App\Http\Controllers\SuperAdmin\BrandingController;
use App\Http\Controllers\BrandAssetController;
use App\Http\Controllers\Company\DashboardController;
use App\Http\Controllers\Company\ProfileController;
use App\Http\Controllers\Company\Settings\CompanySettingsController;
use App\Http\Controllers\Company\Admin\{UsersController, RolesController, FiscalYearsController, VoucherNumberingController, AuditTrailController, QuickBooksImportController, CompanyDataResetController, BudgetsController};
use App\Http\Controllers\Company\Accounting\{ChartOfAccountsController, DimensionsController, VoucherChequeBookController};
use App\Http\Controllers\Company\Masters\{CustomersController, VendorsController, CurrenciesController, TaxCodesController, PriceLevelsController, EmployeesController, OtherNamesController, OpeningBalanceController};
use App\Http\Controllers\Company\Inventory\{ItemsController, UomsController, ItemCategoriesController, WarehousesController};
use App\Http\Controllers\Company\Accounting\AccountingVouchersController;
use App\Http\Controllers\Company\Banking\{BankReconciliationController, ChequeBooksController, ChequeFormatsController, ChequePrintController, IssuedVoidChequesController};
use App\Http\Controllers\Company\Payroll\PayrollModuleController;
use App\Http\Controllers\Company\Payroll\PayrollLookupController;
use App\Http\Controllers\Company\Payroll\PayrollSalaryComponentsController;
use App\Http\Controllers\Company\Payroll\PayrollSheetController;
use App\Http\Controllers\Company\Payroll\PaySlipController;
use App\Http\Controllers\Company\Payroll\BankTransferLetterController;
use App\Http\Controllers\Company\FixedAssets\{FixedAssetsController, FixedAssetCategoriesController, FixedAssetDepreciationController};
use App\Http\Controllers\Company\Reports\{BalanceSheetController, GeneralLedgerController, JournalReportController, ProfitLossController, TrialBalanceController};

Route::get('/brand-assets/app-logo', [BrandAssetController::class, 'appLogo'])->name('brand.app-logo');
Route::get('/brand-assets/company-logo/{company}', [BrandAssetController::class, 'companyLogo'])->name('brand.company-logo');

$saPrefix = env('SUPER_ADMIN_PREFIX', 'sadmin');

Route::prefix($saPrefix)->name('sadmin.')->group(function () {
    Route::get('/login',  [SAAuthController::class, 'showLogin'])->name('login');
    Route::post('/login', [SAAuthController::class, 'login'])->name('login.post');
    Route::middleware('auth:super_admin')->group(function () {
        Route::post('/logout',                      [SAAuthController::class, 'logout'])->name('logout');
        Route::get('/',                             [SADashController::class, 'dashboard'])->name('dashboard');
        Route::get('/dashboard',                    [SADashController::class, 'dashboard']);
        Route::get('/companies',                    [SADashController::class, 'companies'])->name('companies');
        Route::get('/companies/create',             [SADashController::class, 'createCompany'])->name('companies.create');
        Route::post('/companies',                   [SADashController::class, 'storeCompany'])->name('companies.store');
        Route::get('/companies/{company}/edit',     [SADashController::class, 'editCompany'])->name('companies.edit');
        Route::put('/companies/{company}',          [SADashController::class, 'updateCompany'])->name('companies.update');
        Route::patch('/companies/{company}/toggle', [SADashController::class, 'toggleStatus'])->name('companies.toggle');
        Route::post('/company-groups',              [CompanyGroupController::class, 'store'])->name('company-groups.store');
        Route::delete('/company-groups/{group}',    [CompanyGroupController::class, 'destroy'])->name('company-groups.destroy');
        Route::get('/online-users',                 [SADashController::class, 'onlineUsers'])->name('online-users');
        Route::post('/force-logout',                [SADashController::class, 'forceLogout'])->name('force-logout');
        Route::get('/audit-logs',                   [SADashController::class, 'auditLogs'])->name('audit-logs');
        Route::get('/branding',                     [BrandingController::class, 'index'])->name('branding');
        Route::post('/branding',                    [BrandingController::class, 'update'])->name('branding.update');
        Route::put('/branding',                     [BrandingController::class, 'update']);
        Route::delete('/branding',                  [BrandingController::class, 'destroy'])->name('branding.destroy');
    });
});

Route::get('/login',  [LoginController::class, 'showLogin'])->name('login');
Route::post('/login', [LoginController::class, 'login'])->name('login.post');
Route::get('/select-company',         [SelectCompanyController::class, 'show'])->name('select-company');
Route::post('/select-company',        [SelectCompanyController::class, 'select'])->name('select-company.select');
Route::post('/select-company/hide',   [SelectCompanyController::class, 'hide'])->name('select-company.hide');
Route::post('/select-company/unhide', [SelectCompanyController::class, 'unhide'])->name('select-company.unhide');
Route::post('/select-company/skip',   [SelectCompanyController::class, 'skip'])->name('select-company.skip');

Route::middleware(['auth:web','company.active','concurrent.users','password.changed'])->group(function () {

    Route::post('/logout',          [LoginController::class, 'logout'])->name('logout');
    Route::get('/subscription',     [SubscriptionController::class, 'index'])->name('subscription');
    Route::get('/password/change',  [LoginController::class, 'showChangePassword'])->name('password.change');
    Route::post('/password/change', [LoginController::class, 'changePassword'])->name('password.update');
    Route::put('/profile',          [ProfileController::class, 'update'])->name('profile.update');
    Route::get('/',          [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/dashboard', [DashboardController::class, 'index']);

    Route::prefix('settings')->name('settings.')->group(function () {
        Route::get('/general',    [CompanySettingsController::class, 'general'])->name('general');
        Route::put('/general',    [CompanySettingsController::class, 'updateGeneral'])->name('general.update');
        Route::get('/accounting', [CompanySettingsController::class, 'accounting'])->name('accounting');
        Route::put('/accounting', [CompanySettingsController::class, 'updateAccounting'])->name('accounting.update');
        Route::get('/inventory',  [CompanySettingsController::class, 'inventory'])->name('inventory');
        Route::put('/inventory',  [CompanySettingsController::class, 'updateInventory'])->name('inventory.update');
        Route::get('/arap',       [CompanySettingsController::class, 'arap'])->name('arap');
        Route::put('/arap',       [CompanySettingsController::class, 'updateArap'])->name('arap.update');
        Route::get('/payroll',    [CompanySettingsController::class, 'payroll'])->name('payroll');
        Route::put('/payroll/module', [CompanySettingsController::class, 'updatePayrollModule'])->name('payroll.module.update');
        Route::put('/payroll/tax', [CompanySettingsController::class, 'updatePayrollTax'])->name('payroll.tax.update');
        Route::post('/payroll/tax/reset', [CompanySettingsController::class, 'resetPayrollTaxDefaults'])->name('payroll.tax.reset');
        Route::put('/payroll/letter', [CompanySettingsController::class, 'updatePayrollLetter'])->name('payroll.letter.update');
        Route::post('/test-smtp', [CompanySettingsController::class, 'testSmtp'])->name('test-smtp');
        Route::get('/security',   [CompanySettingsController::class, 'security'])->name('security');
        Route::put('/security',   [CompanySettingsController::class, 'updateSecurity'])->name('security.update');
    });

    Route::prefix('admin')->name('admin.')->group(function () {
        Route::prefix('users')->name('users.')->group(function () {
            Route::get('/',                       [UsersController::class, 'index'])->name('index');
            Route::get('/create',                 [UsersController::class, 'create'])->name('create');
            Route::post('/',                      [UsersController::class, 'store'])->name('store');
            Route::get('/{user}/edit',            [UsersController::class, 'edit'])->name('edit');
            Route::put('/{user}',                 [UsersController::class, 'update'])->name('update');
            Route::post('/{user}/reset-password', [UsersController::class, 'resetPassword'])->name('reset-password');
            Route::patch('/{user}/toggle-active', [UsersController::class, 'toggleActive'])->name('toggle-active');
            Route::delete('/{user}',              [UsersController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('roles')->name('roles.')->group(function () {
            Route::get('/',             [RolesController::class, 'index'])->name('index');
            Route::get('/create',       [RolesController::class, 'create'])->name('create');
            Route::post('/',            [RolesController::class, 'store'])->name('store');
            Route::get('/{role}/edit',  [RolesController::class, 'edit'])->name('edit');
            Route::put('/{role}',       [RolesController::class, 'update'])->name('update');
            Route::delete('/{role}',    [RolesController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('budgets')->name('budgets.')->group(function () {
            Route::get('/', [BudgetsController::class, 'index'])->name('index');
            Route::get('/create', [BudgetsController::class, 'create'])->name('create');
            Route::post('/', [BudgetsController::class, 'store'])->name('store');
            Route::get('/{budget}/edit', [BudgetsController::class, 'edit'])->name('edit');
            Route::put('/{budget}', [BudgetsController::class, 'update'])->name('update');
            Route::delete('/{budget}', [BudgetsController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('fiscal-years')->name('fiscal-years.')->group(function () {
            Route::get('/',                           [FiscalYearsController::class, 'index'])->name('index');
            Route::put('/closing',                    [FiscalYearsController::class, 'updateClosing'])->name('update-closing');
            Route::get('/create',                     [FiscalYearsController::class, 'create'])->name('create');
            Route::post('/',                          [FiscalYearsController::class, 'store'])->name('store');
            Route::patch('/{fiscalYear}/set-current', [FiscalYearsController::class, 'setCurrent'])->name('set-current');
            Route::patch('/{fiscalYear}/lock',        [FiscalYearsController::class, 'lock'])->name('lock');
        });
        Route::prefix('voucher-numbering')->name('voucher-numbering.')->group(function () {
            Route::get('/',                   [VoucherNumberingController::class, 'index'])->name('index');
            Route::get('/{voucherType}/edit', [VoucherNumberingController::class, 'edit'])->name('edit');
            Route::put('/{voucherType}',      [VoucherNumberingController::class, 'update'])->name('update');
            Route::post('/preview',           [VoucherNumberingController::class, 'previewAjax'])->name('preview');
        });
        Route::get('/audit-trail', [AuditTrailController::class, 'index'])->name('audit-trail');
        Route::prefix('quickbooks')->name('quickbooks.')->group(function () {
            Route::get('/list-import', [QuickBooksImportController::class, 'listImport'])->name('list-import');
            Route::post('/list-import/{tab}', [QuickBooksImportController::class, 'importList'])->name('list-import.process');
            Route::get('/list-import/{tab}/sample', [QuickBooksImportController::class, 'downloadSample'])->name('list-import.sample');
            Route::get('/data-import', [QuickBooksImportController::class, 'dataImport'])->name('data-import');
            Route::post('/data-import/analyze', [QuickBooksImportController::class, 'analyzeDataImport'])->name('data-import.analyze');
            Route::post('/data-import/import', [QuickBooksImportController::class, 'processDataImport'])->name('data-import.import');
            Route::post('/data-import/cancel', [QuickBooksImportController::class, 'cancelDataImport'])->name('data-import.cancel');
            Route::get('/data-import/{type}/sample', [QuickBooksImportController::class, 'downloadDataSample'])->name('data-import.sample');
        });
        Route::prefix('company-reset')->name('company-reset.')->group(function () {
            Route::get('/transactions', [CompanyDataResetController::class, 'transactions'])->name('transactions');
            Route::delete('/transactions', [CompanyDataResetController::class, 'destroyTransactions'])->name('transactions.destroy');
            Route::get('/listings', [CompanyDataResetController::class, 'listings'])->name('listings');
            Route::delete('/listings', [CompanyDataResetController::class, 'destroyListings'])->name('listings.destroy');
        });
    });

    // Party search (unified: customers, vendors, employees, other names)
    Route::get('/party/search', [\App\Http\Controllers\Company\Accounting\AccountingVouchersController::class, 'partySearch'])->name('party.search');

    Route::prefix('accounting')->name('coa.')->group(function () {
        Route::get('/accounts/search',         [ChartOfAccountsController::class, 'search'])->name('search');
        Route::get('/accounts/print',          [ChartOfAccountsController::class, 'print'])->name('print');
        Route::get('/accounts/export',         [ChartOfAccountsController::class, 'export'])->name('export');
        Route::post('/accounts/import',        [ChartOfAccountsController::class, 'import'])->name('import');
        Route::get('/accounts',                [ChartOfAccountsController::class, 'index'])->name('index');
        Route::get('/accounts/create',         [ChartOfAccountsController::class, 'create'])->name('create');
        Route::post('/accounts',               [ChartOfAccountsController::class, 'store'])->name('store');
        Route::post('/accounts/ajax',          [ChartOfAccountsController::class, 'storeAjax'])->name('store-ajax');
        Route::post('/accounts/quick',         [ChartOfAccountsController::class, 'quickStore'])->name('quick-store');
        Route::get('/accounts/{account}/edit', [ChartOfAccountsController::class, 'edit'])->name('edit');
        Route::put('/accounts/{account}',      [ChartOfAccountsController::class, 'update'])->name('update');
        Route::delete('/accounts/{account}',   [ChartOfAccountsController::class, 'destroy'])->name('destroy');
    });

    Route::prefix('accounting/dimensions')->name('dimensions.')->group(function () {
        Route::get('/',                              [DimensionsController::class, 'index'])->name('index');
        Route::put('/slots',                         [DimensionsController::class, 'updateSlots'])->name('update-slots');
        Route::get('/search/{slot}',                 [DimensionsController::class, 'searchValues'])->name('search');
        Route::get('/{dimension}/values',            [DimensionsController::class, 'values'])->name('values');
        Route::post('/{dimension}/values',           [DimensionsController::class, 'storeValue'])->name('values.store');
        Route::put('/{dimension}/values/{value}',    [DimensionsController::class, 'updateValue'])->name('values.update');
        Route::delete('/{dimension}/values/{value}', [DimensionsController::class, 'destroyValue'])->name('values.destroy');
    });

    Route::prefix('masters')->name('masters.')->group(function () {
        Route::get('/opening-balance', [OpeningBalanceController::class, 'index'])->name('opening-balance.index');
        Route::post('/opening-balance/{tab}', [OpeningBalanceController::class, 'store'])
            ->name('opening-balance.store')
            ->where('tab', 'accounts|items|customers|vendors');
        Route::prefix('customers')->name('customers.')->group(function () {
            Route::get('/search',              [CustomersController::class, 'search'])->name('search');
            Route::get('/',                    [CustomersController::class, 'index'])->name('index');
            Route::get('/create',              [CustomersController::class, 'create'])->name('create');
            Route::post('/',                   [CustomersController::class, 'store'])->name('store');
            Route::get('/{customer}/edit',     [CustomersController::class, 'edit'])->name('edit');
            Route::put('/{customer}',          [CustomersController::class, 'update'])->name('update');
            Route::patch('/{customer}/toggle', [CustomersController::class, 'toggleActive'])->name('toggle');
            Route::delete('/{customer}',       [CustomersController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('vendors')->name('vendors.')->group(function () {
            Route::get('/search',            [VendorsController::class, 'search'])->name('search');
            Route::get('/',                  [VendorsController::class, 'index'])->name('index');
            Route::get('/create',            [VendorsController::class, 'create'])->name('create');
            Route::post('/',                 [VendorsController::class, 'store'])->name('store');
            Route::get('/{vendor}/edit',     [VendorsController::class, 'edit'])->name('edit');
            Route::put('/{vendor}',          [VendorsController::class, 'update'])->name('update');
            Route::patch('/{vendor}/toggle', [VendorsController::class, 'toggleActive'])->name('toggle');
            Route::delete('/{vendor}',       [VendorsController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('currencies')->name('currencies.')->group(function () {
            Route::get('/',                [CurrenciesController::class, 'index'])->name('index');
            Route::post('/',               [CurrenciesController::class, 'store'])->name('store');
            Route::put('/{currency}',      [CurrenciesController::class, 'update'])->name('update');
            Route::delete('/{currency}',   [CurrenciesController::class, 'destroy'])->name('destroy');
            Route::put('/{currency}/rate', [CurrenciesController::class, 'updateRate'])->name('rate');
        });
        Route::prefix('tax-codes')->name('tax-codes.')->group(function () {
            Route::get('/search',       [TaxCodesController::class, 'search'])->name('search');
            Route::get('/',             [TaxCodesController::class, 'index'])->name('index');
            Route::post('/',            [TaxCodesController::class, 'store'])->name('store');
            Route::put('/{taxCode}',    [TaxCodesController::class, 'update'])->name('update');
            Route::delete('/{taxCode}', [TaxCodesController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('price-levels')->name('price-levels.')->group(function () {
            Route::get('/',                [PriceLevelsController::class, 'index'])->name('index');
            Route::post('/',               [PriceLevelsController::class, 'store'])->name('store');
            Route::put('/{priceLevel}',    [PriceLevelsController::class, 'update'])->name('update');
            Route::delete('/{priceLevel}', [PriceLevelsController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('employees')->name('employees.')->group(function () {
            Route::get('/',                    [EmployeesController::class, 'index'])->name('index');
            Route::get('/create',              [EmployeesController::class, 'create'])->name('create');
            Route::post('/',                   [EmployeesController::class, 'store'])->name('store');
            Route::get('/{employee}/edit',     [EmployeesController::class, 'edit'])->name('edit');
            Route::put('/{employee}',          [EmployeesController::class, 'update'])->name('update');
            Route::delete('/{employee}/documents/{document}', [EmployeesController::class, 'destroyDocument'])->name('documents.destroy');
            Route::patch('/{employee}/toggle', [EmployeesController::class, 'toggleActive'])->name('toggle');
            Route::delete('/{employee}',       [EmployeesController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('other-names')->name('other-names.')->group(function () {
            Route::get('/',                [OtherNamesController::class, 'index'])->name('index');
            Route::post('/',               [OtherNamesController::class, 'store'])->name('store');
            Route::put('/{otherName}',     [OtherNamesController::class, 'update'])->name('update');
            Route::delete('/{otherName}',  [OtherNamesController::class, 'destroy'])->name('destroy');
        });
    });

    Route::prefix('inventory')->name('inventory.')->group(function () {
        Route::prefix('items')->name('items.')->group(function () {
            Route::get('/search',          [ItemsController::class, 'search'])->name('search');
            Route::get('/',                [ItemsController::class, 'index'])->name('index');
            Route::get('/create',          [ItemsController::class, 'create'])->name('create');
            Route::post('/',               [ItemsController::class, 'store'])->name('store');
            Route::get('/{item}/edit',     [ItemsController::class, 'edit'])->name('edit');
            Route::put('/{item}',          [ItemsController::class, 'update'])->name('update');
            Route::patch('/{item}/toggle', [ItemsController::class, 'toggleActive'])->name('toggle');
            Route::delete('/{item}',       [ItemsController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('uoms')->name('uoms.')->group(function () {
            Route::get('/',          [UomsController::class, 'index'])->name('index');
            Route::post('/',         [UomsController::class, 'store'])->name('store');
            Route::put('/{uom}',     [UomsController::class, 'update'])->name('update');
            Route::delete('/{uom}',  [UomsController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('categories')->name('categories.')->group(function () {
            Route::get('/',                  [ItemCategoriesController::class, 'index'])->name('index');
            Route::post('/',                 [ItemCategoriesController::class, 'store'])->name('store');
            Route::put('/{itemCategory}',    [ItemCategoriesController::class, 'update'])->name('update');
            Route::delete('/{itemCategory}', [ItemCategoriesController::class, 'destroy'])->name('destroy');
        });
        Route::prefix('warehouses')->name('warehouses.')->group(function () {
            Route::get('/search',          [WarehousesController::class, 'search'])->name('search');
            Route::get('/',                [WarehousesController::class, 'index'])->name('index');
            Route::post('/',               [WarehousesController::class, 'store'])->name('store');
            Route::put('/{warehouse}',     [WarehousesController::class, 'update'])->name('update');
            Route::delete('/{warehouse}',  [WarehousesController::class, 'destroy'])->name('destroy');
        });
    });


    Route::prefix('reports')->name('reports.')->group(function () {
        Route::get('/profit-loss', [ProfitLossController::class, 'index'])->name('profit-loss');
        Route::get('/profit-loss/export', [ProfitLossController::class, 'export'])->name('profit-loss.export');
        Route::post('/profit-loss/email', [ProfitLossController::class, 'email'])->name('profit-loss.email');
        Route::get('/balance-sheet', [BalanceSheetController::class, 'index'])->name('balance-sheet');
        Route::get('/journal', [JournalReportController::class, 'index'])->name('journal');
        Route::get('/trial-balance', [TrialBalanceController::class, 'index'])->name('trial-balance');
        Route::get('/general-ledger', [GeneralLedgerController::class, 'index'])->name('general-ledger');
        Route::get('/general-ledger/export', [GeneralLedgerController::class, 'export'])->name('general-ledger.export');
        Route::get('/general-ledger/export/pdf', [GeneralLedgerController::class, 'exportPdf'])->name('general-ledger.export-pdf');
        Route::post('/general-ledger/email', [GeneralLedgerController::class, 'email'])->name('general-ledger.email');
    });

    Route::prefix('banking')->name('banking.')->group(function () {
        Route::get('/reconciliation', [BankReconciliationController::class, 'index'])->name('reconciliation');
        Route::get('/reconciliation/statement-template', [BankReconciliationController::class, 'statementTemplate'])->name('reconciliation.statement-template');
        Route::post('/reconciliation/account/{account}/start', [BankReconciliationController::class, 'start'])->name('reconciliation.start');
        Route::get('/reconciliation/{reconciliation}', [BankReconciliationController::class, 'show'])->name('reconciliation.show');
        Route::post('/reconciliation/{reconciliation}/save', [BankReconciliationController::class, 'save'])->name('reconciliation.save');
        Route::post('/reconciliation/{reconciliation}/finalize', [BankReconciliationController::class, 'finalize'])->name('reconciliation.finalize');
        Route::post('/reconciliation/{reconciliation}/undo', [BankReconciliationController::class, 'undo'])->name('reconciliation.undo');
        Route::post('/reconciliation/{reconciliation}/import-statement', [BankReconciliationController::class, 'importStatement'])->name('reconciliation.import-statement');
        Route::post('/reconciliation/{reconciliation}/auto-match', [BankReconciliationController::class, 'autoMatch'])->name('reconciliation.auto-match');
        Route::get('/cheque-print', [ChequePrintController::class, 'index'])->name('cheque-print.index');
        Route::post('/cheque-print/add', [ChequePrintController::class, 'add'])->name('cheque-print.add');
        Route::post('/cheque-print/remove', [ChequePrintController::class, 'remove'])->name('cheque-print.remove');
        Route::post('/cheque-print/print', [ChequePrintController::class, 'print'])->name('cheque-print.print');
        Route::prefix('cheque-books')->name('cheque-books.')->group(function () {
            Route::get('/', [ChequeBooksController::class, 'index'])->name('index');
            Route::post('/', [ChequeBooksController::class, 'store'])->name('store');
            Route::put('/{chequeBook}', [ChequeBooksController::class, 'update'])->name('update');
            Route::delete('/{chequeBook}', [ChequeBooksController::class, 'destroy'])->name('destroy');
        });

        Route::prefix('issued-void-cheques')->name('issued-void-cheques.')->group(function () {
            Route::get('/', [IssuedVoidChequesController::class, 'index'])->name('index');
            Route::get('/export', [IssuedVoidChequesController::class, 'export'])->name('export');
            Route::get('/print', [IssuedVoidChequesController::class, 'print'])->name('print');
            Route::post('/email', [IssuedVoidChequesController::class, 'email'])->name('email');
            Route::post('/{voucher}/void', [IssuedVoidChequesController::class, 'void'])->name('void');
            Route::post('/{voucher}/print-cheque', [IssuedVoidChequesController::class, 'printCheque'])->name('print-cheque');
        });

        Route::prefix('cheque-formats')->name('cheque-formats.')->group(function () {
            Route::get('/', [ChequeFormatsController::class, 'index'])->name('index');
            Route::post('/', [ChequeFormatsController::class, 'store'])->name('store');
            Route::get('/{chequeFormat}/design', [ChequeFormatsController::class, 'design'])->name('design');
            Route::put('/{chequeFormat}', [ChequeFormatsController::class, 'update'])->name('update');
            Route::delete('/{chequeFormat}', [ChequeFormatsController::class, 'destroy'])->name('destroy');
        });
    });

    Route::prefix('payroll')->name('payroll.')->group(function () {
        foreach (['departments', 'designations', 'projects', 'branches'] as $lookup) {
            Route::prefix($lookup)->name("{$lookup}.")->group(function () use ($lookup) {
                Route::get('/', [PayrollLookupController::class, 'index'])->defaults('lookup', $lookup)->name('index');
                Route::post('/', [PayrollLookupController::class, 'store'])->defaults('lookup', $lookup)->name('store');
                Route::put('/{record}', [PayrollLookupController::class, 'update'])->defaults('lookup', $lookup)->name('update');
                Route::patch('/{record}/toggle', [PayrollLookupController::class, 'toggle'])->defaults('lookup', $lookup)->name('toggle');
                Route::delete('/{record}', [PayrollLookupController::class, 'destroy'])->defaults('lookup', $lookup)->name('destroy');
            });
        }
        Route::get('/salary-components', [PayrollSalaryComponentsController::class, 'index'])->name('salary-components.index');
        Route::post('/salary-components', [PayrollSalaryComponentsController::class, 'store'])->name('salary-components.store');
        Route::put('/salary-components/{salaryComponent}', [PayrollSalaryComponentsController::class, 'update'])->name('salary-components.update');
        Route::patch('/salary-components/{salaryComponent}/toggle', [PayrollSalaryComponentsController::class, 'toggle'])->name('salary-components.toggle');
        Route::delete('/salary-components/{salaryComponent}', [PayrollSalaryComponentsController::class, 'destroy'])->name('salary-components.destroy');
        Route::get('/payroll-sheet', [PayrollSheetController::class, 'index'])->name('payroll-sheet.index');
        Route::get('/payroll-sheet/print', [PayrollSheetController::class, 'print'])->name('payroll-sheet.print');
        Route::get('/payroll-sheet/vouchers/{payrollVoucher}/print', [PayrollSheetController::class, 'print'])->name('payroll-sheet.voucher.print');
        Route::post('/payroll-sheet/submit', [PayrollSheetController::class, 'submit'])->name('payroll-sheet.submit');
        Route::post('/payroll-sheet/vouchers/{payrollVoucher}/approve', [PayrollSheetController::class, 'approve'])->name('payroll-sheet.approve');
        Route::get('/journal', [PayrollModuleController::class, 'journal'])->name('journal.index');
        Route::get('/pay-slip', [PaySlipController::class, 'index'])->name('pay-slip.index');
        Route::get('/pay-slip/vouchers/{payrollVoucher}/print', [PaySlipController::class, 'print'])->name('pay-slip.print');
        Route::post('/pay-slip/email', [PaySlipController::class, 'email'])->name('pay-slip.email');
        Route::get('/bank-transfer-letter', [BankTransferLetterController::class, 'index'])->name('bank-transfer-letter.index');
        Route::get('/bank-transfer-letter/print', [BankTransferLetterController::class, 'print'])->name('bank-transfer-letter.print');
    });

    Route::prefix('fixed-assets')->name('fixed-assets.')->group(function () {
        Route::get('/register', [FixedAssetsController::class, 'index'])->name('register.index');
        Route::post('/register', [FixedAssetsController::class, 'store'])->name('register.store');
        Route::put('/register/{fixedAsset}', [FixedAssetsController::class, 'update'])->name('register.update');
        Route::patch('/register/{fixedAsset}/toggle', [FixedAssetsController::class, 'toggle'])->name('register.toggle');
        Route::delete('/register/{fixedAsset}', [FixedAssetsController::class, 'destroy'])->name('register.destroy');

        Route::get('/categories', [FixedAssetCategoriesController::class, 'index'])->name('categories.index');
        Route::post('/categories', [FixedAssetCategoriesController::class, 'store'])->name('categories.store');
        Route::put('/categories/{fixedAssetCategory}', [FixedAssetCategoriesController::class, 'update'])->name('categories.update');
        Route::patch('/categories/{fixedAssetCategory}/toggle', [FixedAssetCategoriesController::class, 'toggle'])->name('categories.toggle');
        Route::delete('/categories/{fixedAssetCategory}', [FixedAssetCategoriesController::class, 'destroy'])->name('categories.destroy');

        Route::get('/depreciation', [FixedAssetDepreciationController::class, 'index'])->name('depreciation.index');
        Route::post('/depreciation/run', [FixedAssetDepreciationController::class, 'run'])->name('depreciation.run');
    });

    Route::prefix('accounting')->group(function () {
        Route::get('/vouchers/{voucherType}',        [AccountingVouchersController::class, 'index'])->name('vouchers.index');
        Route::get('/vouchers/{voucherType}/print',  [AccountingVouchersController::class, 'printList'])->name('vouchers.print');
        Route::get('/vouchers/{voucherType}/export', [AccountingVouchersController::class, 'exportList'])->name('vouchers.export');
        Route::get('/vouchers/{voucherType}/create', [AccountingVouchersController::class, 'create'])->name('vouchers.create');
        Route::get('/vouchers/cheque-books/for-account/{account}', [VoucherChequeBookController::class, 'forAccount'])->name('vouchers.cheque-books.for-account');
        Route::get('/vouchers/cheque-books/{chequeBook}/leaves', [VoucherChequeBookController::class, 'leaves'])->name('vouchers.cheque-books.leaves');
        Route::post('/vouchers/{voucherType}',       [AccountingVouchersController::class, 'store'])->name('vouchers.store');
        Route::get('/vouchers/{voucherType}/{voucher}',       [AccountingVouchersController::class, 'show'])->name('vouchers.show');
        Route::get('/vouchers/{voucherType}/{voucher}/edit',  [AccountingVouchersController::class, 'edit'])->name('vouchers.edit');
        Route::put('/vouchers/{voucherType}/{voucher}',       [AccountingVouchersController::class, 'update'])->name('vouchers.update');
        Route::delete('/vouchers/{voucherType}/{voucher}',    [AccountingVouchersController::class, 'destroy'])->name('vouchers.destroy');
        Route::post('/vouchers/{voucherType}/{voucher}/submit',  [AccountingVouchersController::class, 'submit'])->name('vouchers.submit');
        Route::post('/vouchers/{voucherType}/{voucher}/approve', [AccountingVouchersController::class, 'approve'])->name('vouchers.approve');
        Route::post('/vouchers/{voucherType}/{voucher}/reject',  [AccountingVouchersController::class, 'reject'])->name('vouchers.reject');
        Route::post('/vouchers/{voucherType}/{voucher}/post',    [AccountingVouchersController::class, 'post'])->name('vouchers.post');
        Route::post('/vouchers/{voucherType}/{voucher}/cancel',  [AccountingVouchersController::class, 'cancel'])->name('vouchers.cancel');
        Route::post('/vouchers/{voucherType}/{voucher}/reverse', [AccountingVouchersController::class, 'reverse'])->name('vouchers.reverse');
        Route::post('/vouchers/{voucherType}/{voucher}/attachments', [AccountingVouchersController::class, 'storeAttachment'])->name('vouchers.attachments.store');
        Route::get('/vouchers/{voucherType}/{voucher}/attachments/{attachment}', [AccountingVouchersController::class, 'downloadAttachment'])->name('vouchers.attachments.download');
        Route::delete('/vouchers/{voucherType}/{voucher}/attachments/{attachment}', [AccountingVouchersController::class, 'destroyAttachment'])->name('vouchers.attachments.destroy');
    });
});
