<?php

namespace Tests\Feature\Api\V1\FullSurface;

use Illuminate\Support\Facades\DB;
use Tests\Feature\Api\V1\FullSurface\Support\FullSurfaceE2EContractCase;

class ApiEmptyDatasetAndNullStateContractTest extends FullSurfaceE2EContractCase
{
    public function test_list_endpoints_return_stable_empty_shapes_when_filters_match_nothing(): void
    {
        foreach ($this->listPaths() as $path) {
            $response = $this->requestAs($this->actorFor($path), 'GET', $path . '?search=__no_match_contract_probe__&page=1&per_page=5');

            $this->assertStatusIn($response, [200, 204, 302, 400, 401, 403, 404, 405, 419, 422], "empty list $path");
            $this->assertNoServerError($response, "empty list $path");

            if ($response->getStatusCode() === 200 && $this->isJsonString($response->getContent())) {
                $json = $response->json();
                $this->assertTrue(
                    is_array($json) || $json === null,
                    "$path empty list response should remain array/envelope/null, not scalar junk."
                );
            }
        }
    }

    public function test_current_school_year_null_state_is_handled_without_crash(): void
    {
        DB::table('school_years')->update(['is_current' => false]);

        foreach ($this->schoolContextPaths() as $path) {
            $response = $this->requestAs($this->admin, 'GET', $path);

            $this->assertStatusIn($response, [200, 204, 302, 400, 401, 403, 404, 405, 409, 419, 422], "null school year $path");
            $this->assertNoServerError($response, "null school year $path");
        }
    }

    public function test_empty_class_roster_and_attendance_context_are_controlled(): void
    {
        $emptyClassSectionId = $this->seedClassSection(9911, 'Empty Contract Class', 99);
        $this->ids['classSectionId'] = $emptyClassSectionId;
        $this->ids['class_section_id'] = $emptyClassSectionId;

        foreach ($this->emptyClassPaths($emptyClassSectionId) as $path) {
            $response = $this->requestAs($this->admin, 'GET', $path);

            $this->assertStatusIn($response, [200, 204, 302, 400, 401, 403, 404, 405, 409, 419, 422], "empty class $path");
            $this->assertNoServerError($response, "empty class $path");
        }
    }

    /** @return list<string> */
    private function listPaths(): array
    {
        return [
            'api/v1/users',
            'api/v1/students',
            'api/v1/classes',
            'api/v1/attendance',
            'api/v1/finance/invoices',
            'api/v1/inventory/items',
            'api/v1/messages',
            'api/v1/support',
        ];
    }

    /** @return list<string> */
    private function schoolContextPaths(): array
    {
        return [
            'api/v1/system/semester',
            'api/v1/school-years/current',
            'api/v1/classes',
            'api/v1/attendance',
            'api/v1/report-cards',
            'api/v1/finance/reports',
        ];
    }

    /** @return list<string> */
    private function emptyClassPaths(int $classSectionId): array
    {
        return [
            "api/v1/classes/$classSectionId/students",
            "api/v1/teacher/classes/$classSectionId/roster",
            "api/v1/attendance/class/$classSectionId",
            "api/v1/class-progress/$classSectionId",
        ];
    }
}
