<?php

namespace Database\Seeders;

use App\Models\Quiz;
use App\Models\QuizQuestion;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class QuizSeeder extends Seeder
{
    public function run(): void
    {
        $quiz = Quiz::updateOrCreate(
            ['slug' => 'main'],
            [
                'uuid' => (string) Str::uuid(),
                'title' => 'Live Product Management Quiz',
                'test_type' => 'General',
                'status' => 'waiting',
                'is_enabled' => true,
                'current_question_id' => null,
                'current_question_started_at' => null,
                'current_question_ends_at' => null,
            ]
        );

        $quiz->questions()->delete();

        $questions = [
            [
                'question' => 'Which feature is most important for a real-time quiz system?',
                'seconds' => 20,
                'options' => [
                    ['Live updates', true],
                    ['Printed answer sheet', false],
                    ['Manual Excel ranking', false],
                    ['Email-only answers', false],
                ],
            ],
            [
                'question' => 'Which database is used in this Laravel version of the project?',
                'seconds' => 20,
                'options' => [
                    ['MySQL', true],
                    ['Microsoft Word', false],
                    ['Adobe Photoshop', false],
                    ['FTP only', false],
                ],
            ],
            [
                'question' => 'How should the top 3 ranking be calculated after each question?',
                'seconds' => 25,
                'options' => [
                    ['Fastest correct answers first', true],
                    ['Slowest answers first', false],
                    ['Random participant order', false],
                    ['Only alphabetically by name', false],
                ],
            ],
            [
                'question' => 'What happens when a participant submits an incorrect answer?',
                'seconds' => 20,
                'options' => [
                    ['They receive 0 points for that question', true],
                    ['They automatically become first', false],
                    ['The quiz stops immediately', false],
                    ['Their name is deleted', false],
                ],
            ],
            [
                'question' => 'Which Laravel tool provides the WebSocket server in this project?',
                'seconds' => 20,
                'options' => [
                    ['Laravel Reverb', true],
                    ['Laravel Mail', false],
                    ['Laravel Queue only', false],
                    ['Laravel Blade only', false],
                ],
            ],
        ];

        foreach ($questions as $questionIndex => $item) {
            $question = QuizQuestion::create([
                'quiz_id' => $quiz->id,
                'sort_order' => $questionIndex + 1,
                'question_text' => $item['question'],
                'duration_seconds' => $item['seconds'],
            ]);

            foreach ($item['options'] as $optionIndex => [$text, $isCorrect]) {
                $question->options()->create([
                    'sort_order' => $optionIndex + 1,
                    'option_text' => $text,
                    'is_correct' => $isCorrect,
                ]);
            }
        }
    }
}
