<?php

use App\Http\Controllers\AppController;
use App\Http\Controllers\CronJobController;
use App\Http\Controllers\Frontend\DashboardController;
use App\Http\Controllers\Frontend\DepositController;
use App\Http\Controllers\Frontend\GatewayController;
use App\Http\Controllers\Frontend\HomeController;
use App\Http\Controllers\Frontend\InvestController;
use App\Http\Controllers\Frontend\IpnController;
use App\Http\Controllers\Frontend\KycController;
use App\Http\Controllers\Frontend\PageController;
use App\Http\Controllers\Frontend\ReferralController;
use App\Http\Controllers\Frontend\SchemaController;
use App\Http\Controllers\Frontend\SendMoneyController;
use App\Http\Controllers\Frontend\SettingController;
use App\Http\Controllers\Frontend\StatusController;
use App\Http\Controllers\Frontend\TicketController;
use App\Http\Controllers\Frontend\TransactionController;
use App\Http\Controllers\Frontend\UserController;
use App\Http\Controllers\Frontend\WithdrawController;
use Illuminate\Support\Facades\Route;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Session;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| This file defines all web routes for your Laravel application.
| The "front.gate" middleware ensures that only users who pass the
| front-login access check can access the main frontend.
|
*/

// 🔒 Protect all main site pages with front.gate middleware
Route::middleware(['front.gate'])->group(function () {

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

    // Static Pages
    Route::get('/{page}', PageController::class)->name('page')->where('page', 'schema|how-it-works|about-us|faq|rankings|blog|contact|privacy-policy|terms-and-conditions');

    // Dynamic Pages
    Route::get('page/{section}', [PageController::class, 'getPage'])->name('dynamic.page');

    Route::get('blog/{id}', [PageController::class, 'blogDetails'])->name('blog-details');
    Route::post('mail-send', [PageController::class, 'mailSend'])->name('mail-send');

    // --------------------------
    // Authenticated User Section
    // --------------------------
    Route::group([
        'middleware' => [
            'auth', 
            '2fa', 
            'isActive', 
            setting('email_verification', 'permission') ? 'verified' : 'web'
        ],
        'prefix' => 'user',
        'as' => 'user.'
    ], function () {

        Route::get('dashboard', [DashboardController::class, 'dashboard'])->name('dashboard');
        Route::get('notify', [UserController::class, 'notifyUser'])->name('notify');
        Route::get('notification/all', [UserController::class, 'allNotification'])->name('notification.all');
        Route::get('latest-notification', [UserController::class, 'latestNotification'])->name('latest-notification');
        Route::get('notification-read/{id}', [UserController::class, 'readNotification'])->name('read-notification');

        Route::get('/change-password', [UserController::class, 'changePassword'])->name('change.password');
        Route::post('/password-store', [UserController::class, 'newPassword'])->name('new.password');

        Route::get('kyc', [KycController::class, 'kyc'])->name('kyc');
        Route::get('kyc/{id}', [KycController::class, 'kycData'])->name('kyc.data');
        Route::post('kyc-submit', [KycController::class, 'submit'])->name('kyc.submit');

        Route::get('schemas', [SchemaController::class, 'index'])->name('schema');
        Route::get('schema-preview/{id}', [SchemaController::class, 'schemaPreview'])->name('schema.preview');

        Route::post('invest-now', [InvestController::class, 'investNow'])->name('invest-now');
        Route::get('invest-logs', [InvestController::class, 'investLogs'])->name('invest-logs');
        Route::get('invest-cancel/{id}', [InvestController::class, 'investCancel'])->name('invest-cancel');
        Route::get('transactions', [TransactionController::class, 'transactions'])->name('transactions');

        Route::prefix('deposit')->name('deposit.')->group(function () {
            Route::get('', [DepositController::class, 'deposit'])->name('amount');
            Route::get('gateway/{code}', [GatewayController::class, 'gateway'])->name('gateway');
            Route::post('now', [DepositController::class, 'depositNow'])->name('now');
            Route::get('log', [DepositController::class, 'depositLog'])->name('log');
        });

        Route::prefix('send-money')->name('send-money.')->controller(SendMoneyController::class)->group(function () {
            Route::get('/', 'sendMoney')->name('view');
            Route::post('now', 'sendMoneyNow')->name('now');
            Route::get('log', 'sendMoneyLog')->name('log');
        });

        Route::get('wallet-exchange', [UserController::class, 'walletExchange'])->name('wallet-exchange');
        Route::post('wallet-exchange-now', [UserController::class, 'walletExchangeNow'])->name('wallet-exchange-now');

        Route::middleware('KYC')->prefix('withdraw')->name('withdraw.')->controller(WithdrawController::class)->group(function () {
            Route::resource('account', WithdrawController::class)->except('show');
            Route::get('/', 'withdraw')->name('view');
            Route::get('details/{accountId}/{amount?}', 'details')->name('details');
            Route::get('method/{id}', 'withdrawMethod')->name('method');
            Route::post('now', 'withdrawNow')->name('now');
            Route::get('log', 'withdrawLog')->name('log');
        });

        Route::get('exist/{email}', [UserController::class, 'userExist'])->name('exist');

        Route::prefix('support-ticket')->name('ticket.')->controller(TicketController::class)->group(function () {
            Route::get('index', 'index')->name('index');
            Route::get('new', 'new')->name('new');
            Route::post('new-store', 'store')->name('new-store');
            Route::post('reply', 'reply')->name('reply');
            Route::get('show/{uuid}', 'show')->name('show');
            Route::get('close-now/{uuid}', 'closeNow')->name('close.now');
        });

        Route::get('referral', [ReferralController::class, 'referral'])->name('referral');
        Route::get('ranking-badge', [UserController::class, 'rankingBadge'])->name('ranking-badge');

        Route::prefix('settings')->name('setting.')->controller(SettingController::class)->group(function () {
            Route::get('/', 'settings')->name('show');
            Route::get('2fa', 'twoFa')->name('2fa');
            Route::post('action-2fa', 'actionTwoFa')->name('action-2fa');
            Route::post('profile-update', 'profileUpdate')->name('profile-update');

            Route::post('/2fa/verify', function () {
                return redirect(route('user.dashboard'));
            })->name('2fa.verify');
        });
    });

    Route::get('language-update', [HomeController::class, 'languageUpdate'])->name('language-update');
    Route::get('gateway-list', [GatewayController::class, 'gatewayList'])->name('gateway.list')->middleware('XSS','translate','auth');

    Route::prefix('status')->name('status.')->controller(StatusController::class)->group(function () {
        Route::match(['get', 'post'], '/success', 'success')->name('success');
        Route::match(['get', 'post'], '/cancel', 'cancel')->name('cancel');
        Route::match(['get', 'post'], '/pending', 'pending')->name('pending');
    });

    Route::prefix('ipn')->name('ipn.')->controller(IpnController::class)->group(function () {
        Route::post('coinpayments', 'coinpaymentsIpn')->name('coinpayments');
        Route::post('nowpayments', 'nowpaymentsIpn')->name('nowpayments');
        Route::post('cryptomus', 'cryptomusIpn')->name('cryptomus');
        Route::get('paypal', 'paypalIpn')->name('paypal');
        Route::post('mollie', 'mollieIpn')->name('mollie');
        Route::any('perfectmoney', 'perfectMoneyIpn')->name('perfectMoney');
        Route::get('paystack', 'paystackIpn')->name('paystack');
        Route::get('flutterwave', 'flutterwaveIpn')->name('flutterwave');
        Route::post('coingate', 'coingateIpn')->name('coingate');
        Route::get('monnify', 'monnifyIpn')->name('monnify');
        Route::get('non-hosted-securionpay', 'nonHostedSecurionpayIpn')->name('non-hosted.securionpay')->middleware(['auth', 'XSS']);
        Route::post('coinremitter', 'coinremitterIpn')->name('coinremitter');
        Route::post('btcpay', 'btcpayIpn')->name('btcpay');
    });

    Route::get('theme-mode', [HomeController::class, 'themeMode'])->name('mode-theme');
    Route::get('schema-select/{id}', [SchemaController::class, 'schemaSelect'])->name('user.schema.select');
    Route::get('notification-tune', [AppController::class, 'notificationTune'])->name('notification-tune');
    Route::get('cron-job/investment', [CronJobController::class, 'investmentCronJob'])->name('cron-job.investment');
    Route::get('cron-job/referral', [CronJobController::class, 'referralCronJob'])->name('cron-job.referral');
    Route::get('cron-job/user-ranking', [CronJobController::class, 'userRanking'])->name('cron-job.user-ranking');
});

// 🚪 Public routes (not protected by front.gate)
Route::get('/front-login', function () {
    return view('front-login');
});

Route::post('/front-login/check', function (Request $request) {
    $password = env('FRONT_ACCESS_PASSWORD', 'letmein');
    if ($request->input('password') === $password) {
        Session::put('front_authenticated', true);
        return redirect('/');
    }
    return back()->withErrors(['password' => 'Incorrect password. Please try again.']);
});
