<?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 B: Normal Sunday School Day.
 *
 * @see docs/alrahma_end_to_end_use_case_test_plan.md Scenario B
 */
class ScenarioBSundaySchoolDayTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;

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

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

        $form = $this->getJson('/api/v1/attendance/teacher/form?class_section_id=' . $classSectionId)
            ->assertOk();

        $date = (string) ($form->json('data.current_sunday') ?? '2025-10-05');
        $submit = $this->postJson('/api/v1/attendance/teacher/submit', [
            'class_section_id' => $classSectionId,
            'date' => $date,
            'attendance' => [
                ['student_id' => $studentId, 'status' => 'present'],
            ],
            'teachers' => [
                (string) $teacher->id => ['status' => 'present'],
            ],
        ]);

        $submit->assertOk();
        $this->assertDatabaseHas('attendance_data', [
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
            'status' => 'present',
            'school_year' => self::E2E_SCHOOL_YEAR,
        ]);

        // Parent views attendance for their child.
        $this->actingAs($parent, 'api');
        $this->getJson('/api/v1/parents/attendance?school_year=' . self::E2E_SCHOOL_YEAR)
            ->assertOk()
            ->assertJsonPath('ok', true);
    }

    public function test_teacher_cannot_submit_attendance_for_unassigned_class(): void
    {
        $world = $this->seedTeacherClassWithStudent();
        $otherSectionId = $this->seedClassSection(999, '9Z', 9);
        $otherTeacher = $this->createApiUserWithRole('teacher');
        $this->seedTeacherClassAssignment($otherTeacher->id, $otherSectionId);

        $this->actingAs($world['teacher'], 'api');
        $this->postJson('/api/v1/attendance/teacher/submit', [
            'class_section_id' => $otherSectionId,
            'date' => '2025-10-05',
            'attendance' => [
                ['student_id' => $world['student_id'], 'status' => 'present'],
            ],
            'teachers' => [
                (string) $world['teacher']->id => ['status' => 'present'],
            ],
        ])->assertStatus(422);

        $this->assertDatabaseMissing('attendance_data', [
            'student_id' => $world['student_id'],
            'class_section_id' => $otherSectionId,
        ]);
    }

    public function test_admin_views_daily_attendance_summary(): void
    {
        $world = $this->seedTeacherClassWithStudent();
        $admin = $this->actingAsApiAdministrator();

        DB::table('attendance_data')->insert([
            'class_id' => 3,
            'class_section_id' => $world['class_section_id'],
            'student_id' => $world['student_id'],
            'school_id' => '1',
            'date' => '2025-10-05',
            'status' => 'late',
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $this->actingAs($admin, 'api');
        $this->getJson('/api/v1/attendance/admin/daily?date=2025-10-05')
            ->assertOk();
    }
}
