<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Auth\OtpController;
use App\Http\Controllers\Auth\PendingController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\AccountController;
use App\Http\Controllers\TransactionController;
use App\Http\Controllers\TransferController;
use App\Http\Controllers\DepositController;
use App\Http\Controllers\WithdrawController;
use App\Http\Controllers\BrokerageController;
use App\Http\Controllers\NotificationController;
use App\Http\Controllers\BillPaymentController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\Admin\AdminDashboardController;
use App\Http\Controllers\Admin\AdminSettingsController;
use App\Http\Controllers\Admin\AdminUserController;
use App\Http\Controllers\Admin\AdminAccountController;
use App\Http\Controllers\Admin\GenerateTransactionsController;
use App\Http\Controllers\Admin\AdminDepositController;
use App\Http\Controllers\Admin\AdminWithdrawalController;
use App\Http\Controllers\Admin\AdminPaymentMethodController;
use App\Http\Controllers\Admin\AdminTransactionController;
use App\Http\Controllers\Admin\AdminBannerController;
use App\Http\Controllers\Admin\AdminTransferController;
use App\Http\Controllers\Admin\AdminNotificationController;
use App\Http\Middleware\AdminMiddleware;
use App\Http\Middleware\ActiveAccountMiddleware;
use App\Http\Middleware\OtpMiddleware;
use App\Http\Controllers\ContactController;
use App\Http\Controllers\SecurityController;
use App\Http\Controllers\PlanLearnController;
use App\Http\Controllers\Admin\AdminBrokerageController;
use App\Http\Controllers\Admin\AdminFraudController;

Route::get('/', function () {
    return view('guest.index');
})->name('guest.home');

Route::view('/about', 'guest.about')->name('guest.about');
Route::view('/faq', 'guest.faq')->name('guest.faq');
Route::view('/contact-us', 'guest.contact')->name('guest.contact');
Route::post('/contact-us', [ContactController::class, 'send'])->name('guest.contact.send');

// Public auth
Route::get('/login', [LoginController::class, 'show'])->name('login');
Route::post('/login', [LoginController::class, 'store']);
Route::post('/logout', [LoginController::class, 'destroy'])->name('logout');
Route::get('/register', [RegisterController::class, 'show'])->name('register');
Route::post('/register', [RegisterController::class, 'store']);
Route::get('/pending', [PendingController::class, 'show'])->middleware('auth')->name('pending');
Route::post('/pending/upload', [PendingController::class, 'upload'])->middleware('auth')->name('pending.upload');
Route::get('/otp', [OtpController::class, 'show'])->name('otp');
Route::post('/otp/verify', [OtpController::class, 'verify'])->name('otp.verify');
Route::post('/otp/resend', [OtpController::class, 'resend'])->name('otp.resend');

