<?php

namespace Tests\Feature\Api\V1\Workflows;

use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\DB;
use Tests\Concerns\SeedsE2ETestFixtures;
use Tests\TestCase;

/**
 * Scenario L: Teacher academic operations.
 *
 * This walks a teacher through the practical classroom loop: roster context,
 * progress reporting, scoring, and read-only reporting. A test that merely asks
 * if a controller exists is not a workflow; it is a very expensive shrug.
 */
class ScenarioLTeacherAcademicOperationsTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;

    public function test_teacher_manages_class_progress_and_homework_scores_end_to_end(): void
    {
        $world = $this->seedTeacherClassWithStudent();
        $teacher = $world['teacher'];
        $classSectionId = $world['class_section_id'];
        $studentId = $world['student_id'];

        $this->actingAs($teacher, 'api');

        $this->getJson('/api/v1/teachers/classes?class_section_id=' . $classSectionId)
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->getJson('/api/v1/class-progress/meta?class_id=3&school_year=' . self::E2E_SCHOOL_YEAR . '&semester=' . self::E2E_SEMESTER)
            ->assertOk()
            ->assertJsonPath('status', true);

        $this->postJson('/api/v1/class-progress', [
            'class_section_id' => $classSectionId,
            'week_start' => '2025-10-05',
            'week_end' => '2025-10-11',
            'covered_islamic' => 'Seerah review',
            'covered_quran' => 'Surah recitation practice',
            'homework_islamic' => 'Read lesson notes',
            'homework_quran' => 'Memorize assigned ayat',
            'flags' => ['on_track'],
        ])->assertCreated()->assertJsonPath('status', true);

        $reportId = (int) DB::table('class_progress_reports')
            ->where('teacher_id', $teacher->id)
            ->where('class_section_id', $classSectionId)
            ->orderBy('id')
            ->value('id');
        $this->assertGreaterThan(0, $reportId);

        $this->patchJson('/api/v1/class-progress/' . $reportId, [
            'status' => 'ahead',
        ])->assertOk()->assertJsonPath('status', true);

        $this->postJson('/api/v1/scores/homework/columns', [
            'class_section_id' => $classSectionId,
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
        ])->assertOk()->assertJsonPath('ok', true);

        $this->postJson('/api/v1/scores/homework', [
            'class_section_id' => $classSectionId,
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'scores' => [
                (string) $studentId => [
                    '1' => 96,
                ],
            ],
            'missing_ok' => [],
        ])->assertOk()->assertJsonPath('ok', true);

        $this->assertDatabaseHas('homework', [
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
            'homework_index' => 1,
            'score' => 96,
            'school_year' => self::E2E_SCHOOL_YEAR,
        ]);

        $this->getJson('/api/v1/scores/homework?class_section_id=' . $classSectionId . '&semester=' . self::E2E_SEMESTER . '&school_year=' . self::E2E_SCHOOL_YEAR)
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->getJson('/api/v1/reports/report-cards/completeness?class_section_id=' . $classSectionId . '&semester=' . self::E2E_SEMESTER . '&school_year=' . self::E2E_SCHOOL_YEAR)
            ->assertOk()
            ->assertJsonPath('status', true);
    }

    public function test_unassigned_teacher_cannot_view_or_update_another_teachers_progress_report(): void
    {
        $world = $this->seedTeacherClassWithStudent();
        $otherTeacher = $this->createApiUserWithRole('teacher');

        DB::table('class_progress_reports')->insert([
            'teacher_id' => $world['teacher']->id,
            'class_section_id' => $world['class_section_id'],
            'subject' => 'Islamic Studies',
            'week_start' => '2025-10-05',
            'week_end' => '2025-10-11',
            'covered' => 'Private teacher notes',
            'homework' => 'Private homework',
            'status' => 'on_track',
            'next_week_plan' => 'Continue private unit',
            'created_at' => now(),
            'updated_at' => now(),
        ]);
        $reportId = (int) DB::table('class_progress_reports')->value('id');

        $this->actingAs($otherTeacher, 'api');

        $this->getJson('/api/v1/class-progress/' . $reportId)
            ->assertForbidden();

        $this->patchJson('/api/v1/class-progress/' . $reportId, [
            'status' => 'behind',
        ])->assertForbidden();

        $this->assertDatabaseHas('class_progress_reports', [
            'id' => $reportId,
            'status' => 'on_track',
        ]);
    }
}
