<?php

use App\Http\Controllers\Api\V1\Client;
use App\Http\Controllers\Api\V1\Client\Auth\AuthenticationController;
use App\Http\Controllers\Api\V1\Client\games\HexlingGameController;
use App\Http\Controllers\Api\V1\Client\games\WordlingGameController;
use App\Http\Controllers\Api\V1\Client\MiscController;
use App\Http\Middleware\ClientAuthMiddleware;
use Illuminate\Support\Facades\Route;

Route::group(['prefix' => 'auth', 'as' => 'auth.'], function () {
    Route::middleware('guest:client')->group(function () {
        Route::post('sign-up', [AuthenticationController::class, 'register']);
        Route::post('sign-in', [AuthenticationController::class, 'login']);
        Route::get('/login/{provider}', [AuthenticationController::class, 'redirectToProvider']);
        Route::get('/login/{provider}/callback', [AuthenticationController::class, 'handleProviderCallback']);
        Route::post('send-otp', [AuthenticationController::class, 'sendOtp']);
        Route::post('verify-email-or-phone', [AuthenticationController::class, 'verifyEmailOrPhone']);
        Route::post('sign-in-with-2fa', [AuthenticationController::class, 'twoFactorLogin']);
        Route::post('forgot-password', [AuthenticationController::class, 'forgotPassword']);

    });

    Route::group(['middleware' => 'auth:client'], function () {
        Route::get('kyc-forms', [AuthenticationController::class, 'getKycForms']);
        Route::post('kyc-forms', [AuthenticationController::class, 'kycSubmit']);
        Route::post('logout', [AuthenticationController::class, 'logout']);
        Route::get('me', [AuthenticationController::class, 'me']);
    });
});

/**
 * info and initial data also default page seo
 */
Route::get('info', [Client\MiscController::class, 'getInfo']);
/**
 * get pages
 */
Route::get('app-robots', [Client\MiscController::class, 'getRobots']);
Route::get('app-sitemap', [Client\MiscController::class, 'getSitemap']);
/**
 * language switch
 */
Route::get('/lang/{locale}', [Client\MiscController::class, 'changeLang'])->name('lang.change');
Route::get('langs', [Client\MiscController::class, 'getLangs']);

Route::get('menus', [Client\MiscController::class, 'getMenus']);
Route::get('pages', [Client\MiscController::class, 'getPages']);
Route::get('pwa-manifest', [Client\MiscController::class, 'getPwa']);

Route::get('testimonials', [Client\MiscController::class, 'getTestimonials']);
Route::get('page-seo', [Client\MiscController::class, 'getPageSeo']);
Route::get('currencies', [Client\MiscController::class, 'getCurrencies']);
Route::get('/payment-gateways', [MiscController::class, 'getPaymentGateways'])->name('payment-gateways');
Route::post('/currency-convert/{code}', [MiscController::class, 'currencyConvert'])->name('currency-convert');
Route::post('subscribe-newsletter', [Client\MiscController::class, 'subscribeNewsletter']);
Route::get('unsubscribe/{email}', [MiscController::class, 'unsubscribe'])->name('unsubscribe');
Route::get('get-ads', [MiscController::class, 'getAds']);
// Contact form
Route::post('contact-us', Client\ContactController::class);
// leaderboard fetch
Route::get('/leaderboard', [Client\LeaderboardController::class, 'getTopUsers']);

/**
 * Quiz routes
 */
Route::get('quiz-categories', [Client\Quiz\QuizController::class, 'getCategories']);
Route::get('quizzes', [Client\Quiz\QuizController::class, 'getQuiz']);
Route::get('quiz-details/{quiz}', [Client\Quiz\QuizController::class, 'getSingleQuiz']);
Route::get('/{quiz}/quiz/{username}/result', [Client\Quiz\QuizController::class, 'getQuizResult']);

/**
 * Contest routes
 * These routes are for client side access to quizzes and contests.
 */
Route::get('contest-categories', [Client\Contest\ContestController::class, 'getCategories']);
Route::get('get-contests', [Client\Contest\ContestController::class, 'getContests']);
Route::get('single-contest/{slug}', [Client\Contest\ContestController::class, 'getSingleContest']);
Route::get('contest-participants/{slug}', [Client\Contest\ContestController::class, 'getContestParticipants']);
Route::get('/{contest}/contest/{username}/result', [Client\Contest\ContestController::class, 'getContestResult']);

