<?php

use App\Http\Controllers\Auth\ForgotPasswordController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\RegisterController;
use App\Http\Controllers\Auth\ResetPasswordController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\HomeController;
use App\Http\Controllers\Client\DepositController as ClientDepositController;
use App\Http\Controllers\Client\HomeController as ClientHomeController;
use App\Http\Controllers\Client\MarketController as ClientMarketController;
use App\Http\Controllers\Client\OrderController as ClientOrderController;
use App\Http\Controllers\Client\ProfileController as ClientProfileController;
use App\Http\Controllers\Client\TradeController as ClientTradeController;
use App\Http\Controllers\Client\TransferController as ClientTransferController;
use App\Http\Controllers\Client\WalletController as ClientWalletController;
use App\Http\Controllers\Client\WithdrawalController as ClientWithdrawalController;
use App\Http\Controllers\Admin\DashboardController as AdminDashboardController;
use App\Http\Controllers\Admin\DepositController;
use App\Http\Controllers\Admin\MarketController;
use App\Http\Controllers\Admin\PromotionController;
use App\Http\Controllers\Admin\SettingController;
use App\Http\Controllers\Admin\SupportController;
use App\Http\Controllers\Admin\TransactionController;
use App\Http\Controllers\Admin\UserController;
use App\Http\Controllers\Admin\WithdrawalController;
use Illuminate\Support\Facades\Route;

Route::get('/', [HomeController::class, 'index'])->name('home');

Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'create'])->name('login');
    Route::post('/login', [LoginController::class, 'store']);
    Route::get('/register', [RegisterController::class, 'create'])->name('register');
    Route::post('/register', [RegisterController::class, 'store']);
    Route::get('/forgot-password', [ForgotPasswordController::class, 'create'])->name('password.request');
    Route::post('/forgot-password', [ForgotPasswordController::class, 'store'])->name('password.email');
    Route::get('/reset-password/{token}', [ResetPasswordController::class, 'create'])->name('password.reset');
    Route::post('/reset-password', [ResetPasswordController::class, 'store'])->name('password.update');
});

Route::middleware('auth')->group(function () {
    Route::post('/logout', [LoginController::class, 'destroy'])->name('logout');

    Route::middleware('client')->prefix('app')->name('client.')->group(function () {
        Route::get('/dashboard', [DashboardController::class, 'index'])->name('dashboard');
        Route::get('/home', [ClientHomeController::class, 'index'])->name('home');
        Route::get('/home/deposit', [ClientHomeController::class, 'deposit'])->name('home.deposit');
        Route::post('/home/deposit', [ClientDepositController::class, 'store'])->name('home.deposit.store');
        Route::get('/home/withdraw', [ClientHomeController::class, 'withdraw'])->name('home.withdraw');
        Route::post('/home/withdraw', [ClientWithdrawalController::class, 'store'])->name('home.withdraw.store');
        Route::get('/home/transfer', [ClientHomeController::class, 'transfer'])->name('home.transfer');
        Route::post('/home/transfer', [ClientTransferController::class, 'store'])->name('home.transfer.store');
        Route::get('/home/upgrade', [ClientHomeController::class, 'upgrade'])->name('home.upgrade');
        Route::get('/home/rewards', [ClientHomeController::class, 'rewards'])->name('home.rewards');
        Route::get('/home/support', [ClientHomeController::class, 'support'])->name('home.support');

        Route::get('/markets', [ClientMarketController::class, 'index'])->name('markets');
        Route::get('/trade', [ClientTradeController::class, 'index'])->name('trade');
        Route::post('/trade', [ClientTradeController::class, 'store'])->name('trade.store');
        Route::post('/trade/settle', [ClientTradeController::class, 'settle'])->name('trade.settle');
        Route::get('/orders', [ClientOrderController::class, 'index'])->name('orders');
        Route::get('/orders/receipt', [ClientOrderController::class, 'receipt'])->name('orders.receipt');
        Route::get('/wallet', [ClientWalletController::class, 'index'])->name('wallet');

        Route::get('/profile', [ClientProfileController::class, 'edit'])->name('profile.edit');
        Route::put('/profile', [ClientProfileController::class, 'update'])->name('profile.update');
        Route::get('/profile/password', [ClientProfileController::class, 'editPassword'])->name('profile.password');
        Route::put('/profile/password', [ClientProfileController::class, 'updatePassword'])->name('profile.password.update');
    });

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

        Route::resource('users', UserController::class)->only(['index', 'show', 'edit', 'update']);
        Route::post('users/{user}/balance/add', [UserController::class, 'addBalance'])->name('users.balance.add');
        Route::post('users/{user}/balance/subtract', [UserController::class, 'subtractBalance'])->name('users.balance.subtract');
        Route::patch('users/{user}/status', [UserController::class, 'toggleStatus'])->name('users.toggle-status');
        Route::resource('markets', MarketController::class)->only(['index', 'store', 'update', 'destroy']);
        Route::post('markets/wave', [MarketController::class, 'applyWave'])->name('markets.wave');

        Route::get('deposits', [DepositController::class, 'index'])->name('deposits.index');
        Route::post('deposits/{deposit}/approve', [DepositController::class, 'approve'])->name('deposits.approve');
        Route::post('deposits/{deposit}/reject', [DepositController::class, 'reject'])->name('deposits.reject');

        Route::get('withdrawals', [WithdrawalController::class, 'index'])->name('withdrawals.index');
        Route::post('withdrawals/{withdrawal}/approve', [WithdrawalController::class, 'approve'])->name('withdrawals.approve');
        Route::post('withdrawals/{withdrawal}/reject', [WithdrawalController::class, 'reject'])->name('withdrawals.reject');

        Route::get('transactions', [TransactionController::class, 'index'])->name('transactions.index');
        Route::get('transactions/export/excel', [TransactionController::class, 'exportExcel'])->name('transactions.export.excel');
        Route::get('transactions/export/pdf', [TransactionController::class, 'exportPdf'])->name('transactions.export.pdf');

        Route::get('promotions', [PromotionController::class, 'index'])->name('promotions.index');
        Route::post('promotions', [PromotionController::class, 'store'])->name('promotions.store');
        Route::post('promotions/{promotion}/resend', [PromotionController::class, 'resend'])->name('promotions.resend');

        Route::get('support', [SupportController::class, 'index'])->name('support.index');
        Route::post('support/start', [SupportController::class, 'start'])->name('support.start');
        Route::post('support/{conversation}/message', [SupportController::class, 'message'])->name('support.message');
        Route::post('support/{conversation}/favorite', [SupportController::class, 'toggleFavorite'])->name('support.favorite');

        Route::get('settings', [SettingController::class, 'edit'])->name('settings.edit');
        Route::put('settings', [SettingController::class, 'update'])->name('settings.update');
        Route::put('settings/trading-mode', [SettingController::class, 'updateTradingMode'])->name('settings.trading-mode');
        Route::put('settings/daily-bonus', [SettingController::class, 'updateDailyBonus'])->name('settings.daily-bonus');
    });
});
