<?php

use App\Http\Controllers\Auth\AuthenticatedSessionController;
use App\Http\Controllers\Auth\NewPasswordController;
use App\Http\Controllers\Auth\PasswordResetLinkController;
use App\Http\Controllers\Auth\TenantRegistrationController;
use App\Http\Controllers\Agent\DashboardController as AgentDashboardController;
use App\Http\Controllers\Agent\SoftwareProductController as AgentSoftwareProductController;
use App\Http\Controllers\PublicAgentController;
use App\Http\Controllers\PublicTenantController;
use App\Http\Controllers\SyncPullController;
use App\Http\Controllers\SyncReceiveController;
use App\Http\Controllers\SuperAdmin\AgentController as SuperAgentController;
use App\Http\Controllers\SuperAdmin\BillingController as SuperBillingController;
use App\Http\Controllers\SuperAdmin\CommunicationUnitController as SuperCommunicationUnitController;
use App\Http\Controllers\SuperAdmin\DashboardController as SuperDashboardController;
use App\Http\Controllers\SuperAdmin\EmailDeliveryLogController as SuperEmailDeliveryLogController;
use App\Http\Controllers\SuperAdmin\ImpersonationController;
use App\Http\Controllers\SuperAdmin\NotificationController as SuperNotificationController;
use App\Http\Controllers\SuperAdmin\PlanController as SuperPlanController;
use App\Http\Controllers\SuperAdmin\PlatformProductController as SuperPlatformProductController;
use App\Http\Controllers\SuperAdmin\SettingController as SuperSettingController;
use App\Http\Controllers\SuperAdmin\SupportTicketController as SuperSupportTicketController;
use App\Http\Controllers\SuperAdmin\TenantController as SuperTenantController;
use App\Http\Controllers\SuperAdmin\VtuWalletController as SuperVtuWalletController;
use App\Http\Controllers\Tenant\AccountingController;
use App\Http\Controllers\Tenant\AuditTrailController;
use App\Http\Controllers\Tenant\BillPaymentController;
use App\Http\Controllers\Tenant\BillingController as TenantBillingController;
use App\Http\Controllers\Tenant\BranchController;
use App\Http\Controllers\Tenant\BulkSmsController;
use App\Http\Controllers\Tenant\CustomerController;
use App\Http\Controllers\Tenant\CommunicationUnitController as TenantCommunicationUnitController;
use App\Http\Controllers\Tenant\CustomerKycVerificationController;
use App\Http\Controllers\Tenant\CustomerNotificationController;
use App\Http\Controllers\Tenant\CustomerVirtualAccountController;
use App\Http\Controllers\Tenant\DataImportController;
use App\Http\Controllers\Tenant\DataSyncController;
use App\Http\Controllers\Tenant\DashboardController as TenantDashboardController;
use App\Http\Controllers\Tenant\DepositController;
use App\Http\Controllers\Tenant\ExpenseController;
use App\Http\Controllers\Tenant\InvestmentController;
use App\Http\Controllers\Tenant\LoanController;
use App\Http\Controllers\Tenant\LoanPaymentController;
use App\Http\Controllers\Tenant\LoanProductController;
use App\Http\Controllers\Tenant\MemberTransferController;
use App\Http\Controllers\Tenant\MemberPortalController;
use App\Http\Controllers\Tenant\NotificationController;
use App\Http\Controllers\Tenant\ReportController;
use App\Http\Controllers\Tenant\RoleProfileController;
use App\Http\Controllers\Tenant\SavingsAccountController;
use App\Http\Controllers\Tenant\SavingsProductController;
use App\Http\Controllers\Tenant\SavingsTransactionController;
use App\Http\Controllers\Tenant\SettingController as TenantSettingController;
use App\Http\Controllers\Tenant\StaffController;
use App\Http\Controllers\Tenant\StaffWalletController;
use App\Http\Controllers\Tenant\SupportTicketController as TenantSupportTicketController;
use App\Http\Controllers\Tenant\VtuWalletController as TenantVtuWalletController;
use App\Http\Controllers\Tenant\WithdrawalController;
use App\Http\Controllers\Webhook\PaystackWebhookController;
use App\Models\SubscriptionPlan;
use App\Services\PlatformSettings;
use Illuminate\Support\Facades\Route;

