<?php

namespace Database\Factories;

use Illuminate\Database\Eloquent\Factories\Factory;

/**
 * @extends \Illuminate\Database\Eloquent\Factories\Factory<\App\Models\ContestQuestion>
 */
class ContestQuestionFactory extends Factory
{
    /**
     * Define the model's default state.
     *
     * @return array<string, mixed>
     */
    public function definition(): array
    {
        $explanationType = \App\Enums\QuestionExplanationTypeEnum::values()[rand(0, count(\App\Enums\QuestionExplanationTypeEnum::values()) - 2)];
        $question_type = \App\Enums\QuestionTypeEnum::values()[rand(0, count(\App\Enums\QuestionTypeEnum::values()) - 1)];
        $data = $this->setQuestionOptions($question_type);

        return [
            'explanation_type' => $explanationType,
            'contest_id' => 1,
            'time_limit' => 2,
            'order' => 1,
            'score' => 10,
            'question_type' => $question_type,
            'options' => $data['options'],
            'correct_answers' => $data['correct_answers'],
        ];
    }

    public function configure(): static
    {
        return $this->afterCreating(function ($model) {
            $model->translations()->create([
                'locale' => 'en',
                'question_text' => $model->question_type == \App\Enums\QuestionTypeEnum::FILL_IN_THE_BLANK->value ? 'Tea shop {A} in exotic {B} of leaves from {C} the world plus {D} accessories.' : fake()->sentence().'?',
                'question_explanation' => $this->getExplanation($model->explanation_type),
                'hints' => fake()->sentence(500),
                'answer_explanation' => fake()->sentence(500),
            ]);
        });
    }

    private function getExplanation($explanationType)
    {
        $explanation = '';
        switch ($explanationType) {
            case \App\Enums\QuestionExplanationTypeEnum::TEXT->value:
                $explanation = fake()->text();
                break;
            case \App\Enums\QuestionExplanationTypeEnum::VIDEO->value:
                $explanation = '/assets/client/video/sample-quiz-video.mp4';
                break;
            case \App\Enums\QuestionExplanationTypeEnum::IMAGE->value:
                $explanation = '/assets/client/sample-video-thumbnail.png';
                break;
            case \App\Enums\QuestionExplanationTypeEnum::AUDIO->value:
                $explanation = '/assets/client/audio/audio.mp3';
                break;
        }

        return $explanation;
    }

    private function setQuestionOptions($questionType)
    {
        $options = [];
        $correct_answers = [];
        if ($questionType == \App\Enums\QuestionTypeEnum::TRUE_FALSE->value) {
            $options = [
                [
                    'label' => 'True',
                    'value' => 'A',
                ],
                [
                    'label' => 'False',
                    'value' => 'B',
                ],
            ];
        } else {
            for ($i = 0; $i < 4; $i++) {
                $options[] = [
                    'label' => fake()->sentence(),
                    'value' => chr(65 + $i),
                ];
            }

        }

        if (in_array($questionType, [\App\Enums\QuestionTypeEnum::TRUE_FALSE->value, \App\Enums\QuestionTypeEnum::SINGLE_CHOICE->value])) {
            $correct_answers = [$options[rand(0, count($options) - 1)]['value']];
        } else {
            $correct_answers = array_fill(0, rand(1, count($options) - 1), $options[rand(0, count($options) - 1)]['value']);
        }

        return [
            'options' => $options,
            'correct_answers' => $correct_answers,
        ];
    }
}