Route::middleware(['auth:client', ClientAuthMiddleware::class])->group(function () {
    /**
     * Quiz routes
     * These routes are for authenticated users to access quizzes, submit answers, and view results.
     * They allow users to assign quizzes, get single quizzes, submit answers, and view quiz
     */
    Route::post('assign-quiz/{quiz}', [Client\Quiz\QuizController::class, 'assignNewQuiz']);
    Route::get('questions/{quiz}', [Client\Quiz\QuizController::class, 'getQuestions']);
    Route::post('quiz-answer/{quiz}/{question}', [Client\Quiz\QuizController::class, 'submitAnswer']);

    /**
     * Contest routes
     * These routes are for authenticated users to access contests, submit answers, and view results.
     * They allow users to assign contests, get single contests, submit answers, and view contest
     */
    Route::post('join-contest/{slug}', [Client\Contest\ContestController::class, 'joinToContest']);
    Route::post('contest-answer/{contest}/{question}', [Client\Contest\ContestController::class, 'submitAnswer']);

    // profile
    Route::group(['prefix' => 'profile', 'as' => 'profile.'], function () {
        Route::apiResource('deposits', Client\Profile\DepositController::class)->only('index', 'store');
        Route::apiResource('withdraws', Client\Profile\WithdrawController::class)->only('index', 'store');
        Route::get('withdraw-methods', [Client\Profile\WithdrawController::class, 'getWithdrawMethods']);
        Route::apiResource('support-tickets', Client\Profile\SupportTicketController::class);
        Route::post('reply-to-support-ticket/{supportTicket}', [Client\Profile\SupportTicketController::class, 'reply']);
        Route::get('coin-logs', Client\Profile\CoinLogController::class);
        Route::controller(Client\Profile\QuizHistoryController::class)->group(function () {
            Route::get('quiz-history', 'getQuizHistory');
            Route::get('leaderboards', 'getLeaderBoards');
        });

        Route::get('/badges', [Client\Profile\BadgeController::class, 'index']);

        Route::apiResource('favorites', Client\Profile\FavoriteController::class)->only('index', 'store');

        Route::post('update-profile', [AuthenticationController::class, 'updateProfile']);

        Route::controller(Client\Profile\UserProfileController::class)->group(function () {
            Route::get('/statistics', 'getProfileStatistics');
            Route::post('/2fa/toggle', 'toggle2fa');
        });

        /**
         * firebase notification fcm token update route
         */
        Route::post('update-fcm-token', [AuthenticationController::class, 'updateFcmToken'])->name('notification.token-update');

        Route::prefix('notifications')->group(function () {
            Route::get('/', [Client\NotificationController::class, 'index']);
            Route::post('/', [Client\NotificationController::class, 'store']);
            Route::delete('/{id}', [Client\NotificationController::class, 'destroy']);
            Route::delete('/', [Client\NotificationController::class, 'destroyAll']);
        });
    });

    /*
    * Game Routes for wordling
    */
    Route::get('games/wordling/user-submissions', [WordlingGameController::class, 'getWordlingSubmissions']);
    Route::post('games/wordling/check', [WordlingGameController::class, 'checkWordlingGame']);
    Route::get('games/wordling', [WordlingGameController::class, 'getWordlingGames']);
    Route::get('games/wordling/game-result', [WordlingGameController::class, 'getGameResult']);
    Route::get('games/wordling/leaderboard', [WordlingGameController::class, 'getLeaderboardStats']);

    /*
     * Game Routes for hexling
     */
    Route::get('games/hexling/user-submissions', [HexlingGameController::class, 'getHexlingSubmissions']);
    Route::post('games/hexling/check', [HexlingGameController::class, 'checkHexlingGame']);
    Route::get('games/hexling', [HexlingGameController::class, 'getHexlingGame']);
    Route::get('games/hexling/user-badges', [HexlingGameController::class, 'getUserBadges']);
    Route::get('games/hexling/game-result', [HexlingGameController::class, 'getHexlingGameResult']);
    Route::get('games/hexling/global-rankings', [HexlingGameController::class, 'getHexlingGlobalRankings']);
});