Route::get('/', function () {
    return view('landing', [
        'plans' => SubscriptionPlan::query()->where('is_active', true)->orderBy('price_monthly')->get(),
        'homepage' => app(PlatformSettings::class)->homepageSettings(),
    ]);
})->name('landing');

Route::view('/user-manual', 'user-manual')->name('manual');
Route::view('/mobile-app', 'mobile-app')->name('mobile-app');

Route::middleware('guest')->group(function () {
    Route::get('/login', [AuthenticatedSessionController::class, 'create'])->name('login');
    Route::post('/login', [AuthenticatedSessionController::class, 'store']);
    Route::get('/register-company', [TenantRegistrationController::class, 'create'])->name('tenant.register');
    Route::post('/register-company', [TenantRegistrationController::class, 'store'])->name('tenant.register.store');
    Route::get('/forgot-password', [PasswordResetLinkController::class, 'create'])->name('password.request');
    Route::post('/forgot-password', [PasswordResetLinkController::class, 'store'])->name('password.email');
    Route::get('/reset-password/{token}', [NewPasswordController::class, 'create'])->name('password.reset');
    Route::post('/reset-password', [NewPasswordController::class, 'store'])->name('password.store');
});

Route::post('/logout', [AuthenticatedSessionController::class, 'destroy'])->middleware('auth')->name('logout');
Route::post('/webhooks/paystack', PaystackWebhookController::class)->name('webhooks.paystack');
Route::post('/sync/receive', SyncReceiveController::class)
    ->withoutMiddleware(\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class)
    ->name('sync.receive');
Route::post('/sync/pull', SyncPullController::class)
    ->withoutMiddleware(\Illuminate\Foundation\Http\Middleware\VerifyCsrfToken::class)
    ->name('sync.pull');

Route::get('/merchants', [PublicAgentController::class, 'landing'])->name('merchants.landing');
Route::get('/merchants/register', [PublicAgentController::class, 'create'])->middleware('guest')->name('merchants.register');
Route::post('/merchants/register', [PublicAgentController::class, 'store'])->middleware('guest')->name('merchants.register.store');
Route::get('/merchants/{agent:slug}', [PublicAgentController::class, 'show'])->name('merchants.show');
Route::get('/agents', fn () => redirect()->route('merchants.landing'))->name('agents.landing');
Route::get('/agents/register', fn () => redirect()->route('merchants.register'))->middleware('guest')->name('agents.register');
Route::post('/agents/register', [PublicAgentController::class, 'store'])->middleware('guest')->name('agents.register.store');
Route::get('/agents/{agent:slug}', fn (\App\Models\Agent $agent) => redirect()->route('merchants.show', $agent))->name('agents.show');

Route::get('/t/{tenant:slug}', [PublicTenantController::class, 'show'])->name('public.tenant.show');
Route::get('/t/{tenant:slug}/join', [PublicTenantController::class, 'register'])->middleware('guest')->name('public.tenant.register');
Route::post('/t/{tenant:slug}/join', [PublicTenantController::class, 'storeRegistration'])->middleware('guest')->name('public.tenant.register.store');
Route::get('/t/{tenant:slug}/login', [PublicTenantController::class, 'login'])->name('public.tenant.login');
Route::post('/t/{tenant:slug}/login', [PublicTenantController::class, 'authenticate'])->middleware('guest')->name('public.tenant.authenticate');

