<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiBatch12BulkMutationBoundaryContractTest extends FullSurfaceE2EContractCase
{
    public function test_bulk_mutation_routes_reject_empty_and_cross_domain_targets_cleanly(): void
    {
        $routes = array_slice($this->apiRoutesContainingAny(['bulk', 'batch', 'assign', 'remove', 'delete', 'archive', 'restore', 'import']), 0, 140);

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            if (! in_array($method, ['POST', 'PUT', 'PATCH', 'DELETE'], true)) {
                continue;
            }

            foreach ([
                ['ids' => []],
                ['ids' => [$this->ids['studentId'], $this->ids['invoiceId'], $this->ids['classSectionId']]],
                ['student_ids' => [$this->ids['invoiceId']], 'invoice_ids' => [$this->ids['studentId']]],
            ] as $payload) {
                $response = $this->requestAs($this->actorFor($route->uri()), $method, $route->uri(), $this->payloadFor($method, $route->uri()) + $payload);
                $this->assertControlled($response, $method, $route->uri());
                $this->assertNoServerError($response, 'bulk boundary '.$method.' '.$route->uri());
            }
        }
    }

    public function test_bulk_routes_do_not_accept_unbounded_target_lists(): void
    {
        $routes = array_slice($this->apiRoutesContainingAny(['bulk', 'batch', 'assign', 'import']), 0, 100);
        $ids = range(1, 750);

        foreach ($routes as $route) {
            $method = $this->primaryMethod($route);
            if (! in_array($method, ['POST', 'PUT', 'PATCH'], true)) {
                continue;
            }

            $response = $this->requestAs($this->actorFor($route->uri()), $method, $route->uri(), $this->payloadFor($method, $route->uri()) + ['ids' => $ids, 'student_ids' => $ids]);
            $this->assertControlled($response, $method, $route->uri());
            $this->assertNoServerError($response, 'unbounded bulk list '.$method.' '.$route->uri());
        }
    }
}
