<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiNestedPayloadAndArrayAbuseContractTest extends FullSurfaceE2EContractCase
{
    /** @test */
    public function mutation_routes_fail_cleanly_for_deeply_nested_payloads(): void
    {
        $routes = array_slice(array_filter($this->apiRoutes(), fn ($route) => in_array($this->primaryMethod($route), ['POST', 'PUT', 'PATCH'], true)), 0, 90);

        $nested = ['level_1' => ['level_2' => ['level_3' => ['level_4' => ['level_5' => ['level_6' => ['value' => 'too deep']]]]]]];

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();

            $response = $this->requestAs($this->actorFor($uri), $method, $uri, $this->payloadFor($method, $uri) + $nested);

            $this->assertControlled($response, $method, $uri);
            $this->assertLessThan(500, $response->getStatusCode(), "$method $uri should reject or ignore deep nesting without collapsing dramatically.");
        }
    }

    /** @test */
    public function mutation_routes_fail_cleanly_for_large_sparse_arrays(): void
    {
        $routes = array_slice(array_filter($this->apiRoutes(), fn ($route) => in_array($this->primaryMethod($route), ['POST', 'PUT', 'PATCH'], true)), 0, 90);

        $sparse = [
            'ids' => [0 => $this->ids['studentId'], 1000 => 999999, 9999 => null],
            'students' => array_fill(0, 150, ['id' => 999999, 'status' => 'present']),
            'permissions' => array_fill(0, 80, '*'),
        ];

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();

            $response = $this->requestAs($this->actorFor($uri), $method, $uri, $this->payloadFor($method, $uri) + $sparse);

            $this->assertControlled($response, $method, $uri);
        }
    }
}
