<?php

use App\Http\Controllers\AdminQuizController;
use App\Http\Controllers\QuizController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Route;

Route::get('/', [QuizController::class, 'welcome'])->name('home');
Route::get('/csrf-token', fn (Request $request) => response()->json([
    'token' => $request->session()->token(),
]))->name('csrf.token');

Route::get('/join/{quizSlug?}', [QuizController::class, 'join'])->name('quiz.join');
Route::post('/join', [QuizController::class, 'storeParticipant'])->name('quiz.participant.store');
Route::get('/play/{sessionUuid}', [QuizController::class, 'play'])->name('quiz.play');
Route::post('/play/{sessionUuid}/answer', [QuizController::class, 'answer'])->name('quiz.answer');

Route::get('/admin/login', [AdminQuizController::class, 'login'])->name('admin.login');
Route::post('/admin/login', [AdminQuizController::class, 'authenticate'])->name('admin.authenticate');

Route::middleware('admin.password')->group(function () {
    Route::get('/admin', [AdminQuizController::class, 'index'])->name('admin.quiz');
    Route::get('/admin/reports', [AdminQuizController::class, 'report'])->name('admin.reports.index');
    Route::get('/admin/reports/{quiz}', [AdminQuizController::class, 'report'])->name('admin.reports.show');
    Route::post('/admin/start', [AdminQuizController::class, 'start'])->name('admin.quiz.start');
    Route::post('/admin/next', [AdminQuizController::class, 'next'])->name('admin.quiz.next');
    Route::post('/admin/reset', [AdminQuizController::class, 'reset'])->name('admin.quiz.reset');
    Route::post('/admin/import', [AdminQuizController::class, 'import'])->name('admin.quiz.import');
    Route::post('/admin/tests', [AdminQuizController::class, 'storeTest'])->name('admin.tests.store');
    Route::post('/admin/tests/{quiz}/toggle', [AdminQuizController::class, 'toggleTest'])->name('admin.tests.toggle');
    Route::delete('/admin/tests/{quiz}', [AdminQuizController::class, 'destroyTest'])->name('admin.tests.destroy');
    Route::post('/admin/questions', [AdminQuizController::class, 'storeQuestion'])->name('admin.questions.store');
    Route::get('/admin/templates/questions', [AdminQuizController::class, 'downloadTemplate'])->name('admin.templates.questions');
    Route::post('/admin/logout', [AdminQuizController::class, 'logout'])->name('admin.logout');
});
