<?php

namespace Database\Seeders;

use App\Models\Course;
use App\Models\Enrollment;
use App\Models\Level;
use App\Models\Lesson;
use App\Models\Module;
use App\Models\User;
use App\Models\UserProgress;
use Illuminate\Database\Seeder;
use Illuminate\Support\Str;

class TestDataSeeder extends Seeder
{
    public function run(): void
    {
        $admin = User::where('email', 'admin@nihonbuzz.academy')->first();
        if (!$admin) return;

        $n5 = Level::where('code', 'N5')->first();
        $basic = Level::where('code', 'BASIC')->first();
        $n4 = Level::where('code', 'N4')->first();

        // 1. Course: Mastering JLPT N5
        $course1 = Course::updateOrCreate(
            ['slug' => 'mastering-jlpt-n5'],
            [
                'title' => 'Mastering JLPT N5: Dasar Bahasa Jepang',
                'description' => 'Kursus lengkap untuk persiapan JLPT N5 dari nol.',
                'level_id' => $n5->id,
                'teacher_id' => $admin->id,
                'is_published' => true,
                'price' => 0,
            ]
        );

        $module1 = Module::updateOrCreate(
            ['course_id' => $course1->id, 'title' => 'Introduction to N5'],
            ['order_index' => 1]
        );

        $lessons1 = [
            ['title' => 'Selamat Datang di N5', 'slug' => 'welcome-n5', 'type' => 'video', 'video_url' => 'https://www.youtube.com/watch?v=e8mQAY6HQQc'],
            ['title' => 'Struktur Menulis Jepang', 'slug' => 'writing-structure', 'type' => 'text', 'video_url' => null],
            ['title' => 'Kata Ganti Orang', 'slug' => 'pronouns', 'type' => 'video', 'video_url' => 'https://www.youtube.com/watch?v=e8mQAY6HQQc'],
        ];

        foreach ($lessons1 as $index => $lessonData) {
            Lesson::updateOrCreate(
                ['module_id' => $module1->id, 'slug' => $lessonData['slug']],
                [
                    'title' => $lessonData['title'], 
                    'type' => $lessonData['type'], 
                    'video_url' => $lessonData['video_url'],
                    'order_index' => $index + 1
                ]
            );
        }

        // 2. Course: Hiragana & Katakana
        $course2 = Course::updateOrCreate(
            ['slug' => 'kana-mastery'],
            [
                'title' => 'Katakana & Hiragana Mastery',
                'description' => 'Kuasai cara baca dan tulis Hiragana & Katakana dalam 2 minggu.',
                'level_id' => $basic->id,
                'teacher_id' => $admin->id,
                'is_published' => true,
                'price' => 0,
            ]
        );

        $module2 = Module::updateOrCreate(
            ['course_id' => $course2->id, 'title' => 'Hiragana Basics'],
            ['order_index' => 1]
        );

        $lessons2 = [
            ['title' => 'Baris A-I-U-E-O', 'slug' => 'vowels-kana', 'type' => 'video', 'video_url' => 'https://www.youtube.com/watch?v=icK6kVTegDA'],
            ['title' => 'Baris KA-KI-KU-KE-KO', 'slug' => 'ka-line', 'type' => 'video', 'video_url' => 'https://www.youtube.com/watch?v=icK6kVTegDA'],
        ];

        foreach ($lessons2 as $index => $lessonData) {
            Lesson::updateOrCreate(
                ['module_id' => $module2->id, 'slug' => $lessonData['slug']],
                [
                    'title' => $lessonData['title'], 
                    'type' => $lessonData['type'], 
                    'video_url' => $lessonData['video_url'],
                    'order_index' => $index + 1
                ]
            );
        }

        // 3. Enroll Student
        Enrollment::updateOrCreate(
            ['user_id' => $admin->id, 'course_id' => $course1->id],
            ['enrolled_at' => now(), 'status' => 'active']
        );

        Enrollment::updateOrCreate(
            ['user_id' => $admin->id, 'course_id' => $course2->id],
            ['enrolled_at' => now(), 'status' => 'active']
        );

        // 4. Progress
        $lessonToComplete = Lesson::where('slug', 'welcome-n5')->first();
        if ($lessonToComplete) {
            UserProgress::updateOrCreate(
                ['user_id' => $admin->id, 'lesson_id' => $lessonToComplete->id],
                ['completed_at' => now()]
            );
        }

        // 5. Vocabularies
        $vocab1 = \App\Models\Vocabulary::updateOrCreate(
            ['word' => '{私|わたし}'],
            [
                'reading' => 'わたし',
                'romaji' => 'watashi',
                'meaning_id' => 'Saya',
                'meaning_en' => 'I / Me',
                'type' => 'noun',
                'level_id' => $n5->id,
            ]
        );

        $vocab2 = \App\Models\Vocabulary::updateOrCreate(
            ['word' => '{先生|せんせい}'],
            [
                'reading' => 'せんせい',
                'romaji' => 'sensei',
                'meaning_id' => 'Guru',
                'meaning_en' => 'Teacher',
                'type' => 'noun',
                'level_id' => $n5->id,
            ]
        );

        // Attach to lessons
        $lesson1 = Lesson::where('slug', 'welcome-n5')->first();
        if ($lesson1) {
            $lesson1->vocabularies()->syncWithoutDetaching([$vocab1->id, $vocab2->id]);
        }

        $lesson2 = Lesson::where('slug', 'vowels-kana')->first();
        if ($lesson2) {
             $lesson2->vocabularies()->syncWithoutDetaching([$vocab1->id]);
        }

        // 6. Update User Stats
        $admin->update([
            'xp_points' => 1250,
            'current_streak' => 5,
        ]);
    }
}
