<?php

namespace Tests\Feature;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Tests\TestCase;
use App\Models\User;
use App\Models\Course;
use App\Models\CourseSession;

class TeacherCourseManagementTest extends TestCase
{
    use RefreshDatabase;

    /**
     * Test teacher can access course management dashboard
     */
    public function test_teacher_can_access_course_management_dashboard(): void
    {
        $teacher = User::factory()->create(['role' => 'teacher']);

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

        $response->assertStatus(200);
        $response->assertSee('Course Management');
    }

    /**
     * Test student cannot access teacher course management
     */
    public function test_student_cannot_access_teacher_course_management(): void
    {
        $student = User::factory()->create(['role' => 'student']);

        $response = $this->actingAs($student)->get(route('teacher.courses.index'));

        $response->assertStatus(403);
    }

    /**
     * Test teacher can create a course
     */
    public function test_teacher_can_create_course(): void
    {
        $teacher = User::factory()->create(['role' => 'teacher']);

        $courseData = [
            'title' => 'Test English Course',
            'description' => 'A test course for English learning',
            'level' => 'beginner',
            'duration_weeks' => 8,
            'price' => 0,
        ];

        $response = $this->actingAs($teacher)->post(route('teacher.courses.store'), $courseData);

        $response->assertRedirect();
        $this->assertDatabaseHas('courses', [
            'title' => 'Test English Course',
            'teacher_id' => $teacher->id,
        ]);
    }

    /**
     * Test teacher can only edit their own courses
     */
    public function test_teacher_can_only_edit_own_courses(): void
    {
        $teacher1 = User::factory()->create(['role' => 'teacher']);
        $teacher2 = User::factory()->create(['role' => 'teacher']);

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

        // Teacher 1 can edit their own course
        $response = $this->actingAs($teacher1)->get(route('teacher.courses.edit', $course));
        $response->assertStatus(200);

        // Teacher 2 cannot edit teacher 1's course
        $response = $this->actingAs($teacher2)->get(route('teacher.courses.edit', $course));
        $response->assertStatus(403);
    }

    /**
     * Test course sessions display correctly
     */
    public function test_course_sessions_display_correctly(): void
    {
        $teacher = User::factory()->create(['role' => 'teacher']);
        $course = Course::factory()->create(['teacher_id' => $teacher->id]);

        // Create some sessions
        CourseSession::factory()->create([
            'course_id' => $course->id,
            'session_number' => 1,
            'title' => 'Session 1',
        ]);

        CourseSession::factory()->create([
            'course_id' => $course->id,
            'session_number' => 2,
            'title' => 'Session 2',
        ]);

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

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