<?php

use App\Http\Controllers\Admin\AuditLogController;
use App\Http\Controllers\Admin\BanController;
use App\Http\Controllers\Admin\CharacterManagerController;
use App\Http\Controllers\Admin\DiscordLogController;
use App\Http\Controllers\Admin\EconomyController;
use App\Http\Controllers\Admin\ItemManagerController;
use App\Http\Controllers\Admin\JobManagerController;
use App\Http\Controllers\Admin\NewsController as AdminNewsController;
use App\Http\Controllers\Admin\OnlinePlayersController;
use App\Http\Controllers\Admin\PlayerManagerController;
use App\Http\Controllers\Admin\RouletteAdminController;
use App\Http\Controllers\Admin\ServerLogController;
use App\Http\Controllers\Admin\ServerStatusController;
use App\Http\Controllers\Admin\SettingsController;
use App\Http\Controllers\Admin\VehicleManagerController;
use App\Http\Controllers\Admin\WarnController;
use App\Http\Controllers\Admin\WhitelistController;
use App\Http\Controllers\Auth\DiscordAuthController;
use App\Http\Controllers\Auth\LoginController;
use App\Http\Controllers\Auth\TwoFactorController;
use App\Http\Controllers\DashboardController;
use App\Http\Controllers\Player\AchievementController;
use App\Http\Controllers\Player\BankController;
use App\Http\Controllers\Player\CharacterController;
use App\Http\Controllers\Player\GangController;
use App\Http\Controllers\Player\HouseController;
use App\Http\Controllers\Player\InventoryController;
use App\Http\Controllers\Player\JobController;
use App\Http\Controllers\Player\LeaderboardController;
use App\Http\Controllers\Player\NewsController;
use App\Http\Controllers\Player\ProfileController;
use App\Http\Controllers\Player\RouletteController;
use App\Http\Controllers\Player\StatsController;
use App\Http\Controllers\Player\VehicleController;
use App\Http\Controllers\Player\WhitelistApplicationController;
use Illuminate\Support\Facades\Route;
use Inertia\Inertia;

/*
|--------------------------------------------------------------------------
| Public site
|--------------------------------------------------------------------------
*/

Route::get('/', fn () => Inertia::render('Public/Home'))->name('home');
Route::get('/news', [NewsController::class, 'index'])->name('news.index');
Route::get('/news/{news}', [NewsController::class, 'show'])->name('news.show');

/*
|--------------------------------------------------------------------------
| Auth: Discord OAuth (primary), local login (fallback), 2FA challenge
|--------------------------------------------------------------------------
*/

Route::middleware('guest')->group(function () {
    Route::get('/login', [LoginController::class, 'create'])->name('login');
    Route::post('/login', [LoginController::class, 'store']);
    Route::get('/auth/discord/redirect', [DiscordAuthController::class, 'redirect'])->name('discord.redirect');
    Route::get('/auth/discord/callback', [DiscordAuthController::class, 'callback'])->name('discord.callback');
});

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

    Route::get('/two-factor-challenge', [TwoFactorController::class, 'create'])->name('two-factor.challenge');
    Route::post('/two-factor-challenge', [TwoFactorController::class, 'store']);
});

/*
|--------------------------------------------------------------------------
| Player UCP (requires auth + confirmed 2FA when enabled on the account)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'two_factor'])->group(function () {
    Route::get('/dashboard', DashboardController::class)->name('dashboard');

    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::post('/profile/link-code', [ProfileController::class, 'generateLinkCode'])->name('profile.link-code');
    Route::get('/profile/two-factor', [TwoFactorController::class, 'setup'])->name('two-factor.setup');
    Route::post('/profile/two-factor/confirm', [TwoFactorController::class, 'confirm'])->name('two-factor.confirm');
    Route::delete('/profile/two-factor', [TwoFactorController::class, 'disable'])->name('two-factor.disable');

    Route::get('/characters', [CharacterController::class, 'index'])->name('characters.index');
    Route::get('/characters/{citizenId}', [CharacterController::class, 'show'])->name('characters.show');

    Route::get('/characters/{citizenId}/inventory', [InventoryController::class, 'show'])->name('inventory.show');
    Route::get('/characters/{citizenId}/vehicles', [VehicleController::class, 'index'])->name('vehicles.index');
    Route::get('/characters/{citizenId}/houses', [HouseController::class, 'index'])->name('houses.index');
    Route::get('/characters/{citizenId}/bank', [BankController::class, 'show'])->name('bank.show');
    Route::get('/characters/{citizenId}/job', [JobController::class, 'show'])->name('job.show');
    Route::get('/characters/{citizenId}/gang', [GangController::class, 'show'])->name('gang.show');
    Route::get('/characters/{citizenId}/stats', [StatsController::class, 'show'])->name('stats.show');

    Route::get('/leaderboard', [LeaderboardController::class, 'index'])->name('leaderboard.index');
    Route::get('/achievements', [AchievementController::class, 'index'])->name('achievements.index');

    Route::get('/roulette', [RouletteController::class, 'index'])->name('roulette.index');
    Route::post('/roulette/spin', [RouletteController::class, 'spin'])->name('roulette.spin');

    Route::get('/whitelist/apply', [WhitelistApplicationController::class, 'create'])->name('whitelist.create');
    Route::post('/whitelist/apply', [WhitelistApplicationController::class, 'store'])->name('whitelist.store');
});

/*
|--------------------------------------------------------------------------
| Admin panel (requires the "access-admin-panel" permission, plus 2FA)
|--------------------------------------------------------------------------
*/

