<?php

namespace Tests\Feature\Api\V1\FullSurface;

use Tests\Feature\Api\V1\FullSurface\Support\FullSurfaceE2EContractCase;

class ApiSchoolOperationsRegressionBackstopTest extends FullSurfaceE2EContractCase
{
    public function test_administrator_can_probe_core_school_operations_without_route_level_crashes(): void
    {
        $routes = $this->apiRoutesContainingAny([
            'dashboard', 'students', 'parents', 'classes', 'class-sections', 'teachers', 'attendance', 'scores', 'report-card',
            'finance', 'inventory', 'messages', 'support', 'school-years', 'promotions', 'settings', 'configuration',
        ]);

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();
            $response = $this->requestAs($this->admin, $method, $uri, $this->payloadFor($method, $uri));

            $this->assertNoServerError($response, "admin $method $uri regression backstop");
            $this->assertControlled($response, $method, $uri);
        }
    }

    public function test_user_facing_portals_have_at_least_one_reachable_read_contract_per_actor(): void
    {
        $portalGroups = [
            'teacher' => [$this->teacher, ['teacher', 'class-progress', 'attendance']],
            'parent' => [$this->parent, ['parent', 'families', 'guardians']],
            'student' => [$this->studentUser, ['student']],
        ];

        foreach ($portalGroups as $label => [$actor, $needles]) {
            $sawControlledRoute = false;

            foreach ($this->apiRoutesContainingAny($needles) as $route) {
                if ($this->primaryMethod($route) !== 'GET') {
                    continue;
                }

                $uri = $route->uri();
                $response = $this->requestAs($actor, 'GET', $uri);
                $this->assertNoServerError($response, "$label GET $uri portal reachability");
                $this->assertControlled($response, 'GET', $uri);
                $sawControlledRoute = true;
            }

            $this->assertTrue($sawControlledRoute, "$label portal should have at least one registered GET route under its domain.");
        }
    }
}
