<?php

declare(strict_types=1);

namespace App\Tests\Unit\Administration\Infrastructure\Api\Processor;

use ApiPlatform\Metadata\Put;
use App\Administration\Application\Command\ConfigureGradingMode\ConfigureGradingModeHandler;
use App\Administration\Application\Port\GradeExistenceChecker;
use App\Administration\Domain\Model\SchoolClass\AcademicYearId;
use App\Administration\Domain\Model\SchoolClass\SchoolId;
use App\Administration\Infrastructure\Api\Processor\ConfigureGradingModeProcessor;
use App\Administration\Infrastructure\Api\Resource\GradingModeResource;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryGradingConfigurationRepository;
use App\Administration\Infrastructure\School\SchoolIdResolver;
use App\Administration\Infrastructure\Service\CurrentAcademicYearResolver;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use App\Shared\Infrastructure\Tenant\TenantConfig;
use App\Shared\Infrastructure\Tenant\TenantContext;
use App\Shared\Infrastructure\Tenant\TenantId as InfraTenantId;
use DateTimeImmutable;
use Override;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use Symfony\Component\HttpKernel\Exception\BadRequestHttpException;
use Symfony\Component\HttpKernel\Exception\ConflictHttpException;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
use Symfony\Component\HttpKernel\Exception\UnauthorizedHttpException;
use Symfony\Component\Messenger\Envelope;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Security\Core\Authorization\AccessDecision;
use Symfony\Component\Security\Core\Authorization\AuthorizationCheckerInterface;

final class ConfigureGradingModeProcessorTest extends TestCase
{
    private const string TENANT_UUID = '550e8400-e29b-41d4-a716-446655440001';

    private InMemoryGradingConfigurationRepository $repository;
    private TenantContext $tenantContext;
    private Clock $clock;

    protected function setUp(): void
    {
        $this->repository = new InMemoryGradingConfigurationRepository();
        $this->tenantContext = new TenantContext();
        $this->clock = new class implements Clock {
            #[Override]
            public function now(): DateTimeImmutable
            {
                return new DateTimeImmutable('2025-10-15 10:00:00');
            }
        };
    }

    #[Test]
    public function itRejectsUnauthorizedAccess(): void
    {
        $processor = $this->createProcessor(granted: false);
        $this->setTenant();

        $data = new GradingModeResource();
        $data->mode = 'numeric_20';

        $this->expectException(AccessDeniedHttpException::class);
        $processor->process($data, new Put(), ['academicYearId' => 'current']);
    }

    #[Test]
    public function itRejectsRequestWithoutTenant(): void
    {
        $processor = $this->createProcessor(granted: true);

        $data = new GradingModeResource();
        $data->mode = 'numeric_20';

        $this->expectException(UnauthorizedHttpException::class);
        $processor->process($data, new Put(), ['academicYearId' => 'current']);
    }

    #[Test]
    public function itRejectsInvalidAcademicYearId(): void
    {
        $processor = $this->createProcessor(granted: true);
        $this->setTenant();

        $data = new GradingModeResource();
        $data->mode = 'numeric_20';

        $this->expectException(NotFoundHttpException::class);
        $processor->process($data, new Put(), ['academicYearId' => 'invalid']);
    }

    #[Test]
    public function itConfiguresGradingMode(): void
    {
        $processor = $this->createProcessor(granted: true);
        $this->setTenant();

        $data = new GradingModeResource();
        $data->mode = 'competencies';

        $result = $processor->process($data, new Put(), ['academicYearId' => 'current']);

        self::assertInstanceOf(GradingModeResource::class, $result);
        self::assertSame('competencies', $result->mode);
        self::assertSame('Compétences', $result->label);
        self::assertFalse($result->hasExistingGrades);
    }

    #[Test]
    public function itSetsAvailableModesOnResult(): void
    {
        $processor = $this->createProcessor(granted: true);
        $this->setTenant();

        $data = new GradingModeResource();
        $data->mode = 'numeric_20';

        $result = $processor->process($data, new Put(), ['academicYearId' => 'current']);

        self::assertNotNull($result->availableModes);
        self::assertCount(5, $result->availableModes);
    }