Route::middleware(['auth', 'role:super_admin'])
    ->prefix('super')
    ->as('super.')
    ->group(function () {
        Route::get('/dashboard', SuperDashboardController::class)->name('dashboard');
        Route::get('/tenants', [SuperTenantController::class, 'index'])->name('tenants.index');
        Route::get('/tenants/{tenant}', [SuperTenantController::class, 'show'])->name('tenants.show');
        Route::post('/tenants/{tenant}/suspend', [SuperTenantController::class, 'suspend'])->name('tenants.suspend');
        Route::post('/tenants/{tenant}/reactivate', [SuperTenantController::class, 'reactivate'])->name('tenants.reactivate');
        Route::delete('/tenants/{tenant}', [SuperTenantController::class, 'destroy'])->name('tenants.destroy');
        Route::post('/tenants/{tenant}/impersonate', [ImpersonationController::class, 'start'])->name('tenants.impersonate');

        Route::get('/agents', [SuperAgentController::class, 'index'])->name('agents.index');
        Route::post('/agents', [SuperAgentController::class, 'store'])->name('agents.store');
        Route::get('/agents/{agent}', [SuperAgentController::class, 'show'])->name('agents.show');
        Route::patch('/agents/{agent}', [SuperAgentController::class, 'update'])->name('agents.update');
        Route::post('/agents/{agent}/tenants', [SuperAgentController::class, 'assignTenants'])->name('agents.tenants.assign');
        Route::delete('/agents/{agent}/tenants/{tenant}', [SuperAgentController::class, 'unassignTenant'])->name('agents.tenants.unassign');
        Route::patch('/agents/{agent}/commissions/{commission}/paid', [SuperAgentController::class, 'markCommissionPaid'])->name('agents.commissions.paid');
        Route::resource('software-products', SuperPlatformProductController::class)->only(['index', 'store', 'update', 'destroy']);

        Route::resource('plans', SuperPlanController::class)->only(['index', 'store', 'update', 'destroy']);
        Route::get('/billing', [SuperBillingController::class, 'index'])->name('billing.index');
        Route::post('/billing/payments/{payment}/approve', [SuperBillingController::class, 'approve'])->name('billing.approve');
        Route::post('/billing/payments/{payment}/reject', [SuperBillingController::class, 'reject'])->name('billing.reject');
        Route::get('/notifications', [SuperNotificationController::class, 'index'])->name('notifications.index');
        Route::post('/notifications', [SuperNotificationController::class, 'store'])->name('notifications.store');
        Route::get('/communication-units', [SuperCommunicationUnitController::class, 'index'])->name('communication-units.index');
        Route::post('/communication-units/credit', [SuperCommunicationUnitController::class, 'credit'])->name('communication-units.credit');
        Route::post('/communication-units/{topup}/approve', [SuperCommunicationUnitController::class, 'approve'])->name('communication-units.approve');
        Route::post('/communication-units/{topup}/reject', [SuperCommunicationUnitController::class, 'reject'])->name('communication-units.reject');
        Route::get('/vtu-wallets', [SuperVtuWalletController::class, 'index'])->name('vtu-wallets.index');
        Route::post('/vtu-wallets/credit', [SuperVtuWalletController::class, 'credit'])->name('vtu-wallets.credit');
        Route::post('/vtu-wallets/{topup}/approve', [SuperVtuWalletController::class, 'approve'])->name('vtu-wallets.approve');
        Route::post('/vtu-wallets/{topup}/reject', [SuperVtuWalletController::class, 'reject'])->name('vtu-wallets.reject');
        Route::get('/email-logs', [SuperEmailDeliveryLogController::class, 'index'])->name('email-logs.index');
        Route::get('/support', [SuperSupportTicketController::class, 'index'])->name('support.index');
        Route::patch('/support/{ticket}', [SuperSupportTicketController::class, 'update'])->name('support.update');
        Route::get('/settings', [SuperSettingController::class, 'index'])->name('settings.index');
        Route::post('/settings', [SuperSettingController::class, 'update'])->name('settings.update');
    });

Route::post('/impersonation/stop', [ImpersonationController::class, 'stop'])
    ->middleware('auth')
    ->name('impersonation.stop');

Route::middleware(['auth', 'role:agent'])
    ->prefix('agent')
    ->as('agent.')
    ->group(function () {
        Route::get('/dashboard', AgentDashboardController::class)->name('dashboard');
    });
Route::middleware(['auth', 'role:agent'])
    ->prefix('merchant')
    ->as('merchant.')
    ->group(function () {
        Route::get('/dashboard', AgentDashboardController::class)->name('dashboard');
        Route::patch('/kyc', [AgentDashboardController::class, 'updateKyc'])->name('kyc.update');
        Route::get('/software-products', [AgentSoftwareProductController::class, 'index'])->name('software-products.index');
    });

