<?php

use App\Http\Controllers\User\EarnController;
use App\Http\Controllers\User\FaceVerificationController;
use App\Http\Controllers\User\HistoryController;
use App\Http\Controllers\User\HomeController;
use App\Http\Controllers\User\LeaderboardController;
use App\Http\Controllers\User\LiveLeadsController;
use App\Http\Controllers\User\ReferralController;
use App\Http\Controllers\User\SettingsController;
use App\Http\Controllers\User\ShopController;
use App\Http\Controllers\User\SupportTicketController;
use App\Http\Controllers\User\TransactionsController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Auth::routes(['verify' => true]);

// Landing routes
Route::view('/', 'landing.home')->name('home');
Route::get('/offers', [\App\Http\Controllers\PublicOffersController::class, 'index'])->name('offers.preview');
Route::view('/faq', 'landing.faq')->name('faq');
Route::view('/privacy-policy', 'landing.privacy-policy')->name('privacy-policy');
Route::view('/refund-policy', 'landing.refund-policy')->name('refund-policy');
Route::view('/term-condition', 'landing.term-condition')->name('term-condition');
Route::view('/ban', 'landing.ban')->name('ban');
Route::view('/suspicious', 'landing.suspicious')->name('suspicious');
Route::get('/ref/{code}', \App\Http\Controllers\ReferralRedirectController::class)->name('referral.redirect');

// Two-factor login challenge (user is logged out until the code is verified).
Route::middleware('throttle:10,1')->group(function () {
    Route::get('/two-factor/challenge', [\App\Http\Controllers\Auth\TwoFactorController::class, 'show'])->name('two-factor.challenge');
    Route::post('/two-factor/challenge', [\App\Http\Controllers\Auth\TwoFactorController::class, 'verify'])->name('two-factor.verify');
    Route::post('/two-factor/resend', [\App\Http\Controllers\Auth\TwoFactorController::class, 'resend'])->name('two-factor.resend');
});

// User routes
Route::middleware(['auth', 'not_banned', 'not_changed_ip'])->group(function () {
    Route::controller(HomeController::class)->group(function () {
        Route::get('/dashboard', 'index')->name('user.home');
        // Throttled so bonus codes can't be brute-forced.
        Route::post('/dashboard', 'redeem')->name('user.home.redeem')->middleware('throttle:10,1');
    });

    Route::get('/notifications/feed', [\App\Http\Controllers\NotificationsController::class, 'feed'])->name('notifications.feed');
    Route::post('/notifications/read', [\App\Http\Controllers\NotificationsController::class, 'markRead'])->name('notifications.read');

    Route::controller(ShopController::class)->group(function () {
        Route::get('/shop', 'index')->name('user.shop');
        Route::get('/shop/{id}', 'view')->name('user.shop.view');
        Route::post('/shop/{id}', 'checkout')->name('user.shop.checkout')->middleware('throttle:6,1');
    });

    Route::controller(SettingsController::class)->group(function () {
        Route::get('/settings', 'index')->name('user.settings');
        Route::post('/settings', 'update')->name('user.settings.update')->middleware('throttle:10,1');
    });
    Route::post('/settings/face-verification', [FaceVerificationController::class, 'store'])->name('user.face-verification.store')->middleware('throttle:6,1');

    // Two-factor setup (while logged in).
    Route::post('/settings/2fa/enable-email', [SettingsController::class, 'enableEmailTwoFactor'])->name('user.2fa.email')->middleware('throttle:10,1');
    Route::post('/settings/2fa/setup-totp', [SettingsController::class, 'setupTotp'])->name('user.2fa.totp.setup')->middleware('throttle:10,1');
    Route::post('/settings/2fa/confirm-totp', [SettingsController::class, 'confirmTotp'])->name('user.2fa.totp.confirm')->middleware('throttle:10,1');
    Route::post('/settings/2fa/disable', [SettingsController::class, 'disableTwoFactor'])->name('user.2fa.disable')->middleware('throttle:10,1');
    Route::get('/face-verifications/{faceVerification}/image', [FaceVerificationController::class, 'image'])->name('face-verifications.image');

    Route::get('/earn', [EarnController::class, 'index'])->name('user.earn')->middleware('proxy');
    Route::get('/earn/offers', [EarnController::class, 'offers'])->name('user.earn.offers');
    Route::get('/earn/offers/state', [EarnController::class, 'state'])->name('user.earn.state')->middleware('throttle:30,1');
    Route::post('/earn/offers/refresh', [EarnController::class, 'refresh'])->name('user.earn.refresh')->middleware('throttle:6,1');
    Route::post('/earn/offerwalls/{offerwall}/open', [EarnController::class, 'open'])->name('user.offerwalls.open');
    Route::post('/earn/campaigns/{campaign}/open', [EarnController::class, 'openCampaign'])->name('user.campaigns.open');
    Route::post('/earn/offers/{providerOffer}/open', [EarnController::class, 'openProviderOffer'])->name('user.provider-offers.open');
    Route::get('/leaderboard', [LeaderboardController::class, 'index'])->name('user.leaderboard');
    Route::get('/leaderboard/profile/{user}', [LeaderboardController::class, 'profile'])
        ->name('user.leaderboard.profile')
        ->middleware('throttle:30,1');
    Route::get('/referrals', [ReferralController::class, 'index'])->name('user.referrals');
    Route::get('/transactions', [TransactionsController::class, 'index'])->name('user.transactions');
    Route::get('/live-leads', [LiveLeadsController::class, 'index'])->name('user.live-leads');
    Route::get('/history', [HistoryController::class, 'index'])->name('user.history');
    Route::get('/support', [SupportTicketController::class, 'index'])->name('user.tickets');
    Route::post('/support/bot', [\App\Http\Controllers\User\SupportBotController::class, 'chat'])->name('user.support.bot')->middleware('throttle:15,1');
    Route::post('/support', [SupportTicketController::class, 'store'])->name('user.tickets.store')->middleware('throttle:10,1');
    Route::get('/support/{ticket}', [SupportTicketController::class, 'show'])->name('user.tickets.show');
    Route::post('/support/{ticket}', [SupportTicketController::class, 'reply'])->name('user.tickets.reply')->middleware('throttle:20,1');
    Route::view('/proxy', 'user.proxy')->name('user.proxy');
});