    #[Test]
    public function itThrowsConflictWhenGradesExistAndModeChanges(): void
    {
        // First configure with numeric_20 (no grades)
        $processor = $this->createProcessor(granted: true, hasGrades: false);
        $this->setTenant();

        $data = new GradingModeResource();
        $data->mode = 'numeric_20';
        $processor->process($data, new Put(), ['academicYearId' => 'current']);

        // Now try to change mode with grades existing
        $processorWithGrades = $this->createProcessor(granted: true, hasGrades: true);
        $this->setTenant();

        $data = new GradingModeResource();
        $data->mode = 'competencies';

        $this->expectException(ConflictHttpException::class);
        $processorWithGrades->process($data, new Put(), ['academicYearId' => 'current']);
    }

    #[Test]
    public function itChangesExistingModeWhenNoGradesExist(): void
    {
        $processor = $this->createProcessor(granted: true);
        $this->setTenant();

        $data = new GradingModeResource();
        $data->mode = 'numeric_20';
        $processor->process($data, new Put(), ['academicYearId' => 'current']);

        $data = new GradingModeResource();
        $data->mode = 'letters';
        $result = $processor->process($data, new Put(), ['academicYearId' => 'current']);

        self::assertSame('letters', $result->mode);
    }

    #[Test]
    public function itResolvesNextAcademicYear(): void
    {
        $processor = $this->createProcessor(granted: true);
        $this->setTenant();

        $data = new GradingModeResource();
        $data->mode = 'no_grades';

        $result = $processor->process($data, new Put(), ['academicYearId' => 'next']);

        self::assertSame('no_grades', $result->mode);
    }

    #[Test]
    public function itRejectsInvalidGradingModeWithBadRequest(): void
    {
        $processor = $this->createProcessor(granted: true);
        $this->setTenant();

        $data = new GradingModeResource();
        $data->mode = 'invalid_mode';

        $this->expectException(BadRequestHttpException::class);
        $processor->process($data, new Put(), ['academicYearId' => 'current']);
    }

    private function setTenant(): void
    {
        $this->tenantContext->setCurrentTenant(new TenantConfig(
            tenantId: InfraTenantId::fromString(self::TENANT_UUID),
            subdomain: 'test',
            databaseUrl: 'sqlite:///:memory:',
        ));
    }

    private function createProcessor(bool $granted, bool $hasGrades = false): ConfigureGradingModeProcessor
    {
        $authChecker = new class($granted) implements AuthorizationCheckerInterface {
            public function __construct(private readonly bool $granted)
            {
            }

            public function isGranted(mixed $attribute, mixed $subject = null, ?AccessDecision $accessDecision = null): bool
            {
                return $this->granted;
            }
        };

        $eventBus = new class implements MessageBusInterface {
            public function dispatch(object $message, array $stamps = []): Envelope
            {
                return new Envelope($message);
            }
        };

        $gradeChecker = new class($hasGrades) implements GradeExistenceChecker {
            public function __construct(private readonly bool $hasGrades)
            {
            }

            #[Override]
            public function hasGradesInPeriod(TenantId $tenantId, AcademicYearId $academicYearId, int $periodSequence): bool
            {
                return $this->hasGrades;
            }

            #[Override]
            public function hasGradesForYear(TenantId $tenantId, SchoolId $schoolId, AcademicYearId $academicYearId): bool
            {
                return $this->hasGrades;
            }
        };

        $handler = new ConfigureGradingModeHandler($this->repository, $gradeChecker, $this->clock);
        $resolver = new CurrentAcademicYearResolver($this->tenantContext, $this->clock);
        $schoolIdResolver = new SchoolIdResolver();

        return new ConfigureGradingModeProcessor(
            $handler,
            $gradeChecker,
            $this->tenantContext,
            $eventBus,
            $authChecker,
            $resolver,
            $schoolIdResolver,
        );
    }
}
