<?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 C: Academic Term Completion.
 *
 * @see docs/alrahma_end_to_end_use_case_test_plan.md Scenario C
 */
class ScenarioCAcademicTermTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;

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

        DB::table('homework')->insert([
            'student_id' => $studentId,
            'school_id' => 1,
            'class_section_id' => $classSectionId,
            'updated_by' => $teacher->id,
            'homework_index' => 1,
            'score' => 85,
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        DB::table('semester_scores')->insert([
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
            'school_id' => 'E2E-SCORE-1',
            'school_year' => self::E2E_SCHOOL_YEAR,
            'semester' => self::E2E_SEMESTER,
            'midterm_exam_score' => '85',
            'participation_score' => '10',
            'created_at' => now(),
            'updated_at' => now(),
        ]);

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

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

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

        $this->assertDatabaseHas('grading_locks', [
            'class_section_id' => $classSectionId,
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'is_locked' => 1,
        ]);

        $this->getJson('/api/v1/reports/report-cards/students/' . $studentId . '?school_year=' . self::E2E_SCHOOL_YEAR . '&semester=' . self::E2E_SEMESTER)
            ->assertOk();
    }

    public function test_teacher_invalid_homework_score_is_rejected(): void
    {
        $world = $this->seedTeacherClassWithStudent();

        $this->actingAs($world['teacher'], 'api');
        $response = $this->postJson('/api/v1/scores/homework', [
            'class_section_id' => $world['class_section_id'],
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'scores' => [
                $world['student_id'] => [
                    1 => 999,
                ],
            ],
        ]);

        $this->assertGreaterThanOrEqual(400, $response->getStatusCode());
        $this->assertNotSame(true, $response->json('ok'));
    }
}
