<?php

use App\Http\Controllers\DashboardController;
use App\Http\Controllers\EventController;
use App\Http\Controllers\EventRegistrationController;
use App\Http\Controllers\GameController;
use App\Http\Controllers\MyEventsController;
use App\Http\Controllers\PaymentProofController;
use App\Http\Controllers\PlayerGameProfileController;
use App\Http\Controllers\PlayerRecordController;
use App\Http\Controllers\PlayerVideoController;
use App\Http\Controllers\Preview\PreviewController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\SettingsController;
use Illuminate\Support\Facades\Route;

// ----------------------------------------------------------------
// Production Routes
// ----------------------------------------------------------------
Route::get('/', function () {
    if (auth()->check()) {
        return redirect()->route('dashboard');
    }

    return redirect()->route('preview.dashboard');
})->name('home');

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

Route::get('/games', [GameController::class, 'index'])->name('games.index');
Route::get('/games/{game:slug}', [GameController::class, 'show'])->name('games.show');

Route::get('/events', [EventController::class, 'index'])->name('events.index');
Route::get('/events/{event:slug}', [EventController::class, 'show'])->name('events.show');

// ----------------------------------------------------------------
// Preview Routes — Safe, no DB required
// ----------------------------------------------------------------
Route::prefix('preview')->name('preview.')->group(function () {
    Route::get('/dashboard',              [PreviewController::class, 'dashboard'])->name('dashboard');
    Route::get('/discover',               [PreviewController::class, 'discover'])->name('discover');

    // Players
    Route::get('/players',                [PreviewController::class, 'players'])->name('players');
    Route::get('/players/{slug}',         [PreviewController::class, 'playerProfile'])->name('player');

    // Teams
    Route::get('/teams',                  [PreviewController::class, 'teams'])->name('teams');
    Route::get('/teams/{slug}',           [PreviewController::class, 'teamProfile'])->name('team');

    // Games
    Route::get('/games',                  [PreviewController::class, 'games'])->name('games');
    Route::get('/games/{slug}',           [PreviewController::class, 'gameDetail'])->name('game');

    // Tournaments
    Route::get('/tournaments',            [PreviewController::class, 'tournaments'])->name('tournaments');
    Route::get('/tournaments/{slug}',     [PreviewController::class, 'tournamentDetail'])->name('tournament');

    // Rankings
    Route::get('/rankings',               [PreviewController::class, 'rankings'])->name('rankings');

    // Recruitment
    Route::get('/recruitment',            [PreviewController::class, 'recruitment'])->name('recruitment');
    Route::get('/recruitment/{id}',       [PreviewController::class, 'recruitmentDetail'])->name('recruitment.detail');

    // Transfer Market
    Route::get('/transfer-market',        [PreviewController::class, 'transferMarket'])->name('transfer-market');

    // Scrims
    Route::get('/scrims',                 [PreviewController::class, 'scrims'])->name('scrims');
    Route::get('/scrims/{id}',            [PreviewController::class, 'scrimDetail'])->name('scrim');

    // Matches
    Route::get('/matches',            [PreviewController::class, 'matches'])->name('matches');
    Route::get('/matches/{id}',           [PreviewController::class, 'matchDetail'])->name('match');

    // Community
    Route::get('/community',              [PreviewController::class, 'community'])->name('community');

    // Chat
    Route::get('/chat',                   [PreviewController::class, 'chat'])->name('chat');

    // Profile
    Route::get('/profile/{slug}',         [PreviewController::class, 'profile'])->name('profile');
    Route::get('/profile/{slug}/edit',    [PreviewController::class, 'profileEdit'])->name('profile.edit');

    // Verification
    Route::get('/verification',           [PreviewController::class, 'verification'])->name('verification');

    // Subscriptions
    Route::get('/subscriptions',          [PreviewController::class, 'subscriptions'])->name('subscriptions');

    // Settings
    Route::get('/settings',               [PreviewController::class, 'settings'])->name('settings');
    Route::get('/settings/{tab}',         [PreviewController::class, 'settings'])->name('settings.tab');
});

