<?php

use App\Http\Controllers\Admin\AuditLogController;
use App\Http\Controllers\Admin\BranchController;
use App\Http\Controllers\Admin\BusinessSettingsController;
use App\Http\Controllers\Admin\ContractTemplateController;
use App\Http\Controllers\Admin\ContractTemplateDeletionController;
use App\Http\Controllers\Admin\RoleController;
use App\Http\Controllers\Admin\SystemSettingController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\NewPasswordController;
use App\Http\Controllers\Auth\PasswordResetLinkController;
use App\Http\Controllers\BlacklistController;
use App\Http\Controllers\CollateralCategoryController;
use App\Http\Controllers\CollateralController;
use App\Http\Controllers\CreditScoreController;
use App\Http\Controllers\CustomerController;
use App\Http\Controllers\CustomerDocumentController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\GuarantorController;
use App\Http\Controllers\LoanApplicationController;
use App\Http\Controllers\LoanApprovalController;
use App\Http\Controllers\LoanContractController;
use App\Http\Controllers\LoanProductController;
use App\Http\Controllers\NinLookupController;
use Illuminate\Support\Facades\Route;

Route::redirect('/', '/dashboard')->name('home');
Route::middleware('guest')->group(function (): void {
    Route::get('/login',[AuthenticatedSessionController::class,'create'])->name('login');
    Route::post('/login',[AuthenticatedSessionController::class,'store'])->middleware('throttle:6,1')->name('login.store');
    Route::get('/forgot-password',[PasswordResetLinkController::class,'create'])->name('password.request');
    Route::post('/forgot-password',[PasswordResetLinkController::class,'store'])->middleware('throttle:5,1')->name('password.email');
    Route::get('/reset-password/{token}',[NewPasswordController::class,'create'])->name('password.reset');
    Route::post('/reset-password',[NewPasswordController::class,'store'])->middleware('throttle:6,1')->name('password.update');
});