// Authenticated user
Route::middleware(['auth', ActiveAccountMiddleware::class, OtpMiddleware::class])->group(function () {
    Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
    Route::get('/accounts', [AccountController::class, 'index'])->name('accounts');
    Route::get('/transactions', [TransactionController::class, 'index'])->name('transactions');
    Route::get('/transactions/export', [TransactionController::class, 'export'])->name('transactions.export');
    Route::get('/transactions/{transaction}', [TransactionController::class, 'show'])->name('transactions.show');
    Route::get('/transfer', [TransferController::class, 'show'])->name('transfer');
    Route::post('/transfer/internal', [TransferController::class, 'internal'])->name('transfer.internal');
    Route::post('/transfer/wire-transfer', [TransferController::class, 'wireTransfer'])->name('transfer.wire-transfer');
    Route::post('/transfer/local', [TransferController::class, 'local'])->name('transfer.local');
    Route::get('/transfer/lookup', [TransferController::class, 'lookup'])->name('transfer.lookup');
    Route::get('/deposit', [DepositController::class, 'show'])->name('deposit');
    Route::post('/deposit', [DepositController::class, 'store'])->name('deposit.store');
    Route::get('/withdraw', [WithdrawController::class, 'show'])->name('withdraw');
    Route::post('/withdraw', [WithdrawController::class, 'store'])->name('withdraw.store');
    Route::get('/brokerage', [BrokerageController::class, 'index'])->name('brokerage');
    Route::get('/notifications', [NotificationController::class, 'index'])->name('notifications');
    Route::post('/notifications/mark-all-read', [NotificationController::class, 'markAllRead'])->name('notifications.markAllRead');
    Route::get('/bill-payments', [BillPaymentController::class, 'index'])->name('bill-payments');
    Route::post('/bill-payments', [BillPaymentController::class, 'store'])->name('bill-payments.store');
    Route::get('/profile', [ProfileController::class, 'show'])->name('profile');
    Route::post('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::post('/profile/password', [ProfileController::class, 'updatePassword'])->name('profile.password');
    Route::post('/profile/notifications', [ProfileController::class, 'updateNotifications'])->name('profile.notifications');
    Route::post('/profile/otp-toggle', [ProfileController::class, 'toggleOtp'])->name('profile.otp-toggle');

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

    // Brokerage
    Route::post('/brokerage/request', [BrokerageController::class, 'requestAccount'])->name('brokerage.request');
    Route::get('/brokerage/transfer', [BrokerageController::class, 'transfer'])->name('brokerage.transfer');
    Route::post('/brokerage/transfer', [BrokerageController::class, 'storeTransfer'])->name('brokerage.transfer.store');
    Route::get('/brokerage/{account}', [BrokerageController::class, 'show'])->name('brokerage.show');

    // Security & Support
    Route::get('/security/report-fraud', [SecurityController::class, 'reportFraud'])->name('security.report-fraud');
    Route::post('/security/report-fraud', [SecurityController::class, 'storeReportFraud'])->name('security.report-fraud.store');
    Route::get('/security/track-claims', [SecurityController::class, 'trackClaims'])->name('security.track-claims');
    Route::get('/security/report-phishing', [SecurityController::class, 'reportPhishing'])->name('security.report-phishing');
    Route::post('/security/report-phishing', [SecurityController::class, 'storeReportPhishing'])->name('security.report-phishing.store');
    Route::get('/security/protect-account', [SecurityController::class, 'protectAccount'])->name('security.protect-account');
    Route::post('/security/freeze-account', [SecurityController::class, 'freezeAccount'])->name('security.freeze-account');

    // Plan & Learn
    Route::get('/plan-learn/money-map', [PlanLearnController::class, 'moneyMap'])->name('plan-learn.money-map');
    Route::get('/plan-learn/credit-planning', [PlanLearnController::class, 'creditPlanning'])->name('plan-learn.credit-planning');
    Route::get('/plan-learn/budget-savings', [PlanLearnController::class, 'budgetSavings'])->name('plan-learn.budget-savings');
    Route::post('/plan-learn/budget', [PlanLearnController::class, 'storeBudget'])->name('plan-learn.budget.store');
    Route::delete('/plan-learn/budget/{budget}', [PlanLearnController::class, 'destroyBudget'])->name('plan-learn.budget.destroy');
    Route::post('/plan-learn/savings', [PlanLearnController::class, 'storeSavingsGoal'])->name('plan-learn.savings.store');
    Route::put('/plan-learn/savings/{goal}', [PlanLearnController::class, 'updateSavingsGoal'])->name('plan-learn.savings.update');
    Route::delete('/plan-learn/savings/{goal}', [PlanLearnController::class, 'destroySavingsGoal'])->name('plan-learn.savings.destroy');
});

// Admin
Route::prefix('admin')->middleware(['auth', AdminMiddleware::class])->name('admin.')->group(function () {
    Route::get('/dashboard', [AdminDashboardController::class, 'index'])->name('dashboard');
    Route::get('/settings', [AdminSettingsController::class, 'show'])->name('settings');
    Route::post('/settings', [AdminSettingsController::class, 'update'])->name('settings.update');
    Route::resource('/users', AdminUserController::class);
    Route::get('/users/{user}/accounts', [AdminAccountController::class, 'index'])->name('users.accounts');
    Route::post('/users/{user}/accounts', [AdminAccountController::class, 'store'])->name('users.accounts.store');
    Route::put('/accounts/{account}', [AdminAccountController::class, 'update'])->name('accounts.update');
    Route::post('/accounts/{account}/generate', [GenerateTransactionsController::class, 'store'])->name('accounts.generate');
    Route::get('/deposits', [AdminDepositController::class, 'index'])->name('deposits');
    Route::post('/deposits/{deposit}/approve', [AdminDepositController::class, 'approve'])->name('deposits.approve');
    Route::post('/deposits/{deposit}/reject', [AdminDepositController::class, 'reject'])->name('deposits.reject');
    Route::get('/withdrawals', [AdminWithdrawalController::class, 'index'])->name('withdrawals');
    Route::post('/withdrawals/{withdrawal}/approve', [AdminWithdrawalController::class, 'approve'])->name('withdrawals.approve');
    Route::post('/withdrawals/{withdrawal}/reject', [AdminWithdrawalController::class, 'reject'])->name('withdrawals.reject');
    Route::resource('/payment-methods', AdminPaymentMethodController::class);
    Route::get('/transactions', [AdminTransactionController::class, 'index'])->name('transactions');
    Route::post('/transactions', [AdminTransactionController::class, 'store'])->name('transactions.store');
    Route::get('/transactions/{transaction}/edit', [AdminTransactionController::class, 'edit'])->name('transactions.edit');
    Route::put('/transactions/{transaction}', [AdminTransactionController::class, 'update'])->name('transactions.update');
    Route::get('/transfers', [AdminTransferController::class, 'index'])->name('transfers.index');
    Route::get('/transfers/{transfer}', [AdminTransferController::class, 'show'])->name('transfers.show');
    Route::post('/transfers/{transfer}/approve', [AdminTransferController::class, 'approve'])->name('transfers.approve');
    Route::post('/transfers/{transfer}/reject', [AdminTransferController::class, 'reject'])->name('transfers.reject');
    Route::resource('/banners', AdminBannerController::class)->except(['show']);
    Route::get('/notifications', [AdminNotificationController::class, 'index'])->name('notifications.index');
    Route::get('/notifications/create', [AdminNotificationController::class, 'create'])->name('notifications.create');
    Route::post('/notifications', [AdminNotificationController::class, 'store'])->name('notifications.store');
    Route::get('/notifications/{notification}', [AdminNotificationController::class, 'show'])->name('notifications.show');

    // Brokerage Management
    Route::get('/brokerage', [AdminBrokerageController::class, 'requests'])->name('brokerage.requests');
    Route::post('/brokerage/requests/{brokerageRequest}/approve', [AdminBrokerageController::class, 'approve'])->name('brokerage.requests.approve');
    Route::post('/brokerage/requests/{brokerageRequest}/reject', [AdminBrokerageController::class, 'reject'])->name('brokerage.requests.reject');
    Route::get('/brokerage/accounts', [AdminBrokerageController::class, 'accounts'])->name('brokerage.accounts');
    Route::get('/brokerage/accounts/{account}/holdings', [AdminBrokerageController::class, 'holdings'])->name('brokerage.holdings');
    Route::post('/brokerage/accounts/{account}/holdings', [AdminBrokerageController::class, 'storeHolding'])->name('brokerage.holdings.store');
    Route::put('/brokerage/holdings/{holding}', [AdminBrokerageController::class, 'updateHolding'])->name('brokerage.holdings.update');
    Route::delete('/brokerage/holdings/{holding}', [AdminBrokerageController::class, 'destroyHolding'])->name('brokerage.holdings.destroy');

    // Fraud Reports Management
    Route::get('/fraud-reports', [AdminFraudController::class, 'index'])->name('fraud.index');
    Route::get('/fraud-reports/{report}', [AdminFraudController::class, 'show'])->name('fraud.show');
    Route::put('/fraud-reports/{report}', [AdminFraudController::class, 'update'])->name('fraud.update');
});
