<?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 M: Administrator enrollment and classroom operations.
 *
 * This is the operational spine: parent account, student registration, class
 * section setup, student placement, teacher assignment, roster visibility, and
 * reversal paths. If this breaks, school administration becomes archaeology.
 */
class ScenarioMAdministratorEnrollmentAndClassOperationsTest extends TestCase
{
    use RefreshDatabase;
    use SeedsE2ETestFixtures;

    public function test_admin_builds_classroom_roster_from_parent_registration_to_teacher_assignment(): void
    {
        $this->seedE2EConfiguration();
        $admin = $this->actingAsApiAdministrator();
        $teacher = $this->createApiUserWithRole('teacher', [
            'firstname' => 'Maryam',
            'lastname' => 'Teacher',
        ]);
        $this->actingAs($admin, 'api');

        $parentId = $this->createParentAccountViaApi([
            'firstname' => 'Roster',
            'lastname' => 'Parent',
        ]);
        $studentId = $this->createStudentViaApi($parentId, [
            'firstname' => 'Roster',
            'lastname' => 'Student',
        ]);

        DB::table('classes')->insert([
            'id' => 9,
            'class_name' => 'Grade 9',
            'schedule' => 'Sunday',
            'capacity' => 25,
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
            'created_at' => now(),
            'updated_at' => now(),
        ]);

        $this->postJson('/api/v1/class-sections', [
            'class_id' => 9,
            'class_section_id' => 909,
            'class_section_name' => '9A',
            'semester' => self::E2E_SEMESTER,
            'school_year' => self::E2E_SCHOOL_YEAR,
        ])->assertCreated();

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

        $this->postJson('/api/v1/administrator/teacher-class/assign', [
            'teacher_id' => $teacher->id,
            'class_section_id' => 909,
            'teacher_role' => 'teacher',
            'school_year' => self::E2E_SCHOOL_YEAR,
        ])->assertOk()->assertJsonPath('ok', true);

        $this->assertDatabaseHas('student_class', [
            'student_id' => $studentId,
            'class_section_id' => 909,
            'school_year' => self::E2E_SCHOOL_YEAR,
        ]);
        $this->assertDatabaseHas('teacher_class', [
            'teacher_id' => $teacher->id,
            'class_section_id' => 909,
            'position' => 'main',
            'school_year' => self::E2E_SCHOOL_YEAR,
        ]);

        $this->getJson('/api/v1/students/' . $studentId . '/classes')
            ->assertOk()
            ->assertJsonPath('ok', true);

        $this->actingAs($teacher, 'api');
        $this->getJson('/api/v1/teachers/classes?class_section_id=909')
            ->assertOk()
            ->assertJsonPath('ok', true);
    }

    public function test_admin_can_reverse_roster_and_teacher_assignments_without_orphaning_records(): void
    {
        $this->seedE2EConfiguration();
        $admin = $this->actingAsApiAdministrator();
        $teacher = $this->createApiUserWithRole('teacher');
        $classSectionId = $this->seedClassSection(910, '9B', 9);
        $parentId = $this->createParentAccountViaApi();
        $studentId = $this->createStudentViaApi($parentId);

        $this->actingAs($admin, 'api');
        $this->assignStudentToClassViaApi($studentId, $classSectionId)->assertOk();
        $this->seedTeacherClassAssignment($teacher->id, $classSectionId);

        $this->postJson('/api/v1/students/remove-class', [
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
        ])->assertOk()->assertJsonPath('ok', true);

        $this->postJson('/api/v1/administrator/teacher-class/delete', [
            'teacher_id' => $teacher->id,
            'class_section_id' => $classSectionId,
            'position' => 'main',
            'school_year' => self::E2E_SCHOOL_YEAR,
        ])->assertOk()->assertJsonPath('ok', true);

        $this->assertDatabaseMissing('student_class', [
            'student_id' => $studentId,
            'class_section_id' => $classSectionId,
            'school_year' => self::E2E_SCHOOL_YEAR,
        ]);
        $this->assertDatabaseMissing('teacher_class', [
            'teacher_id' => $teacher->id,
            'class_section_id' => $classSectionId,
            'position' => 'main',
            'school_year' => self::E2E_SCHOOL_YEAR,
        ]);
        $this->assertDatabaseHas('students', [
            'id' => $studentId,
            'parent_id' => $parentId,
        ]);
    }
}