Route::middleware('auth')->group(function (): void {
    Route::get('/dashboard',DashboardController::class)->name('dashboard');
    Route::post('/logout',[AuthenticatedSessionController::class,'destroy'])->name('logout');

    Route::get('/customers',[CustomerController::class,'index'])->middleware('permission:customers.view')->name('customers.index');
    Route::get('/customers/create',[CustomerController::class,'create'])->middleware('permission:customers.create')->name('customers.create');
    Route::post('/customers',[CustomerController::class,'store'])->middleware('permission:customers.create')->name('customers.store');
    Route::post('/customers/nin-lookup',NinLookupController::class)->middleware('permission:customers.create')->name('customers.nin-lookup');
    Route::get('/customers/{customer}/credit-score-history',[CreditScoreController::class,'history'])->middleware(['permission:credit-score.history','can:view,customer'])->name('customers.credit-score.history');
    Route::post('/customers/{customer}/credit-score/recalculate',[CreditScoreController::class,'recalculate'])->middleware(['permission:credit-score.recalculate','can:view,customer'])->name('customers.credit-score.recalculate');
    Route::get('/customers/{customer}',[CustomerController::class,'show'])->middleware(['permission:customers.view','can:view,customer'])->name('customers.show');
    Route::get('/customers/{customer}/edit',[CustomerController::class,'edit'])->middleware(['permission:customers.update','can:update,customer'])->name('customers.edit');
    Route::put('/customers/{customer}',[CustomerController::class,'update'])->middleware(['permission:customers.update','can:update,customer'])->name('customers.update');
    Route::post('/customers/{customer}/documents',[CustomerDocumentController::class,'store'])->middleware(['permission:customers.documents','can:manageDocuments,customer'])->name('customers.documents.store');
    Route::get('/customers/{customer}/documents/{document}/download',[CustomerDocumentController::class,'download'])->middleware(['permission:customers.documents','can:manageDocuments,customer'])->name('customers.documents.download');
    Route::post('/customers/{customer}/guarantors',[GuarantorController::class,'store'])->middleware(['permission:customers.guarantors','can:manageGuarantors,customer'])->name('customers.guarantors.store');
    Route::post('/customers/{customer}/watchlist',[BlacklistController::class,'store'])->middleware(['permission:customers.watchlist','can:manageWatchlist,customer'])->name('customers.watchlist.store');
    Route::post('/customers/{customer}/watchlist/{record}/resolve',[BlacklistController::class,'resolve'])->middleware(['permission:customers.watchlist','can:manageWatchlist,customer'])->name('customers.watchlist.resolve');

    Route::get('/loan-products',[LoanProductController::class,'index'])->middleware('permission:loan-products.view')->name('loan-products.index');
    Route::get('/loan-products/create',[LoanProductController::class,'create'])->middleware('permission:loan-products.manage')->name('loan-products.create');
    Route::post('/loan-products',[LoanProductController::class,'store'])->middleware('permission:loan-products.manage')->name('loan-products.store');
    Route::get('/loan-products/{loanProduct}',[LoanProductController::class,'show'])->middleware(['permission:loan-products.view','can:view,loanProduct'])->name('loan-products.show');
    Route::get('/loan-products/{loanProduct}/edit',[LoanProductController::class,'edit'])->middleware(['permission:loan-products.manage','can:update,loanProduct'])->name('loan-products.edit');
    Route::put('/loan-products/{loanProduct}',[LoanProductController::class,'update'])->middleware(['permission:loan-products.manage','can:update,loanProduct'])->name('loan-products.update');
    Route::post('/loan-products/{loanProduct}/preview',[LoanProductController::class,'preview'])->middleware(['permission:loan-products.view','can:preview,loanProduct'])->name('loan-products.preview');

    Route::get('/loans',[LoanApplicationController::class,'index'])->middleware('permission:loans.view')->name('loans.index');
    Route::get('/loans/create',[LoanApplicationController::class,'create'])->middleware('permission:loans.create')->name('loans.create');
    Route::post('/loans/preview',[LoanApplicationController::class,'preview'])->middleware('permission:loans.create')->name('loans.preview');
    Route::post('/loans',[LoanApplicationController::class,'store'])->middleware('permission:loans.create')->name('loans.store');
    Route::get('/loan-approvals',[LoanApprovalController::class,'index'])->middleware('permission:loans.approve')->name('loans.approvals.index');
    Route::post('/loans/{loan}/approval-decision',[LoanApprovalController::class,'decide'])->middleware('permission:loans.approve')->name('loans.approvals.decide');
    Route::post('/loans/{loan}/additional-collateral',[LoanApprovalController::class,'addCollateral'])->middleware('permission:loans.update')->name('loans.additional-collateral');
    Route::get('/loans/{loan}',[LoanApplicationController::class,'show'])->middleware(['permission:loans.view','can:view,loan'])->name('loans.show');
    Route::get('/loans/{loan}/edit',[LoanApplicationController::class,'edit'])->middleware(['permission:loans.update','can:update,loan'])->name('loans.edit');
    Route::put('/loans/{loan}',[LoanApplicationController::class,'update'])->middleware(['permission:loans.update','can:update,loan'])->name('loans.update');
    Route::post('/loans/{loan}/transition',[LoanApplicationController::class,'transition'])->middleware(['permission:loans.update','can:transition,loan'])->name('loans.transition');
    Route::get('/loans/{loan}/contract-preview',[LoanContractController::class,'preview'])->middleware(['permission:loans.contract','can:generateContract,loan'])->name('loans.contract-preview');
    Route::post('/loans/{loan}/contract-preview',[LoanContractController::class,'preview'])->middleware(['permission:loans.contract','can:generateContract,loan'])->name('loans.contract-preview.refresh');
    Route::get('/loans/{loan}/contract-preview/pdf',[LoanContractController::class,'previewPdf'])->middleware(['permission:loans.contract','can:generateContract,loan'])->name('loans.contract-preview.pdf');
    Route::post('/loans/{loan}/contract',[LoanContractController::class,'generate'])->middleware(['permission:loans.contract','can:generateContract,loan'])->name('loans.contract');
    Route::post('/loans/{loan}/signed-contract',[LoanApplicationController::class,'uploadSignedContract'])->middleware(['permission:loans.documents','can:manageDocuments,loan'])->name('loans.signed-contract');
    Route::post('/loans/{loan}/documents',[LoanApplicationController::class,'uploadDocument'])->middleware(['permission:loans.documents','can:manageDocuments,loan'])->name('loans.documents.store');
    Route::get('/loans/{loan}/documents/{document}/download',[LoanApplicationController::class,'downloadDocument'])->middleware(['permission:loans.view','can:downloadDocument,loan'])->name('loans.documents.download');

    Route::get('/collateral',[CollateralController::class,'index'])->middleware('permission:collateral.view')->name('collateral.index');
    Route::get('/collateral/create',[CollateralController::class,'create'])->middleware('permission:collateral.manage')->name('collateral.create');
    Route::post('/collateral',[CollateralController::class,'store'])->middleware('permission:collateral.manage')->name('collateral.store');
    Route::get('/collateral/categories',[CollateralCategoryController::class,'index'])->middleware('permission:collateral.manage')->name('collateral.categories.index');
    Route::post('/collateral/categories',[CollateralCategoryController::class,'store'])->middleware('permission:collateral.manage')->name('collateral.categories.store');
    Route::put('/collateral/categories/{category}',[CollateralCategoryController::class,'update'])->middleware('permission:collateral.manage')->name('collateral.categories.update');
    Route::get('/collateral/{collateral}',[CollateralController::class,'show'])->middleware(['permission:collateral.view','can:view,collateral'])->name('collateral.show');
    Route::get('/collateral/{collateral}/edit',[CollateralController::class,'edit'])->middleware(['permission:collateral.manage','can:update,collateral'])->name('collateral.edit');
    Route::put('/collateral/{collateral}',[CollateralController::class,'update'])->middleware(['permission:collateral.manage','can:update,collateral'])->name('collateral.update');
    Route::post('/collateral/{collateral}/movement',[CollateralController::class,'move'])->middleware(['permission:collateral.custody','can:move,collateral'])->name('collateral.move');
    Route::post('/collateral/{collateral}/files',[CollateralController::class,'storeFile'])->middleware(['permission:collateral.documents','can:manageDocuments,collateral'])->name('collateral.files.store');
    Route::get('/collateral/{collateral}/files/{file}/download',[CollateralController::class,'downloadFile'])->middleware(['permission:collateral.view','can:view,collateral'])->name('collateral.files.download');
    Route::get('/collateral/{collateral}/receipt',[CollateralController::class,'receipt'])->middleware(['permission:collateral.view','can:view,collateral'])->name('collateral.receipt');
    Route::get('/collateral/{collateral}/label',[CollateralController::class,'label'])->middleware(['permission:collateral.view','can:view,collateral'])->name('collateral.label');
    Route::get('/collateral/{collateral}/release-acknowledgement',[CollateralController::class,'releaseAcknowledgement'])->middleware(['permission:collateral.view','can:view,collateral'])->name('collateral.release-acknowledgement');

    Route::prefix('admin')->name('admin.')->group(function (): void {
        Route::get('/business-settings',[BusinessSettingsController::class,'edit'])->middleware('permission:business.settings.view')->name('business-settings.edit');
        Route::put('/business-settings',[BusinessSettingsController::class,'update'])->middleware('permission:business.settings.update')->name('business-settings.update');
        Route::get('/contract-templates',[ContractTemplateController::class,'index'])->middleware('permission:business.settings.view')->name('contract-templates.index');
        Route::get('/contract-templates/create',[ContractTemplateController::class,'create'])->middleware('permission:business.settings.update')->name('contract-templates.create');
        Route::post('/contract-templates',[ContractTemplateController::class,'store'])->middleware('permission:business.settings.update')->name('contract-templates.store');
        Route::get('/contract-templates/{contractTemplate}/edit',[ContractTemplateController::class,'edit'])->middleware('permission:business.settings.update')->name('contract-templates.edit');
        Route::put('/contract-templates/{contractTemplate}',[ContractTemplateController::class,'update'])->middleware('permission:business.settings.update')->name('contract-templates.update');
        Route::delete('/contract-templates/{contractTemplate}',ContractTemplateDeletionController::class)->middleware('permission:business.settings.update')->name('contract-templates.destroy');
        Route::post('/contract-templates/{contractTemplate}/default',[ContractTemplateController::class,'makeDefault'])->middleware('permission:business.settings.update')->name('contract-templates.default');
        Route::get('/contract-templates/{contractTemplate}/preview-source',[ContractTemplateController::class,'previewSource'])->middleware('permission:business.settings.view')->name('contract-templates.preview-source');
        Route::get('/contract-templates/{contractTemplate}/download',[ContractTemplateController::class,'download'])->middleware('permission:business.settings.view')->name('contract-templates.download');
        Route::get('/system-settings',[SystemSettingController::class,'edit'])->middleware('permission:system.settings.manage')->name('system-settings.edit');
        Route::put('/system-settings',[SystemSettingController::class,'update'])->middleware('permission:system.settings.manage')->name('system-settings.update');
        Route::post('/system-settings/sms/test',[SystemSettingController::class,'testSms'])->middleware(['permission:system.settings.manage','throttle:5,1'])->name('system-settings.sms.test');
        Route::get('/branches',[BranchController::class,'index'])->middleware('permission:branches.view')->name('branches.index');
        Route::get('/branches/create',[BranchController::class,'create'])->middleware('permission:branches.create')->name('branches.create');
        Route::post('/branches',[BranchController::class,'store'])->middleware('permission:branches.create')->name('branches.store');
        Route::get('/branches/{branch}/edit',[BranchController::class,'edit'])->middleware('permission:branches.update')->name('branches.edit');
        Route::put('/branches/{branch}',[BranchController::class,'update'])->middleware('permission:branches.update')->name('branches.update');
        Route::get('/staff',[UserController::class,'index'])->middleware('permission:users.view')->name('users.index');
        Route::get('/staff/create',[UserController::class,'create'])->middleware('permission:users.create')->name('users.create');
        Route::post('/staff',[UserController::class,'store'])->middleware('permission:users.create')->name('users.store');
        Route::get('/staff/{user}/edit',[UserController::class,'edit'])->middleware('permission:users.update')->name('users.edit');
        Route::put('/staff/{user}',[UserController::class,'update'])->middleware('permission:users.update')->name('users.update');
        Route::get('/roles',[RoleController::class,'index'])->middleware('permission:roles.view')->name('roles.index');
        Route::get('/roles/create',[RoleController::class,'create'])->middleware('permission:roles.create')->name('roles.create');
        Route::post('/roles',[RoleController::class,'store'])->middleware('permission:roles.create')->name('roles.store');
        Route::get('/roles/{role}/edit',[RoleController::class,'edit'])->middleware('permission:roles.update')->name('roles.edit');
        Route::put('/roles/{role}',[RoleController::class,'update'])->middleware('permission:roles.update')->name('roles.update');
        Route::delete('/roles/{role}',[RoleController::class,'destroy'])->middleware('permission:roles.delete')->name('roles.destroy');
        Route::get('/audit-logs',[AuditLogController::class,'index'])->middleware('permission:audit.view')->name('audit.index');
    });
});

require __DIR__.'/security.php';
require __DIR__.'/accounting.php';
require __DIR__.'/notifications.php';
require __DIR__.'/arrears.php';
require __DIR__.'/topups.php';
require __DIR__.'/loan-adjustments.php';
require __DIR__.'/loan-write-offs.php';
require __DIR__.'/expenses.php';
require __DIR__.'/payroll.php';
require __DIR__.'/cash-closing.php';
require __DIR__.'/management-work.php';
require __DIR__.'/global-search.php';
