<?php

namespace Tests\Feature\Api\V1\FullSurface;

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


class ApiBatch19RevokedDeletedUserTokenBoundaryContractTest extends FullSurfaceE2EContractCase
{
    public function test_batch19_revoked_deleted_user_token_boundary_stays_controlled(): void
    {
        $routes = $this->apiRoutesContainingAny(['auth', 'login', 'logout', 'me', 'users', 'profile', 'session', 'tokens']);
        $payload = [
            'user_id' => 999999,
            'token' => 'revoked-token',
            'deleted_user_id' => 999999,
            'restore_user' => true,
            'reactivate' => true,
            'remember_token' => 'client-forged',
            'access_token' => 'client-forged',
        ];

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

            if (str_contains($uri, 'admin') || str_contains($uri, 'roles') || str_contains($uri, 'permissions') || str_contains($uri, 'backup') || str_contains($uri, 'restore')) {
                $actor = $this->studentUser;
            }

            $response = $this->requestAs($actor, $method, $uri, $this->payloadFor($method, $uri) + $payload);
            $this->assertControlled($response, $method, $uri);
            $this->assertNotEquals(500, $response->getStatusCode(), $method.' '.$uri.' crashed during Batch 19 revoked deleted user token boundaries probe.');

            $body = strtolower($response->getContent());
            foreach (['sqlstate', 'stack trace', 'app_key', 'remember_token', 'api_token', 'massassignmentexception', 'foreign key constraint', 'database.sqlite'] as $leak) {
                $this->assertStringNotContainsString($leak, $body, $method.' '.$uri.' leaked '.$leak.' during Batch 19 revoked deleted user token boundaries probe.');
            }
        }
    }
}
