<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiBatch16OptimisticLockingAndVersionFieldContractTest extends FullSurfaceE2EContractCase
{
    public function test_batch16_version_and_lock_fields_cannot_be_forged_to_override_newer_records(): void
    {
        $routes = $this->apiRoutesContainingAny(['students', 'users', 'attendance', 'scores', 'finance', 'inventory', 'settings', 'profile']);

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

            $uri = $route->uri();
            $response = $this->withHeaders([
                'If-Match' => '"stale-batch16-etag"',
                'If-Unmodified-Since' => 'Mon, 01 Jan 1990 00:00:00 GMT',
            ])->requestAs($this->actorFor($uri), $method, $uri, $this->payloadFor($method, $uri) + [
                'version' => -1,
                'lock_version' => -999,
                'updated_at' => '1990-01-01T00:00:00Z',
                'force_overwrite' => true,
                'ignore_conflicts' => true,
            ]);

            $this->assertControlled($response, $method, $uri);
            $this->assertStringNotContainsString('optimisticlockexception', strtolower($response->getContent()));
        }
    }
}
