<?php

namespace Tests\Feature\Api\V1\Workflows;

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

/**
 * Scenario F: Teacher Submission Follow-Up.
 *
 * @see docs/alrahma_end_to_end_use_case_test_plan.md Scenario F
 */
class ScenarioFTeacherSubmissionTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;

    public function test_admin_detects_missing_submission_and_notifies_teacher(): void
    {
        Mail::fake();

        $world = $this->seedTeacherClassWithStudent();
        $admin = $this->actingAsApiAdministrator();
        $teacher = $world['teacher'];
        $classSectionId = $world['class_section_id'];

        $this->actingAs($admin, 'api');
        $report = $this->getJson('/api/v1/administrator/teacher-submissions');
        $report->assertOk();
        $this->assertNotEmpty($report->json('rows'));

        $row = collect($report->json('rows'))
            ->firstWhere('class_section_id', $classSectionId);
        $this->assertNotNull($row);
        $this->assertNotEmpty($row['missing_items'] ?? []);

        $notify = $this->postJson('/api/v1/administrator/teacher-submissions/notify', [
            'notify' => [
                $classSectionId => [
                    $teacher->id => 1,
                ],
            ],
            'missing_items' => [
                $classSectionId => [
                    $teacher->id => base64_encode(json_encode($row['missing_items'])),
                ],
            ],
        ]);

        $notify->assertOk()->assertJsonPath('sent', 1);

        $this->assertDatabaseHas('teacher_submission_notification_history', [
            'teacher_id' => $teacher->id,
            'class_section_id' => $classSectionId,
            'admin_id' => $admin->id,
            'notification_category' => 'teacher_submissions',
        ]);

        // Teacher submits missing homework after reminder.
        $this->actingAs($teacher, 'api');
        $this->postJson('/api/v1/scores/homework', [
            'class_section_id' => $classSectionId,
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'scores' => [
                $world['student_id'] => [
                    1 => 88,
                ],
            ],
        ])->assertOk()->assertJsonPath('ok', true);

        $this->assertDatabaseHas('homework', [
            'student_id' => $world['student_id'],
            'class_section_id' => $classSectionId,
            'homework_index' => 1,
            'score' => 88,
        ]);
    }

    public function test_duplicate_notify_requires_valid_payload(): void
    {
        $admin = $this->actingAsApiAdministrator();

        $this->actingAs($admin, 'api');
        $this->postJson('/api/v1/administrator/teacher-submissions/notify', [
            'missing_items' => [],
        ])->assertStatus(422)
            ->assertJsonValidationErrors(['notify']);
    }
}
