<?php

namespace Tests\Feature\Api\V1\Workflows;

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

/**
 * Scenario S: Scores, grading, placement, and report cards.
 *
 * This walks from teacher-entered scores to admin grading decisions and report
 * card readiness. Because grades are where "just trust the UI" goes to die.
 */
class ScenarioSScoringGradingAndReportCardLifecycleTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;
    use AssertsE2EApiResponses;

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

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

        foreach (['homework', 'quizzes', 'projects'] as $type) {
            $column = $this->postJson('/api/v1/scores/' . $type . '/columns', [
                'class_section_id' => $classSectionId,
                'semester' => self::E2E_SEMESTER,
                'school_year' => self::E2E_SCHOOL_YEAR,
            ]);
            $this->assertNoServerError($column, ucfirst($type) . ' score column creation');

            $save = $this->postJson('/api/v1/scores/' . $type, [
                'class_section_id' => $classSectionId,
                'semester' => self::E2E_SEMESTER,
                'school_year' => self::E2E_SCHOOL_YEAR,
                'scores' => [
                    (string) $studentId => ['1' => 91],
                ],
                'missing_ok' => [],
            ]);
            $this->assertNoServerError($save, ucfirst($type) . ' score save');
        }

        foreach (['midterm', 'final', 'participation'] as $type) {
            $save = $this->postJson('/api/v1/scores/' . $type, [
                'class_section_id' => $classSectionId,
                'semester' => self::E2E_SEMESTER,
                'school_year' => self::E2E_SCHOOL_YEAR,
                'scores' => [
                    (string) $studentId => 88,
                ],
            ]);
            $this->assertNoServerError($save, ucfirst($type) . ' score save');
        }

        $this->postJson('/api/v1/scores/comments', [
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'semester' => self::E2E_SEMESTER,
            'comment' => 'Consistent progress across the semester.',
        ])->assertSuccessful();

        $this->getJson('/api/v1/scores/overview?class_section_id=' . $classSectionId . '&school_year=' . self::E2E_SCHOOL_YEAR)
            ->assertSuccessful();
        $this->getJson('/api/v1/scores/student-scores?student_id=' . $studentId . '&class_section_id=' . $classSectionId)
            ->assertSuccessful();

        $this->actingAs($admin, 'api');
        $this->getJson('/api/v1/grading/overview?class_section_id=' . $classSectionId)
            ->assertSuccessful();
        $this->postJson('/api/v1/grading/decisions/generate', [
            'class_section_id' => $classSectionId,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'semester' => self::E2E_SEMESTER,
        ])->assertSuccessful();
        $this->getJson('/api/v1/grading/decisions?class_section_id=' . $classSectionId)
            ->assertSuccessful();
        $this->getJson('/api/v1/reports/report-cards/completeness?class_section_id=' . $classSectionId . '&school_year=' . self::E2E_SCHOOL_YEAR . '&semester=' . self::E2E_SEMESTER)
            ->assertSuccessful();
    }

    public function test_grading_locks_and_below_sixty_intervention_are_visible_and_mutable(): void
    {
        $world = $this->seedTeacherClassWithStudent();
        $admin = $this->actingAsApiAdministrator();
        $studentId = $world['student_id'];
        $classSectionId = $world['class_section_id'];
        $this->actingAs($admin, 'api');

        $this->postJson('/api/v1/grading/locks/toggle', [
            'type' => 'homework',
            'class_section_id' => $classSectionId,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'semester' => self::E2E_SEMESTER,
            'locked' => true,
        ])->assertSuccessful();

        $this->postJson('/api/v1/grading/locks/all', [
            'class_section_id' => $classSectionId,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'semester' => self::E2E_SEMESTER,
            'locked' => false,
        ])->assertSuccessful();

        $this->getJson('/api/v1/grading/below-sixty?school_year=' . self::E2E_SCHOOL_YEAR)
            ->assertSuccessful();
        $this->postJson('/api/v1/grading/below-sixty/decisions', [
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
            'decision' => 'monitor',
            'notes' => 'E2E intervention note.',
            'school_year' => self::E2E_SCHOOL_YEAR,
            'semester' => self::E2E_SEMESTER,
        ])->assertSuccessful();
        $this->postJson('/api/v1/grading/below-sixty/meeting', [
            'student_id' => $studentId,
            'meeting_date' => '2025-10-12',
            'notes' => 'Parent meeting scheduled.',
        ])->assertSuccessful();
    }
}