// Error page previews
Route::get('/errors/403', fn() => response(view('pages.errors.403'), 403));
Route::get('/errors/404', fn() => response(view('pages.errors.404'), 404));
Route::get('/errors/500', fn() => response(view('pages.errors.500'), 500));
Route::get('/errors/503', fn() => response(view('pages.errors.503'), 503));

// ----------------------------------------------------------------
// Public profile
// ----------------------------------------------------------------
Route::get('/players/{playerProfile}', [ProfileController::class, 'show'])
    ->name('profile.show');

// ----------------------------------------------------------------
// Authenticated app routes
// ----------------------------------------------------------------
Route::middleware(['auth', 'verified', 'active'])->group(function () {
    Route::get('/settings', [SettingsController::class, 'index'])->name('settings');
    Route::get('/settings/{tab}', [SettingsController::class, 'index'])
        ->name('settings.tab')
        ->where('tab', 'account|security|notifications');

    Route::post('/settings/profile', [SettingsController::class, 'updateProfile'])->name('settings.profile.update');
    Route::post('/settings/profile/image', [SettingsController::class, 'updateProfileImage'])->name('settings.profile.image.update');
    Route::post('/settings/password', [SettingsController::class, 'updatePassword'])->name('settings.password.update');
    Route::post('/settings/phone', [SettingsController::class, 'updatePhone'])->name('settings.phone.update');

    Route::get('/profile/edit', [ProfileController::class, 'edit'])->name('profile.edit');

    Route::prefix('profile/games')->name('profile.games.')->group(function () {
        Route::get('/', [PlayerGameProfileController::class, 'index'])->name('index');
        Route::get('/create', [PlayerGameProfileController::class, 'create'])->name('create');
        Route::post('/', [PlayerGameProfileController::class, 'store'])->name('store');
        Route::get('/{playerGameProfile}', [PlayerGameProfileController::class, 'show'])->name('show');
        Route::get('/{playerGameProfile}/edit', [PlayerGameProfileController::class, 'edit'])->name('edit');
        Route::put('/{playerGameProfile}', [PlayerGameProfileController::class, 'update'])->name('update');
        Route::delete('/{playerGameProfile}', [PlayerGameProfileController::class, 'destroy'])->name('destroy');
    });

    Route::get('/profile/records', [PlayerRecordController::class, 'index'])->name('profile.records.index');
    Route::post('/profile/records', [PlayerRecordController::class, 'store'])->name('profile.records.store');
    Route::delete('/profile/records/{record}', [PlayerRecordController::class, 'destroy'])->name('profile.records.destroy');

    Route::get('/profile/videos', [PlayerVideoController::class, 'index'])->name('profile.videos.index');
    Route::post('/profile/videos', [PlayerVideoController::class, 'store'])->name('profile.videos.store');
    Route::delete('/profile/videos/{video}', [PlayerVideoController::class, 'destroy'])->name('profile.videos.destroy');

    Route::get('/my-events', [MyEventsController::class, 'index'])->name('my-events.index');

    Route::get('/events/{event:slug}/register', [EventRegistrationController::class, 'create'])->name('events.register.create');
    Route::post('/events/{event:slug}/register', [EventRegistrationController::class, 'store'])->name('events.register.store');

    Route::get('/event-registrations/{registration}', [EventRegistrationController::class, 'show'])->name('registrations.show');
    Route::delete('/event-registrations/{registration}', [EventRegistrationController::class, 'cancel'])->name('registrations.cancel');

    Route::get('/event-registrations/{registration}/payment-proof', [PaymentProofController::class, 'create'])->name('registrations.payment-proof.create');
    Route::post('/event-registrations/{registration}/payment-proof', [PaymentProofController::class, 'store'])->name('registrations.payment-proof.store');

    Route::get('/payment-proofs/{proof}', [PaymentProofController::class, 'show'])->name('payment-proofs.show');

    Route::get('notifications', [\App\Http\Controllers\NotificationController::class, 'index'])->name('notifications.index');
    Route::post('notifications/read-all', [\App\Http\Controllers\NotificationController::class, 'markAllRead'])->name('notifications.read-all');
    Route::get('notifications/{id}/read', [\App\Http\Controllers\NotificationController::class, 'markRead'])->name('notifications.read');
});
