<?php

declare(strict_types=1);

namespace App\Tests\Functional\Administration\Api;

use ApiPlatform\Symfony\Bundle\Test\ApiTestCase;
use PHPUnit\Framework\Attributes\Test;

/**
 * Tests for grading mode API endpoints.
 *
 * @see Story 2.4 - Configuration Mode de Notation
 */
final class GradingModeEndpointsTest extends ApiTestCase
{
    protected static ?bool $alwaysBootKernel = true;

    // =========================================================================
    // Security - Without tenant
    // =========================================================================

    #[Test]
    public function getGradingModeReturns404WithoutTenant(): void
    {
        $client = static::createClient();

        $client->request('GET', '/api/academic-years/current/grading-mode', [
            'headers' => [
                'Host' => 'localhost',
                'Accept' => 'application/json',
            ],
        ]);

        self::assertResponseStatusCodeSame(404);
    }

    #[Test]
    public function configureGradingModeReturns404WithoutTenant(): void
    {
        $client = static::createClient();

        $client->request('PUT', '/api/academic-years/current/grading-mode', [
            'headers' => [
                'Host' => 'localhost',
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
            ],
            'json' => ['mode' => 'numeric_20'],
        ]);

        self::assertResponseStatusCodeSame(404);
    }

    // =========================================================================
    // Security - Without authentication (with tenant)
    // =========================================================================

    #[Test]
    public function getGradingModeReturns401WithoutAuthentication(): void
    {
        $client = static::createClient();

        $client->request('GET', 'http://ecole-alpha.classeo.local/api/academic-years/current/grading-mode', [
            'headers' => [
                'Accept' => 'application/json',
            ],
        ]);

        self::assertResponseStatusCodeSame(401);
    }

    #[Test]
    public function configureGradingModeReturns401WithoutAuthentication(): void
    {
        $client = static::createClient();

        $client->request('PUT', 'http://ecole-alpha.classeo.local/api/academic-years/current/grading-mode', [
            'headers' => [
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
            ],
            'json' => ['mode' => 'numeric_20'],
        ]);

        self::assertResponseStatusCodeSame(401);
    }

    // =========================================================================
    // Validation - Invalid mode
    // =========================================================================

    #[Test]
    public function configureGradingModeRejectsInvalidModeWithoutTenant(): void
    {
        $client = static::createClient();

        $client->request('PUT', '/api/academic-years/current/grading-mode', [
            'headers' => [
                'Host' => 'localhost',
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
            ],
            'json' => ['mode' => 'invalid_mode'],
        ]);

        // Without tenant, returns 404 before validation kicks in
        self::assertResponseStatusCodeSame(404);
    }

    // =========================================================================
    // Special identifiers - 'current', 'next', 'previous'
    // =========================================================================

    #[Test]
    public function getGradingModeAcceptsCurrentIdentifier(): void
    {
        $client = static::createClient();

        $client->request('GET', 'http://ecole-alpha.classeo.local/api/academic-years/current/grading-mode', [
            'headers' => ['Accept' => 'application/json'],
        ]);

        // 401 (no auth) not 404 (invalid id) — proves 'current' is accepted
        self::assertResponseStatusCodeSame(401);
    }

    #[Test]
    public function getGradingModeAcceptsNextIdentifier(): void
    {
        $client = static::createClient();

        $client->request('GET', 'http://ecole-alpha.classeo.local/api/academic-years/next/grading-mode', [
            'headers' => ['Accept' => 'application/json'],
        ]);

        self::assertResponseStatusCodeSame(401);
    }

    #[Test]
    public function getGradingModeAcceptsPreviousIdentifier(): void
    {
        $client = static::createClient();

        $client->request('GET', 'http://ecole-alpha.classeo.local/api/academic-years/previous/grading-mode', [
            'headers' => ['Accept' => 'application/json'],
        ]);

        self::assertResponseStatusCodeSame(401);
    }
}
