<?php

use App\Http\Controllers\Admin\AccountController as AdminAccountController;
use App\Http\Controllers\Admin\AccountingController;
use App\Http\Controllers\Admin\CrmController;
use App\Http\Controllers\Admin\CustomerController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\InvestmentController;
use App\Http\Controllers\Admin\LoanController as AdminLoanController;
use App\Http\Controllers\Admin\OperationsController;
use App\Http\Controllers\Admin\ReportController;
use App\Http\Controllers\Admin\MenuController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\StaffController;
use App\Http\Controllers\Admin\TreasuryController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\PortalLoginController;
use App\Http\Controllers\Portal\AccountController as PortalAccountController;
use App\Http\Controllers\Portal\DashboardController as PortalDashboardController;
use App\Http\Controllers\Portal\LoanController as PortalLoanController;
use Illuminate\Support\Facades\Route;

Route::get('/', fn () => view('welcome'))->name('home');

Route::get('/login', [LoginController::class, 'showLoginForm'])->name('login');
Route::post('/login', [LoginController::class, 'login']);
Route::post('/logout', [LoginController::class, 'logout'])->name('logout');

Route::get('/portal/login', [PortalLoginController::class, 'showLoginForm'])->name('portal.login');
Route::post('/portal/login', [PortalLoginController::class, 'login']);
Route::post('/portal/logout', [PortalLoginController::class, 'logout'])->name('portal.logout');

Route::prefix('admin')->middleware('staff')->name('admin.')->group(function () {
    Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');

    Route::resource('customers', CustomerController::class)->except(['destroy']);

    Route::resource('loans', AdminLoanController::class)->only(['index', 'create', 'store', 'show']);
    Route::post('/loans/{loan}/approve', [AdminLoanController::class, 'approve'])->name('loans.approve');
    Route::post('/loans/{loan}/reject', [AdminLoanController::class, 'reject'])->name('loans.reject');

    Route::resource('accounts', AdminAccountController::class)->only(['index', 'create', 'store', 'show']);

    Route::resource('investments', InvestmentController::class)->only(['index', 'create', 'store', 'show']);

    Route::get('/treasury', [TreasuryController::class, 'index'])->name('treasury.index');
    Route::post('/treasury/positions', [TreasuryController::class, 'storePosition'])->name('treasury.positions.store');

    Route::get('/accounting/chart', [AccountingController::class, 'chartOfAccounts'])->name('accounting.chart');
    Route::get('/accounting/journal', [AccountingController::class, 'journalIndex'])->name('accounting.journal.index');
    Route::get('/accounting/journal/create', [AccountingController::class, 'createJournal'])->name('accounting.journal.create');
    Route::post('/accounting/journal', [AccountingController::class, 'storeJournal'])->name('accounting.journal.store');
    Route::post('/accounting/journal/{entry}/post', [AccountingController::class, 'postJournal'])->name('accounting.journal.post');

    Route::get('/crm', [CrmController::class, 'index'])->name('crm.index');
    Route::post('/crm/leads', [CrmController::class, 'storeLead'])->name('crm.leads.store');
    Route::patch('/crm/leads/{lead}/stage', [CrmController::class, 'updateLeadStage'])->name('crm.leads.stage');
    Route::post('/crm/interactions', [CrmController::class, 'storeInteraction'])->name('crm.interactions.store');

    Route::get('/operations', [OperationsController::class, 'index'])->name('operations.index');
    Route::post('/operations/{item}/approve', [OperationsController::class, 'approve'])->name('operations.approve');
    Route::post('/operations/{item}/reject', [OperationsController::class, 'reject'])->name('operations.reject');

    Route::get('/reports/portfolio', [ReportController::class, 'portfolio'])->name('reports.portfolio');
    Route::get('/reports/liquidity', [ReportController::class, 'liquidity'])->name('reports.liquidity');
    Route::get('/reports/income', [ReportController::class, 'incomeSummary'])->name('reports.income');

    Route::get('/staff', [StaffController::class, 'index'])->name('staff.index');

    Route::get('/menus', [MenuController::class, 'index'])->name('menus.index');
    Route::post('/menus', [MenuController::class, 'store'])->name('menus.store');
    Route::put('/menus/{menu}', [MenuController::class, 'update'])->name('menus.update');
    Route::delete('/menus/{menu}', [MenuController::class, 'destroy'])->name('menus.destroy');

    Route::get('/settings', [SettingController::class, 'index'])->name('settings.index');
    Route::put('/settings', [SettingController::class, 'update'])->name('settings.update');
});

Route::prefix('portal')->middleware('portal')->name('portal.')->group(function () {
    Route::get('/dashboard', [PortalDashboardController::class, 'index'])->name('dashboard');

    Route::get('/accounts', [PortalAccountController::class, 'index'])->name('accounts.index');
    Route::get('/accounts/{account}', [PortalAccountController::class, 'show'])->name('accounts.show');

    Route::get('/loans', [PortalLoanController::class, 'index'])->name('loans.index');
    Route::get('/loans/{loan}', [PortalLoanController::class, 'show'])->name('loans.show');
});
