<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Models\User;
use App\Models\Course;
use App\Models\CourseSession;
use App\Models\MediaFile;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;

class CourseEnhancementTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Test enhanced course data seeder creates comprehensive courses
     */
    public function test_enhanced_course_seeder_creates_comprehensive_data(): void
    {
        $this->seed();

        // Check that multiple courses were created
        $this->assertGreaterThanOrEqual(6, Course::count());

        // Check that courses have rich content
        $course = Course::where('slug', 'english-for-young-learner')->first();
        $this->assertNotNull($course);
        $this->assertNotEmpty($course->description);
        $this->assertNotEmpty($course->tags);
        $this->assertNotNull($course->image);

        // Check that sessions have rich content
        $sessions = $course->sessions;
        $this->assertEquals(8, $sessions->count());

        $firstSession = $sessions->first();
        $this->assertNotEmpty($firstSession->content);
        $this->assertNotNull($firstSession->materials);
        $this->assertIsArray($firstSession->materials);
    }

    /**
     * Test teacher can access enhanced course management
     */
    public function test_teacher_can_access_enhanced_course_management(): void
    {
        $teacher = User::factory()->create(['role' => 'teacher']);
        $course = Course::factory()->create(['teacher_id' => $teacher->id]);

        $response = $this->actingAs($teacher)->get(route('teacher.courses.sessions', $course));

        $response->assertStatus(200);
        $response->assertSee('Course Management');
        $response->assertSee('SESI 1');
        $response->assertSee('SESI 2');
        $response->assertSee('Course Media Library');
        $response->assertSee('Upload Media');
    }

    /**
     * Test WYSIWYG editor is included in session forms
     */
    public function test_wysiwyg_editor_included_in_session_forms(): void
    {
        $teacher = User::factory()->create(['role' => 'teacher']);
        $course = Course::factory()->create(['teacher_id' => $teacher->id]);

        // Test create session form
        $response = $this->actingAs($teacher)->get(route('teacher.sessions.create', $course));
        $response->assertStatus(200);
        $response->assertSee('tinymce-editor');
        $response->assertSee('tinymce.min.js');

        // Test edit session form
        $session = CourseSession::factory()->create(['course_id' => $course->id]);
        $response = $this->actingAs($teacher)->get(route('teacher.sessions.edit', $session));
        $response->assertStatus(200);
        $response->assertSee('tinymce-editor');
        $response->assertSee('tinymce.min.js');
    }

    /**
     * Test file upload functionality
     */
    public function test_file_upload_functionality(): void
    {
        Storage::fake('public');

        $teacher = User::factory()->create(['role' => 'teacher']);
        $course = Course::factory()->create(['teacher_id' => $teacher->id]);

        $file = UploadedFile::fake()->create('test-document.pdf', 1000, 'application/pdf');

        $response = $this->actingAs($teacher)->post(route('media.upload'), [
            'file' => $file,
            'uploadable_type' => 'Course',
            'uploadable_id' => $course->id,
            'description' => 'Test document'
        ]);

        $response->assertStatus(200);
        $response->assertJson(['success' => true]);

        // Check database record
        $this->assertDatabaseHas('media_files', [
            'original_name' => 'test-document.pdf',
            'uploadable_type' => 'Course',
            'uploadable_id' => $course->id,
            'uploaded_by' => $teacher->id,
        ]);

        // Check file was stored
        $mediaFile = MediaFile::where('original_name', 'test-document.pdf')->first();
        $this->assertNotNull($mediaFile);
        $this->assertTrue(Storage::disk('public')->exists($mediaFile->file_path));
    }

    /**
     * Test media file relationships
     */
    public function test_media_file_relationships(): void
    {
        $teacher = User::factory()->create(['role' => 'teacher']);
        $course = Course::factory()->create(['teacher_id' => $teacher->id]);

        $mediaFile = MediaFile::create([
            'filename' => 'test.pdf',
            'original_name' => 'test.pdf',
            'file_path' => 'uploads/documents/test.pdf',
            'file_type' => 'document',
            'mime_type' => 'application/pdf',
            'file_size' => 1000,
            'uploaded_by' => $teacher->id,
            'uploadable_type' => 'Course',
            'uploadable_id' => $course->id,
            'is_public' => true,
        ]);

        // Test course relationship
        $this->assertTrue($course->mediaFiles->contains($mediaFile));
        $this->assertEquals($course->id, $mediaFile->uploadable_id);
        $this->assertEquals('Course', $mediaFile->uploadable_type);

        // Test uploader relationship
        $this->assertEquals($teacher->id, $mediaFile->uploader->id);
    }

    /**
     * Test document viewer functionality
     */
    public function test_document_viewer_functionality(): void
    {
        $teacher = User::factory()->create(['role' => 'teacher']);
        $session = CourseSession::factory()->create();

        $response = $this->actingAs($teacher)->get(route('teacher.sessions.materials', $session));

        $response->assertStatus(200);
        $response->assertSee('viewDocument');
        $response->assertSee('viewMedia');
        $response->assertSee('Document Viewer');
    }

    /**
     * Test enhanced materials management
     */
    public function test_enhanced_materials_management(): void
    {
        $teacher = User::factory()->create(['role' => 'teacher']);
        $course = Course::factory()->create(['teacher_id' => $teacher->id]);
        $session = CourseSession::factory()->create([
            'course_id' => $course->id,
            'materials' => [
                [
                    'type' => 'video',
                    'title' => 'Introduction Video',
                    'content' => '/videos/intro.mp4'
                ],
                [
                    'type' => 'file',
                    'title' => 'Course Handbook',
                    'content' => '/files/handbook.pdf'
                ]
            ]
        ]);

        $response = $this->actingAs($teacher)->get(route('teacher.sessions.materials', $session));

        $response->assertStatus(200);
        $response->assertSee('Introduction Video');
        $response->assertSee('Course Handbook');
        $response->assertSee('Play Video');
        $response->assertSee('View');
        $response->assertSee('Download');
    }

    /**
     * Test course preview system
     */
    public function test_course_preview_system(): void
    {
        $course = Course::factory()->create(['status' => 'published']);
        CourseSession::factory()->count(3)->create([
            'course_id' => $course->id,
            'is_published' => true
        ]);

        $response = $this->get(route('courses.show', $course->slug));

        $response->assertStatus(200);
        $response->assertSee('SESI 1');
        $response->assertSee('SESI 2');
        $response->assertSee('SESI 3');
        $response->assertSee('showSession');
    }
}
