<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiBatch16SchemaEvolutionDeprecationContractTest extends FullSurfaceE2EContractCase
{
    public function test_batch16_deprecated_aliases_remain_controlled_and_do_not_return_incompatible_shapes(): void
    {
        $routes = $this->apiRoutesContainingAny(['legacy', 'alias', 'teacher', 'login', 'auth', 'v1']);

        foreach (array_slice($routes, 0, 50) as $route) {
            $method = $this->primaryMethod($route);
            $uri = $route->uri();
            $response = $this->withHeaders([
                'X-Api-Version' => '2019-01-01',
                'Accept' => 'application/json',
            ])->requestAs($this->actorFor($uri), $method, $uri, $this->payloadFor($method, $uri) + [
                'legacy_client' => true,
                'response_shape' => 'old',
            ]);

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

    public function test_batch16_removed_or_deprecated_fields_are_not_required_for_current_clients(): void
    {
        foreach (array_slice($this->apiRoutes(), 0, 80) as $route) {
            $method = $this->primaryMethod($route);
            if (! in_array($method, ['POST', 'PUT', 'PATCH'], true)) {
                continue;
            }

            $uri = $route->uri();
            $payload = $this->payloadFor($method, $uri);
            unset($payload['legacy_id'], $payload['old_student_id'], $payload['deprecated_role']);

            $response = $this->requestAs($this->actorFor($uri), $method, $uri, $payload + [
                'client_contract_version' => 'current',
            ]);
            $this->assertControlled($response, $method, $uri);
        }
    }
}
