<?php

namespace Tests\Feature\Api\V1\FullSurface;

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

class ApiLegacyCurrentAliasParityExpansionTest extends FullSurfaceE2EContractCase
{
    public function test_auth_aliases_return_compatible_login_shapes(): void
    {
        foreach ($this->loginAliasPaths() as $path) {
            $response = $this->json('POST', $path, [
                'email' => $this->admin->email,
                'password' => 'password',
            ]);

            $this->assertStatusIn($response, [200, 201, 302, 400, 401, 403, 404, 405, 419, 422], "POST $path login alias");
            $this->assertNoServerError($response, "POST $path login alias");

            if ($response->getStatusCode() === 200 && $this->isJsonString($response->getContent())) {
                $json = $response->json();
                $this->assertTrue(
                    array_key_exists('token', $json) || array_key_exists('access_token', $json) || array_key_exists('user', $json) || array_key_exists('status', $json),
                    "$path successful login alias must expose auth-compatible JSON."
                );
            }
        }
    }

    public function test_legacy_teacher_aliases_and_current_v1_teacher_routes_have_compatible_denial_behavior(): void
    {
        foreach ($this->teacherAliasPairs() as [$legacy, $current]) {
            $legacyResponse = $this->requestAs($this->studentUser, 'GET', $legacy);
            $currentResponse = $this->requestAs($this->studentUser, 'GET', $current);

            $this->assertStatusIn($legacyResponse, [302, 401, 403, 404, 405, 419, 422], "student denied legacy $legacy");
            $this->assertStatusIn($currentResponse, [302, 401, 403, 404, 405, 419, 422], "student denied current $current");
            $this->assertNoServerError($legacyResponse, "student denied legacy $legacy");
            $this->assertNoServerError($currentResponse, "student denied current $current");
        }
    }

    public function test_legacy_non_versioned_routes_do_not_drift_into_5xx(): void
    {
        foreach ($this->legacyProbePaths() as [$method, $path]) {
            $response = $this->requestAs($this->actorFor($path), $method, $path, $this->payloadFor($method, $path));

            $this->assertStatusIn($response, [200, 201, 202, 204, 302, 400, 401, 403, 404, 405, 409, 419, 422], "$method $path legacy alias");
            $this->assertNoServerError($response, "$method $path legacy alias");
        }
    }

    /** @return list<string> */
    private function loginAliasPaths(): array
    {
        return ['/api/v1/auth/login', '/api/login', '/user/login'];
    }

    /** @return list<array{string,string}> */
    private function teacherAliasPairs(): array
    {
        return [
            ['api/teacher/classes', 'api/v1/teacher/classes'],
            ['api/teacher/attendance', 'api/v1/teacher/attendance'],
            ['api/teacher/homework', 'api/v1/teacher/homework'],
        ];
    }

    /** @return list<array{string,string}> */
    private function legacyProbePaths(): array
    {
        return [
            ['GET', 'api/frontend/identity'],
            ['GET', 'api/docs'],
            ['GET', 'api/health'],
            ['GET', 'api/db-check'],
            ['POST', 'api/logout'],
            ['GET', 'api/user'],
            ['POST', 'api/teacher/homework'],
        ];
    }
}