Route::middleware(['auth', 'tenant.active', 'role:tenant_admin,manager,loan_officer,accountant', 'permission'])
    ->prefix('app')
    ->as('tenant.')
    ->group(function () {
        Route::get('/dashboard', TenantDashboardController::class)->name('dashboard');

        Route::post('/customers/bulk-update', [CustomerController::class, 'bulkUpdate'])->name('customers.bulk-update');
        Route::post('/customers/{customer}/virtual-account', [CustomerVirtualAccountController::class, 'store'])->name('customers.virtual-account.store');
        Route::post('/customers/{customer}/kyc/verify', [CustomerKycVerificationController::class, 'store'])->name('customers.kyc.verify');
        Route::patch('/customers/{customer}/notification-preferences', [CustomerController::class, 'updateNotificationPreferences'])->name('customers.notification-preferences.update');
        Route::resource('customers', CustomerController::class);
        Route::resource('staff', StaffController::class)->only(['index', 'store', 'update', 'destroy'])
            ->middleware('role:tenant_admin,manager');
        Route::resource('role-profiles', RoleProfileController::class)->only(['index', 'store', 'update', 'destroy'])
            ->middleware('role:tenant_admin,manager');
        Route::resource('branches', BranchController::class)->only(['index', 'store', 'update', 'destroy'])
            ->middleware('role:tenant_admin,manager');

        Route::resource('savings', SavingsAccountController::class)->only(['index', 'store', 'show']);
        Route::post('/savings/{saving}/approve', [SavingsAccountController::class, 'approve'])->name('savings.approve')->middleware('role:tenant_admin,manager');
        Route::post('/savings/{saving}/reject', [SavingsAccountController::class, 'reject'])->name('savings.reject')->middleware('role:tenant_admin,manager');
        Route::post('/savings/products', [SavingsProductController::class, 'store'])->name('savings-products.store')->middleware('role:tenant_admin,manager');
        Route::patch('/savings/products/{product}', [SavingsProductController::class, 'update'])->name('savings-products.update')->middleware('role:tenant_admin,manager');
        Route::delete('/savings/products/{product}', [SavingsProductController::class, 'destroy'])->name('savings-products.destroy')->middleware('role:tenant_admin,manager');
        Route::post('/savings/{savings}/transactions', [SavingsTransactionController::class, 'store'])->name('savings.transactions.store');
        Route::post('/savings/transactions/bulk-approve', [SavingsTransactionController::class, 'bulkApprove'])->name('savings.transactions.bulk-approve')->middleware('role:tenant_admin,manager');
        Route::post('/savings/transactions/bulk-reject', [SavingsTransactionController::class, 'bulkReject'])->name('savings.transactions.bulk-reject')->middleware('role:tenant_admin,manager');
        Route::post('/savings/transactions/{transaction}/approve', [SavingsTransactionController::class, 'approve'])->name('savings.transactions.approve')->middleware('role:tenant_admin,manager');
        Route::post('/savings/transactions/{transaction}/reject', [SavingsTransactionController::class, 'reject'])->name('savings.transactions.reject')->middleware('role:tenant_admin,manager');
        Route::post('/savings/transactions/{transaction}/cancel', [SavingsTransactionController::class, 'cancel'])->name('savings.transactions.cancel')->middleware('role:tenant_admin,manager');
        Route::get('/savings/transactions/{transaction}/receipt', [SavingsTransactionController::class, 'receipt'])->name('savings.transactions.receipt');
        Route::get('/deposits', [DepositController::class, 'index'])->name('deposits.index');
        Route::post('/deposits', [DepositController::class, 'store'])->name('deposits.store');
        Route::get('/withdrawals', [WithdrawalController::class, 'index'])->name('withdrawals.index');
        Route::get('/transfers', [MemberTransferController::class, 'index'])->name('transfers.index');
        Route::post('/transfers/{transfer}/approve', [MemberTransferController::class, 'approve'])->name('transfers.approve')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/transfers/{transfer}/reject', [MemberTransferController::class, 'reject'])->name('transfers.reject')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/transfers/{transfer}/cancel', [MemberTransferController::class, 'cancel'])->name('transfers.cancel')->middleware('role:tenant_admin,manager,accountant');
        Route::get('/investments', [InvestmentController::class, 'index'])->name('investments.index');
        Route::post('/investments/products', [InvestmentController::class, 'storeProduct'])->name('investment-products.store')->middleware('role:tenant_admin,manager');
        Route::patch('/investments/products/{product}', [InvestmentController::class, 'updateProduct'])->name('investment-products.update')->middleware('role:tenant_admin,manager');
        Route::delete('/investments/products/{product}', [InvestmentController::class, 'destroyProduct'])->name('investment-products.destroy')->middleware('role:tenant_admin,manager');
        Route::post('/investments/accounts', [InvestmentController::class, 'storeAccount'])->name('investments.accounts.store');
        Route::get('/investments/{investment}', [InvestmentController::class, 'show'])->name('investments.show');
        Route::post('/investments/{investment}/approve', [InvestmentController::class, 'approve'])->name('investments.approve')->middleware('role:tenant_admin,manager');
        Route::post('/investments/{investment}/reject', [InvestmentController::class, 'reject'])->name('investments.reject')->middleware('role:tenant_admin,manager');
        Route::post('/investments/{investment}/transactions', [InvestmentController::class, 'storeTransaction'])->name('investments.transactions.store');
        Route::post('/investments/transactions/{transaction}/approve', [InvestmentController::class, 'approveTransaction'])->name('investments.transactions.approve')->middleware('role:tenant_admin,manager');
        Route::post('/investments/transactions/{transaction}/reject', [InvestmentController::class, 'rejectTransaction'])->name('investments.transactions.reject')->middleware('role:tenant_admin,manager');
        Route::post('/investments/transactions/{transaction}/cancel', [InvestmentController::class, 'cancelTransaction'])->name('investments.transactions.cancel')->middleware('role:tenant_admin,manager');
        Route::get('/repayments', [LoanPaymentController::class, 'index'])->name('repayments.index');

        Route::resource('loans', LoanController::class)->only(['index', 'create', 'store', 'show', 'edit', 'update']);
        Route::post('/loans/products', [LoanProductController::class, 'store'])->name('loan-products.store')->middleware('role:tenant_admin,manager');
        Route::patch('/loans/products/{product}', [LoanProductController::class, 'update'])->name('loan-products.update')->middleware('role:tenant_admin,manager');
        Route::delete('/loans/products/{product}', [LoanProductController::class, 'destroy'])->name('loan-products.destroy')->middleware('role:tenant_admin,manager');
        Route::post('/loans/{loan}/approve', [LoanController::class, 'approve'])->name('loans.approve')->middleware('role:tenant_admin,manager');
        Route::post('/loans/{loan}/reject', [LoanController::class, 'reject'])->name('loans.reject')->middleware('role:tenant_admin,manager');
        Route::post('/loans/{loan}/disburse', [LoanController::class, 'disburse'])->name('loans.disburse')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/loans/{loan}/schedule/regenerate', [LoanController::class, 'regenerateSchedule'])->name('loans.schedule.regenerate')->middleware('role:tenant_admin,manager');
        Route::post('/loans/{loan}/reschedule', [LoanController::class, 'reschedule'])->name('loans.reschedule')->middleware('role:tenant_admin,manager');
        Route::post('/loans/{loan}/payments', [LoanPaymentController::class, 'store'])->name('loans.payments.store');
        Route::post('/loan-payments/{payment}/approve', [LoanPaymentController::class, 'approve'])->name('loan-payments.approve')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/loan-payments/{payment}/reject', [LoanPaymentController::class, 'reject'])->name('loan-payments.reject')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/loan-payments/{payment}/cancel', [LoanPaymentController::class, 'cancel'])->name('loan-payments.cancel')->middleware('role:tenant_admin,manager,accountant');
        Route::get('/loan-payments/{payment}/receipt', [LoanPaymentController::class, 'receipt'])->name('loan-payments.receipt');

        Route::get('/accounting', [AccountingController::class, 'index'])->name('accounting.index')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/accounting/accounts', [AccountingController::class, 'storeAccount'])->name('accounting.accounts.store')->middleware('role:tenant_admin,manager,accountant');
        Route::patch('/accounting/accounts/{account}', [AccountingController::class, 'updateAccount'])->name('accounting.accounts.update')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/accounting/income', [AccountingController::class, 'storeIncome'])->name('accounting.income.store')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/accounting/income/{income}/cancel', [AccountingController::class, 'cancelIncome'])->name('accounting.income.cancel')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/accounting/journals', [AccountingController::class, 'storeJournal'])->name('accounting.journals.store')->middleware('role:tenant_admin,manager,accountant');
        Route::get('/expenses', [ExpenseController::class, 'index'])->name('expenses.index')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/expenses', [ExpenseController::class, 'store'])->name('expenses.store')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/expenses/{expense}/approve', [ExpenseController::class, 'approve'])->name('expenses.approve')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/expenses/{expense}/reject', [ExpenseController::class, 'reject'])->name('expenses.reject')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/expenses/{expense}/cancel', [ExpenseController::class, 'cancel'])->name('expenses.cancel')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/expenses/categories', [ExpenseController::class, 'storeCategory'])->name('expenses.categories.store')->middleware('role:tenant_admin,manager,accountant');
        Route::patch('/expenses/categories/{category}', [ExpenseController::class, 'updateCategory'])->name('expenses.categories.update')->middleware('role:tenant_admin,manager,accountant');

        Route::get('/reports', [ReportController::class, 'index'])->name('reports.index');
        Route::get('/reports/pdf', [ReportController::class, 'pdf'])->name('reports.pdf');
        Route::get('/billing', [TenantBillingController::class, 'index'])->name('billing.index');
        Route::post('/billing/payments', [TenantBillingController::class, 'storePayment'])
            ->name('billing.payments.store')
            ->middleware('role:tenant_admin,manager,accountant');
        Route::post('/billing/payments/gateway', [TenantBillingController::class, 'startGatewayPayment'])
            ->name('billing.gateway.start')
            ->middleware('role:tenant_admin,manager,accountant');
        Route::get('/billing/payments/gateway/callback', [TenantBillingController::class, 'completeGatewayPayment'])
            ->name('billing.gateway.callback')
            ->middleware('role:tenant_admin,manager,accountant');
        Route::post('/billing/auto-renewal', [TenantBillingController::class, 'updateAutoRenewal'])
            ->name('billing.auto-renewal.update')
            ->middleware('role:tenant_admin,manager,accountant');
        Route::get('/communication-units', [TenantCommunicationUnitController::class, 'index'])->name('communication-units.index')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/communication-units/manual', [TenantCommunicationUnitController::class, 'storeManual'])->name('communication-units.manual.store')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/communication-units/gateway', [TenantCommunicationUnitController::class, 'startGateway'])->name('communication-units.gateway.start')->middleware('role:tenant_admin,manager,accountant');
        Route::get('/communication-units/gateway/callback', [TenantCommunicationUnitController::class, 'completeGateway'])->name('communication-units.gateway.callback')->middleware('role:tenant_admin,manager,accountant');
        Route::get('/vtu-wallet', [TenantVtuWalletController::class, 'index'])->name('vtu-wallet.index')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/vtu-wallet/manual', [TenantVtuWalletController::class, 'storeManual'])->name('vtu-wallet.manual.store')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/vtu-wallet/gateway', [TenantVtuWalletController::class, 'startGateway'])->name('vtu-wallet.gateway.start')->middleware('role:tenant_admin,manager,accountant');
        Route::get('/vtu-wallet/gateway/callback', [TenantVtuWalletController::class, 'completeGateway'])->name('vtu-wallet.gateway.callback')->middleware('role:tenant_admin,manager,accountant');
        Route::get('/wallet', [StaffWalletController::class, 'index'])->name('staff-wallet.index');
        Route::post('/wallet/virtual-account', [StaffWalletController::class, 'createVirtualAccount'])->name('staff-wallet.virtual-account');
        Route::post('/wallet/bill-payments', [StaffWalletController::class, 'payBill'])->name('staff-wallet.bill-payments.store');
        Route::get('/bill-payments', [BillPaymentController::class, 'index'])->name('bill-payments.index')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/bill-payments', [BillPaymentController::class, 'store'])->name('bill-payments.store')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/bill-payments/{billPayment}/requery', [BillPaymentController::class, 'requery'])->name('bill-payments.requery')->middleware('role:tenant_admin,manager,accountant');
        Route::patch('/bill-payments/{billPayment}/status', [BillPaymentController::class, 'updateStatus'])->name('bill-payments.status')->middleware('role:tenant_admin,manager,accountant');
        Route::post('/bill-payments/{billPayment}/cancel', [BillPaymentController::class, 'cancel'])->name('bill-payments.cancel')->middleware('role:tenant_admin,manager,accountant');
        Route::get('/bulk-sms', [BulkSmsController::class, 'index'])->name('bulk-sms.index')->middleware('role:tenant_admin,manager');
        Route::post('/bulk-sms', [BulkSmsController::class, 'store'])->name('bulk-sms.store')->middleware('role:tenant_admin,manager');
        Route::get('/audit-trail', AuditTrailController::class)->name('audit-trail.index')->middleware('role:tenant_admin,manager');
        Route::get('/settings', [TenantSettingController::class, 'edit'])->name('settings.edit')->middleware('role:tenant_admin,manager');
        Route::post('/settings', [TenantSettingController::class, 'update'])->name('settings.update')->middleware('role:tenant_admin,manager');
        Route::get('/data-imports', [DataImportController::class, 'index'])->name('data-imports.index')->middleware('role:tenant_admin,manager');
        Route::post('/data-imports', [DataImportController::class, 'store'])->name('data-imports.store')->middleware('role:tenant_admin,manager');
        Route::get('/data-imports/templates/{type}', [DataImportController::class, 'template'])->name('data-imports.template')->middleware('role:tenant_admin,manager');
        Route::get('/sync', [DataSyncController::class, 'index'])->name('sync.index')->middleware('role:tenant_admin,manager');
        Route::post('/sync/clients', [DataSyncController::class, 'createClient'])->name('sync.clients.store')->middleware('role:tenant_admin,manager');
        Route::post('/sync/export', [DataSyncController::class, 'export'])->name('sync.export')->middleware('role:tenant_admin,manager');
        Route::post('/sync/import', [DataSyncController::class, 'import'])->name('sync.import')->middleware('role:tenant_admin,manager');
        Route::post('/sync/remote', [DataSyncController::class, 'saveRemote'])->name('sync.remote')->middleware('role:tenant_admin,manager');
        Route::post('/sync/auto', [DataSyncController::class, 'saveAutoSettings'])->name('sync.auto')->middleware('role:tenant_admin,manager');
        Route::post('/sync/run', [DataSyncController::class, 'syncNow'])->name('sync.run')->middleware('role:tenant_admin,manager');
        Route::post('/sync/push', [DataSyncController::class, 'push'])->name('sync.push')->middleware('role:tenant_admin,manager');
        Route::post('/sync/pull', [DataSyncController::class, 'pull'])->name('sync.pull')->middleware('role:tenant_admin,manager');
        Route::get('/customer-notifications', [CustomerNotificationController::class, 'index'])->name('customer-notifications.index')->middleware('role:tenant_admin,manager');
        Route::post('/customer-notifications', [CustomerNotificationController::class, 'store'])->name('customer-notifications.store')->middleware('role:tenant_admin,manager');
        Route::post('/customer-notifications/{campaign}/resend-failed', [CustomerNotificationController::class, 'resendFailed'])->name('customer-notifications.resend-failed')->middleware('role:tenant_admin,manager');
        Route::get('/support', [TenantSupportTicketController::class, 'index'])->name('support.index');
        Route::post('/support', [TenantSupportTicketController::class, 'store'])->name('support.store');
        Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications.index');
        Route::patch('/notifications/{notification}/read', [NotificationController::class, 'markRead'])->name('notifications.read');
    });

