<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiBatch16EventualConsistencyAndRetryContractTest extends FullSurfaceE2EContractCase
{
    public function test_batch16_retry_headers_and_client_operation_ids_are_scoped_per_actor_and_route(): void
    {
        $routes = $this->apiRoutesContainingAny(['create', 'store', 'send', 'dispatch', 'notify', 'payment', 'attendance', 'import', 'export']);

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

            foreach ([$this->admin, $this->parent] as $actor) {
                $response = $this->withHeaders([
                    'Idempotency-Key' => 'batch16-shared-retry-key',
                    'X-Client-Operation-Id' => 'batch16-shared-operation',
                    'X-Retry-Attempt' => '2',
                ])->requestAs($actor, $method, $uri, $this->payloadFor($method, $uri) + [
                    'client_operation_id' => 'batch16-shared-operation',
                    'retry_of' => 'batch16-original-operation',
                ]);

                $this->assertControlled($response, $method, $uri);
                $this->assertStringNotContainsString('duplicate key value violates unique constraint', strtolower($response->getContent()));
            }
        }
    }
}
