<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiFailureModeResilienceExpansionTest extends FullSurfaceE2EContractCase
{
    public function test_mutation_routes_fail_cleanly_when_required_relationships_are_null(): void
    {
        $routes = array_slice(array_filter($this->apiRoutes(), fn ($route) => in_array($this->primaryMethod($route), ['POST', 'PUT', 'PATCH'], true)), 0, 120);

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();
            $payload = $this->payloadFor($method, $uri) + [
                'student_id' => null,
                'parent_id' => null,
                'class_section_id' => null,
                'school_year_id' => null,
                'invoice_id' => null,
            ];

            $response = $this->requestAs($this->actorFor($uri), $method, $uri, $payload);

            $this->assertControlled($response, $method, $uri);
            $this->assertNoServerError($response, 'null relationship probe for ' . $method . ' ' . $uri);
        }
    }

    public function test_mutation_routes_fail_cleanly_when_relationships_are_strings_instead_of_ids(): void
    {
        $routes = array_slice(array_filter($this->apiRoutes(), fn ($route) => in_array($this->primaryMethod($route), ['POST', 'PUT', 'PATCH'], true)), 0, 120);

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();
            $payload = $this->payloadFor($method, $uri) + [
                'student_id' => 'student-' . $this->ids['studentId'],
                'parent_id' => 'parent-' . $this->ids['parentId'],
                'class_section_id' => 'class-' . $this->ids['classSectionId'],
                'invoice_id' => 'invoice-' . $this->ids['invoiceId'],
            ];

            $response = $this->requestAs($this->actorFor($uri), $method, $uri, $payload);

            $this->assertControlled($response, $method, $uri);
            $this->assertNoServerError($response, 'string relationship probe for ' . $method . ' ' . $uri);
        }
    }
}