Route::middleware(['auth', 'tenant.active', 'role:customer'])
    ->prefix('member')
    ->as('member.')
    ->group(function () {
        Route::get('/dashboard', MemberPortalController::class)->name('dashboard');
        Route::patch('/profile', [MemberPortalController::class, 'updateProfile'])->name('profile.update');
        Route::patch('/notification-preferences', [MemberPortalController::class, 'updateNotificationPreferences'])->name('notification-preferences.update');
        Route::post('/kyc/start', [MemberPortalController::class, 'startKycVerification'])->name('kyc.start');
        Route::get('/kyc/callback', [MemberPortalController::class, 'completeKycVerification'])->name('kyc.callback');
        Route::post('/savings/accounts', [MemberPortalController::class, 'openSavingsAccount'])->name('savings.accounts.store');
        Route::post('/savings/deposit', [MemberPortalController::class, 'startSavingsDeposit'])->name('savings.deposit');
        Route::get('/savings/deposit/callback', [MemberPortalController::class, 'completeSavingsDeposit'])->name('savings.gateway.callback');
        Route::post('/loans/apply', [MemberPortalController::class, 'applyForLoan'])->name('loans.apply');
        Route::post('/loans/repay', [MemberPortalController::class, 'startLoanRepayment'])->name('loans.repay');
        Route::get('/loans/repay/callback', [MemberPortalController::class, 'completeLoanRepayment'])->name('loans.gateway.callback');
        Route::post('/bill-payments', [MemberPortalController::class, 'payBill'])->name('bill-payments.store');
        Route::post('/transfers', [MemberPortalController::class, 'initiateTransfer'])->name('transfers.store');
        Route::post('/investments/request', [MemberPortalController::class, 'requestInvestment'])->name('investments.request');
    });