Route::middleware(['auth', 'two_factor', 'permission:access-admin-panel'])
    ->prefix('admin')
    ->name('admin.')
    ->group(function () {
        Route::get('/', ServerStatusController::class)->name('dashboard');

        Route::middleware('permission:players.manage')->group(function () {
            Route::get('/players', [PlayerManagerController::class, 'index'])->name('players.index');
            Route::get('/players/{citizenId}', [PlayerManagerController::class, 'show'])->name('players.show');
            Route::patch('/players/{citizenId}', [PlayerManagerController::class, 'update'])->name('players.update');
            Route::get('/characters', [CharacterManagerController::class, 'index'])->name('characters.index');
        });

        Route::middleware('permission:bans.manage')->group(function () {
            Route::get('/bans', [BanController::class, 'index'])->name('bans.index');
            Route::post('/bans', [BanController::class, 'store'])->name('bans.store');
            Route::delete('/bans/{ban}', [BanController::class, 'destroy'])->name('bans.destroy');
        });

        Route::middleware('permission:warnings.manage')->group(function () {
            Route::get('/warnings', [WarnController::class, 'index'])->name('warnings.index');
            Route::post('/warnings', [WarnController::class, 'store'])->name('warnings.store');
            Route::delete('/warnings/{warning}', [WarnController::class, 'destroy'])->name('warnings.destroy');
        });

        Route::middleware('permission:whitelist.manage')->group(function () {
            Route::get('/whitelist', [WhitelistController::class, 'index'])->name('whitelist.index');
            Route::patch('/whitelist/{application}', [WhitelistController::class, 'update'])->name('whitelist.update');
        });

        Route::middleware('permission:jobs.manage')->group(function () {
            Route::get('/jobs', [JobManagerController::class, 'index'])->name('jobs.index');
            Route::post('/jobs', [JobManagerController::class, 'store'])->name('jobs.store');
            Route::patch('/jobs/{job}', [JobManagerController::class, 'update'])->name('jobs.update');
        });

        Route::middleware('permission:vehicles.manage')->group(function () {
            Route::get('/vehicles', [VehicleManagerController::class, 'index'])->name('vehicles.index');
            Route::patch('/vehicles/{vehicle}', [VehicleManagerController::class, 'update'])->name('vehicles.update');
            Route::delete('/vehicles/{vehicle}', [VehicleManagerController::class, 'destroy'])->name('vehicles.destroy');
        });

        Route::middleware('permission:items.manage')->group(function () {
            Route::get('/items', [ItemManagerController::class, 'index'])->name('items.index');
            Route::post('/items/grant', [ItemManagerController::class, 'grant'])->name('items.grant');
        });

        Route::middleware('permission:economy.manage')->group(function () {
            Route::get('/economy', [EconomyController::class, 'index'])->name('economy.index');
            Route::post('/economy/adjust', [EconomyController::class, 'adjust'])->name('economy.adjust');
        });

        Route::middleware('permission:news.manage')->group(function () {
            Route::get('/news', [AdminNewsController::class, 'index'])->name('news.index');
            Route::post('/news', [AdminNewsController::class, 'store'])->name('news.store');
            Route::patch('/news/{news}', [AdminNewsController::class, 'update'])->name('news.update');
            Route::delete('/news/{news}', [AdminNewsController::class, 'destroy'])->name('news.destroy');
        });

        Route::middleware('permission:roulette.manage')->group(function () {
            Route::get('/roulette', [RouletteAdminController::class, 'index'])->name('roulette.index');
            Route::post('/roulette/categories', [RouletteAdminController::class, 'storeCategory'])->name('roulette.categories.store');
            Route::delete('/roulette/categories/{category}', [RouletteAdminController::class, 'destroyCategory'])->name('roulette.categories.destroy');
            Route::post('/roulette/items', [RouletteAdminController::class, 'storeItem'])->name('roulette.items.store');
            Route::patch('/roulette/items/{item}', [RouletteAdminController::class, 'updateItem'])->name('roulette.items.update');
            Route::delete('/roulette/items/{item}', [RouletteAdminController::class, 'destroyItem'])->name('roulette.items.destroy');
        });

        Route::middleware('permission:settings.manage')->group(function () {
            Route::get('/settings', [SettingsController::class, 'edit'])->name('settings.edit');
            Route::patch('/settings', [SettingsController::class, 'update'])->name('settings.update');
        });

        Route::middleware('permission:logs.view')->group(function () {
            Route::get('/logs/server', [ServerLogController::class, 'index'])->name('logs.server');
            Route::get('/logs/discord', [DiscordLogController::class, 'index'])->name('logs.discord');
            Route::get('/logs/audit', [AuditLogController::class, 'index'])->name('logs.audit');
        });

        Route::get('/online-players', OnlinePlayersController::class)->name('online-players');
    });
