<?php

namespace Database\Seeders;

use App\Models\Question;
use App\Models\Subject;
use Illuminate\Database\Seeder;

/**
 * Seeds a per-subject question bank across three difficulties, so the
 * performance-based practice suggester has real content to recommend.
 */
class QuestionSeeder extends Seeder
{
    public function run(): void
    {
        foreach (Subject::all() as $subject) {
            $bank = $this->bankFor($subject->name);
            foreach ($bank as [$difficulty, $topic, $text, $options, $answer]) {
                Question::updateOrCreate(
                    ['subject_id' => $subject->id, 'text' => $text],
                    [
                        'topic' => $topic,
                        'difficulty' => $difficulty,
                        'options' => $options,
                        'answer' => $answer,
                        'marks' => $difficulty === 'hard' ? 3 : ($difficulty === 'medium' ? 2 : 1),
                    ],
                );
            }
        }
    }

    /** Curated questions: [difficulty, topic, text, options|null, answer|null]. */
    private function bankFor(string $subject): array
    {
        $generic = fn () => [
            ['easy', 'Basics', "Define a key term in {$subject}.", null, null],
            ['easy', 'Basics', "List three important ideas in {$subject}.", null, null],
            ['medium', 'Application', "Explain how a concept in {$subject} is used in daily life.", null, null],
            ['medium', 'Application', "Compare two related ideas in {$subject}.", null, null],
            ['hard', 'Analysis', "Analyse a challenging problem in {$subject} and justify your answer.", null, null],
            ['hard', 'Analysis', "Design your own example that demonstrates a {$subject} principle.", null, null],
        ];

        $banks = [
            'Mathematics' => [
                ['easy', 'Arithmetic', 'What is 7 × 8?', ['54', '56', '64', '48'], '56'],
                ['easy', 'Arithmetic', 'What is 15 + 27?', ['42', '32', '52', '41'], '42'],
                ['easy', 'Fractions', 'Which is larger: 1/2 or 1/3?', ['1/2', '1/3', 'Equal', 'Cannot tell'], '1/2'],
                ['medium', 'Algebra', 'Solve for x: 2x + 5 = 17', ['5', '6', '7', '8'], '6'],
                ['medium', 'Geometry', 'A triangle has angles 60° and 70°. What is the third angle?', ['40°', '50°', '60°', '30°'], '50°'],
                ['medium', 'Fractions', 'What is 3/4 + 1/8?', ['7/8', '4/12', '1', '5/8'], '7/8'],
                ['hard', 'Algebra', 'Solve: x² − 5x + 6 = 0', ['x=2,3', 'x=1,6', 'x=−2,−3', 'x=0,5'], 'x=2,3'],
                ['hard', 'Geometry', 'Find the area of a circle with radius 7 (use π≈22/7).', ['154', '44', '49', '308'], '154'],
                ['hard', 'Word problems', 'A train travels 240 km in 3 hours. What is its speed?', ['60 km/h', '80 km/h', '90 km/h', '120 km/h'], '80 km/h'],
            ],
            'English' => [
                ['easy', 'Vocabulary', 'What is the opposite of "happy"?', ['sad', 'glad', 'tall', 'fast'], 'sad'],
                ['easy', 'Grammar', 'Choose the article: ___ apple a day.', ['An', 'A', 'The', 'No article'], 'An'],
                ['medium', 'Tenses', 'Past tense of "go"?', ['goed', 'went', 'gone', 'going'], 'went'],
                ['medium', 'Grammar', 'Identify the verb: "She reads books."', ['She', 'reads', 'books', 'none'], 'reads'],
                ['hard', 'Reading', 'Write a five-sentence paragraph about your school.', null, null],
                ['hard', 'Grammar', 'Rewrite in passive voice: "The teacher praised the student."', null, 'The student was praised by the teacher.'],
            ],
            'Science' => [
                ['easy', 'Biology', 'Which organ pumps blood in the human body?', ['Heart', 'Lungs', 'Liver', 'Brain'], 'Heart'],
                ['easy', 'Physics', 'What force pulls objects toward the earth?', ['Gravity', 'Friction', 'Magnetism', 'Tension'], 'Gravity'],
                ['medium', 'Chemistry', 'What is the chemical symbol for water?', ['H2O', 'CO2', 'O2', 'NaCl'], 'H2O'],
                ['medium', 'Biology', 'Plants make food through which process?', ['Photosynthesis', 'Respiration', 'Digestion', 'Evaporation'], 'Photosynthesis'],
                ['hard', 'Physics', 'State Newton\'s second law of motion and give an example.', null, null],
                ['hard', 'Chemistry', 'Explain the difference between a mixture and a compound.', null, null],
            ],
            'Islamic Studies' => [
                ['easy', 'Quran', 'How many Surahs are in the Holy Quran?', ['114', '104', '99', '120'], '114'],
                ['easy', 'Fiqh', 'How many obligatory (fard) prayers are there in a day?', ['5', '3', '7', '4'], '5'],
                ['medium', 'Seerah', 'In which city was the Prophet Muhammad (PBUH) born?', ['Makkah', 'Madinah', 'Taif', 'Jerusalem'], 'Makkah'],
                ['medium', 'Fiqh', 'Name the pillars of Islam.', null, null],
                ['hard', 'Seerah', 'Describe the importance of the Hijrah and its lessons.', null, null],
            ],
            'Dari' => [
                ['easy', 'Grammar', 'یک اسم در جمله «کتاب روی میز است» کدام است؟', ['کتاب', 'روی', 'است', 'میز'], 'کتاب'],
                ['medium', 'Comprehension', 'یک جمله با کلمه «مکتب» بسازید.', null, null],
                ['hard', 'Writing', 'یک پاراگراف کوتاه درباره وطن بنویسید.', null, null],
            ],
            'Pashto' => [
                ['easy', 'Grammar', 'په «زه ښوونځي ته ځم» جمله کې فعل کوم دی؟', ['زه', 'ښوونځي', 'ځم', 'ته'], 'ځم'],
                ['medium', 'Comprehension', 'د «کتاب» کلمې سره یوه جمله جوړه کړئ.', null, null],
                ['hard', 'Writing', 'د خپل ښوونځي په اړه لنډ انشا ولیکئ.', null, null],
            ],
        ];

        return array_merge($banks[$subject] ?? [], $generic());
    }
}
