<?php

use App\Http\Controllers\AccountingLedgerController;
use App\Http\Controllers\CapitalInjectionController;
use App\Http\Controllers\FinancialAccountController;
use App\Http\Controllers\FinancialTransferController;
use App\Http\Controllers\LoanDisbursementController;
use App\Http\Controllers\LoanPaymentController;
use App\Http\Controllers\PublicRemotePaymentController;
use App\Http\Controllers\RemotePaymentController;
use Illuminate\Support\Facades\Route;

Route::middleware('module:remote_payments')->group(function (): void {
    Route::get('/remote-payments/submit',[PublicRemotePaymentController::class,'create'])->name('remote-payments.public.create');
    Route::post('/remote-payments/submit',[PublicRemotePaymentController::class,'store'])->middleware('throttle:8,1')->name('remote-payments.public.store');
});

Route::middleware('auth')->group(function (): void {
    Route::get('/financial-accounts',[FinancialAccountController::class,'index'])->middleware('permission:accounts.view')->name('accounts.index');
    Route::get('/financial-accounts/create',[FinancialAccountController::class,'create'])->middleware('permission:accounts.manage')->name('accounts.create');
    Route::post('/financial-accounts',[FinancialAccountController::class,'store'])->middleware('permission:accounts.manage')->name('accounts.store');
    Route::get('/financial-accounts/{account}',[FinancialAccountController::class,'show'])->middleware(['permission:accounts.view','can:view,account'])->name('accounts.show');
    Route::get('/financial-accounts/{account}/edit',[FinancialAccountController::class,'edit'])->middleware(['permission:accounts.manage','can:manage,account'])->name('accounts.edit');
    Route::put('/financial-accounts/{account}',[FinancialAccountController::class,'update'])->middleware(['permission:accounts.manage','can:manage,account'])->name('accounts.update');

    Route::get('/business-capital/create',[CapitalInjectionController::class,'create'])->middleware('permission:accounts.capital')->name('capital-injections.create');
    Route::post('/business-capital',[CapitalInjectionController::class,'store'])->middleware('permission:accounts.capital')->name('capital-injections.store');
    Route::get('/account-transfers/create',[FinancialTransferController::class,'create'])->middleware('permission:accounts.transfer')->name('account-transfers.create');
    Route::post('/account-transfers',[FinancialTransferController::class,'store'])->middleware('permission:accounts.transfer')->name('account-transfers.store');
    Route::get('/accounting-ledger',[AccountingLedgerController::class,'index'])->middleware('permission:accounting.view')->name('accounting-ledger.index');

    Route::get('/loan-disbursements',[LoanDisbursementController::class,'index'])->middleware('permission:loans.disburse')->name('loan-disbursements.index');
    Route::get('/loans/{loan}/disburse',[LoanDisbursementController::class,'create'])->middleware(['permission:loans.disburse','can:disburse,loan'])->name('loan-disbursements.create');
    Route::post('/loans/{loan}/disburse',[LoanDisbursementController::class,'store'])->middleware(['permission:loans.disburse','can:disburse,loan'])->name('loan-disbursements.store');
    Route::get('/loan-disbursements/{disbursement}/receipt',[LoanDisbursementController::class,'receipt'])->middleware(['permission:loans.view','can:view,disbursement'])->name('loan-disbursements.receipt');

    Route::get('/loan-payments',[LoanPaymentController::class,'index'])->middleware('permission:payments.receive')->name('loan-payments.index');
    Route::get('/loans/{loan}/payment',[LoanPaymentController::class,'create'])->middleware(['permission:payments.receive','can:receivePayment,loan'])->name('loan-payments.create');
    Route::post('/loans/{loan}/payment',[LoanPaymentController::class,'store'])->middleware(['permission:payments.receive','can:receivePayment,loan'])->name('loan-payments.store');
    Route::get('/loan-payments/{payment}/receipt',[LoanPaymentController::class,'receipt'])->middleware(['permission:loans.view','can:view,payment'])->name('loan-payments.receipt');
    Route::get('/loans/{loan}/statement',[LoanPaymentController::class,'statement'])->middleware(['permission:loans.view','can:view,loan'])->name('loan-payments.statement');

    Route::middleware('module:remote_payments')->group(function (): void {
        Route::get('/remote-payments',[RemotePaymentController::class,'index'])->middleware('permission:payments.remote.submit')->name('remote-payments.index');
        Route::get('/remote-payments/create',[RemotePaymentController::class,'create'])->middleware('permission:payments.remote.submit')->name('remote-payments.create');
        Route::post('/remote-payments',[RemotePaymentController::class,'store'])->middleware('permission:payments.remote.submit')->name('remote-payments.store');
        Route::get('/remote-payments/{submission}',[RemotePaymentController::class,'show'])->middleware(['permission:payments.remote.verify','can:view,submission'])->name('remote-payments.show');
        Route::get('/remote-payments/{submission}/proof',[RemotePaymentController::class,'proof'])->middleware(['permission:payments.remote.verify','can:proof,submission'])->name('remote-payments.proof');
        Route::post('/remote-payments/{submission}/approve',[RemotePaymentController::class,'approve'])->middleware(['permission:payments.remote.verify','can:verify,submission'])->name('remote-payments.approve');
        Route::post('/remote-payments/{submission}/reject',[RemotePaymentController::class,'reject'])->middleware(['permission:payments.remote.verify','can:verify,submission'])->name('remote-payments.reject');
    });
});

require __DIR__.'/fixed-assets.php';
require __DIR__.'/reports.php';
require __DIR__.'/documents.php';
