<?php

declare(strict_types=1);

namespace App\Tests\Functional\Administration\Api;

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

/**
 * Functional tests for homework rules API endpoints.
 *
 * Validates routing, tenant isolation, authentication, and
 * basic request/response contracts without full auth flow.
 */
final class HomeworkRulesEndpointsTest extends ApiTestCase
{
    protected static ?bool $alwaysBootKernel = true;

    // =========================================================================
    // GET /settings/homework-rules — Without tenant
    // =========================================================================

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

        $client->request('GET', '/api/settings/homework-rules', [
            'headers' => [
                'Host' => 'localhost',
                'Accept' => 'application/json',
            ],
        ]);

        self::assertResponseStatusCodeSame(404);
    }

    // =========================================================================
    // GET /settings/homework-rules — Without authentication (with tenant)
    // =========================================================================

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

        $client->request('GET', 'http://ecole-alpha.classeo.local/api/settings/homework-rules', [
            'headers' => [
                'Accept' => 'application/json',
            ],
        ]);

        self::assertResponseStatusCodeSame(401);
    }

    // =========================================================================
    // PUT /settings/homework-rules — Without tenant
    // =========================================================================

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

        $client->request('PUT', '/api/settings/homework-rules', [
            'headers' => [
                'Host' => 'localhost',
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
            ],
            'json' => [
                'rules' => [],
                'enforcementMode' => 'soft',
                'enabled' => true,
            ],
        ]);

        self::assertResponseStatusCodeSame(404);
    }

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

        $client->request('PUT', 'http://ecole-alpha.classeo.local/api/settings/homework-rules', [
            'headers' => [
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
            ],
            'json' => [
                'rules' => [],
                'enforcementMode' => 'soft',
                'enabled' => true,
            ],
        ]);

        self::assertResponseStatusCodeSame(401);
    }

    // =========================================================================
    // GET /settings/homework-rules/history — Without tenant / auth
    // =========================================================================

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

        $client->request('GET', '/api/settings/homework-rules/history', [
            'headers' => [
                'Host' => 'localhost',
                'Accept' => 'application/json',
            ],
        ]);

        self::assertResponseStatusCodeSame(404);
    }

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

        $client->request('GET', 'http://ecole-alpha.classeo.local/api/settings/homework-rules/history', [
            'headers' => [
                'Accept' => 'application/json',
            ],
        ]);

        self::assertResponseStatusCodeSame(401);
    }

    // =========================================================================
    // Route existence — tenant + no auth proves route exists (401 not 404)
    // =========================================================================

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

        $client->request('GET', 'http://ecole-alpha.classeo.local/api/settings/homework-rules', [
            'headers' => ['Accept' => 'application/json'],
        ]);

        // 401 (no auth) not 404 — proves route is registered
        self::assertResponseStatusCodeSame(401);
    }

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

        $client->request('PUT', 'http://ecole-alpha.classeo.local/api/settings/homework-rules', [
            'headers' => [
                'Accept' => 'application/json',
                'Content-Type' => 'application/json',
            ],
            'json' => [
                'rules' => [],
                'enforcementMode' => 'soft',
                'enabled' => true,
            ],
        ]);

        self::assertResponseStatusCodeSame(401);
    }

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

        $client->request('GET', 'http://ecole-alpha.classeo.local/api/settings/homework-rules/history', [
            'headers' => ['Accept' => 'application/json'],
        ]);

        self::assertResponseStatusCodeSame(401);
    }
}
