<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiBatch14ApiConsumerContractCompatibilityTest extends FullSurfaceE2EContractCase
{
public function test_core_identity_contracts_remain_compatible_for_web_mobile_and_api_clients(): void
    {
        foreach (array_slice($this->apiRoutesContainingAny(['login', 'auth/me', 'me', 'frontend', 'profile']), 0, 25) as $route) {
            $method = $this->primaryMethod($route);
            foreach ([['Accept' => 'application/json', 'X-Client' => 'web'], ['Accept' => 'application/json', 'X-Client' => 'mobile', 'X-App-Version' => '1.0.0'], ['Accept' => 'application/vnd.alrahma.v1+json', 'X-Client' => 'api-consumer']] as $headers) {
                $response = $this->withHeaders($headers)->actingAs($this->actorFor($route->uri()), 'api')->json($method, $this->materializePath($route->uri()), $this->payloadFor($method, $route->uri()));
                $this->assertControlled($response, $method, $route->uri());
                $this->assertJsonResponseWhenSuccessful($response, $method . ' ' . $route->uri());
                if ($response->isSuccessful()) { $this->assertStringNotContainsString('undefined index', strtolower($response->getContent())); }
            }
        }
    }

    public function test_successful_login_like_responses_keep_token_and_user_contract_when_present(): void
    {
        foreach (array_slice($this->apiRoutesContainingAny(['login', 'auth/login']), 0, 10) as $route) {
            $method = $this->primaryMethod($route);
            if ($method !== 'POST') { continue; }
            $response = $this->json($method, $this->materializePath($route->uri()), ['email' => $this->admin->email, 'password' => 'password']);
            $this->assertControlled($response, $method, $route->uri());
            if ($response->isSuccessful() && $this->isJsonString($response->getContent())) {
                $data = $response->json();
                $this->assertTrue(isset($data['token']) || isset($data['access_token']), $route->uri() . ' should expose token/access_token when login succeeds.');
                $this->assertArrayHasKey('user', $data, $route->uri() . ' should expose user when login succeeds.');
            }
        }
    }
}
