<?php

use App\Http\Controllers\AuthController;
use App\Http\Controllers\GameController;
use App\Http\Controllers\MatchController;
use App\Http\Controllers\ProfileController;
use App\Http\Controllers\StatisticsController;
use App\Http\Controllers\UserController;
use App\Http\Controllers\CoinPurchaseController;
use Illuminate\Support\Facades\Route;

Route::prefix('v1')->group(function () {
    // Public Routes
    Route::post('/login', [AuthController::class, 'login']);
    Route::post('/register', [AuthController::class, 'register']);

    Route::get('/statistics/public', [StatisticsController::class, 'getPublicStats']);
    Route::get('/leaderboard', [StatisticsController::class, 'getLeaderboard']);

    // Authenticated Routes
    Route::middleware(['auth:sanctum', 'throttle:60,1'])->group(function () {
        Route::post('/logout', [AuthController::class, 'logout']);
        Route::post('/coin-purchases', [CoinPurchaseController::class, 'store']);
        Route::get('/statistics/me', [StatisticsController::class, 'getPersonalStats']);
        Route::prefix('users/me')->group(function () {
            Route::get('/', [ProfileController::class, 'show']);
            Route::put('/', [ProfileController::class, 'update']);
            Route::patch('/password', [ProfileController::class, 'updatePassword']);
            Route::delete('/', [ProfileController::class, 'destroy']);
            Route::get('/coins', [ProfileController::class, 'getCoins']);
            Route::put('/password', [
                ProfileController::class,
                'updatePassword',
            ]);
            Route::post('/avatar', [ProfileController::class, 'uploadAvatar']);
            Route::get('/transactions', [UserController::class, 'getMyTransactions']);
        });

        // User Resources
        Route::prefix('/users')->group(function () {
            Route::get('/{user}', [UserController::class, 'show']);
            Route::get('/{user}/matches', [
                UserController::class,
                'getMatches',
            ]);
            Route::get('/{user}/transactions', [UserController::class, 'getTransactions']);
        });

        // Game Resources
        Route::prefix('games')->group(function () {
            Route::apiResource('/', GameController::class)->parameters([
                '' => 'game',
            ]);
            Route::post('/{game}/join', [GameController::class, 'join']);
            Route::post('/{game}/resign', [GameController::class, 'resign']);
        });

        // Match Resources
        Route::prefix('matches')->group(function () {
            Route::apiResource('/', MatchController::class)->parameters([
                '' => 'match',
            ]);
            Route::post('/{match}/join', [
                MatchController::class,
                'join',
            ]);
        });

        // Admin Routes
        Route::middleware('user.type:A')
            ->prefix('admin')
            ->group(function () {
                Route::get('/statistics', [StatisticsController::class, 'getAdminStats']);
                Route::prefix('users')->group(function () {
                    Route::get('/', [UserController::class, 'index']);
                    Route::post('/', [UserController::class, 'store']);
                    Route::put('/{user}', [UserController::class, 'update']);
                    Route::delete('/{user}', [
                        UserController::class,
                        'destroy',
                    ]);
                    Route::post('/{user}/block', [
                        UserController::class,
                        'block',
                    ]);
                    Route::post('/{user}/unblock', [
                        UserController::class,
                        'unblock',
                    ]);
                });
            });
    });
});
