<?php

namespace Tests\Feature\Api\V1\Workflows;

use App\Models\User;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Event;
use Tests\Concerns\SeedsE2ETestFixtures;
use Tests\TestCase;

/**
 * Scenario A: New Family Registration to Active Enrollment.
 *
 * @see docs/alrahma_end_to_end_use_case_test_plan.md Scenario A
 */
class ScenarioAFamilyEnrollmentTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;

    public function test_admin_onboards_family_through_active_enrollment(): void
    {
        Event::fake();
        $this->seedE2EConfiguration();

        $admin = $this->actingAsApiAdministrator();
        $classSectionId = $this->seedClassSection(701, '5A', 5);
        $teacher = $this->createApiUserWithRole('teacher');
        $this->seedTeacherClassAssignment($teacher->id, $classSectionId);

        $parentId = $this->createParentAccountViaApi([
            'firstname' => 'Khadija',
            'lastname' => 'Saleh',
            'email' => 'khadija.saleh@example.test',
        ]);
        $studentId = $this->createStudentViaApi($parentId, [
            'firstname' => 'Yusuf',
            'lastname' => 'Saleh',
            'name' => 'Khadija Saleh',
            'email' => 'khadija.saleh@example.test',
        ]);

        $this->assignStudentToClassViaApi($studentId, $classSectionId)
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->postEnrollmentStatuses([$studentId => 'enrolled'])->assertOk();

        $this->assertDatabaseHas('enrollments', [
            'student_id' => $studentId,
            'parent_id' => $parentId,
            'enrollment_status' => 'enrolled',
        ]);

        // Teacher sees student in roster.
        $this->actingAs($teacher, 'api');
        $this->getJson('/api/v1/teachers/classes?class_section_id=' . $classSectionId)
            ->assertOk()
            ->assertJsonPath('ok', true);

        $studentIds = array_column($this->getJson('/api/v1/teachers/classes?class_section_id=' . $classSectionId)->json('students'), 'student_id');
        $this->assertContains($studentId, $studentIds);

        // Parent sees own profile.
        $parent = User::query()->findOrFail($parentId);
        $this->actingAs($parent, 'api');
        $this->getJson('/api/v1/parents/profile')
            ->assertOk()
            ->assertJsonPath('ok', true);

        // Admin can search the new user.
        $this->actingAs($admin, 'api');
        $this->getJson('/api/v1/administrator/dashboard/user-search?query=Saleh')
            ->assertOk();
    }

    public function test_parent_cannot_update_unrelated_student(): void
    {
        Event::fake();
        $this->seedE2EConfiguration();

        $this->actingAsApiAdministrator();
        $parentA = $this->createApiUserWithRole('parent');
        $parentB = $this->createApiUserWithRole('parent');

        $studentB = $this->createStudentViaApi($parentB->id, [
            'firstname' => 'Other',
            'lastname' => 'Child',
        ]);

        $this->actingAs($parentA, 'api');
        $this->patchJson('/api/v1/parents/students/' . $studentB, [
            'firstname' => 'Hacked',
            'lastname' => 'Child',
            'dob' => '2015-01-01',
            'gender' => 'Male',
        ])->assertNotFound();

        $this->assertDatabaseHas('students', [
            'id' => $studentB,
            'firstname' => 'Other',
        ]);
    }
}
