<?php

declare(strict_types=1);

namespace App\Tests\Unit\Administration\Application\Command\ResendParentInvitation;

use App\Administration\Application\Command\ResendParentInvitation\ResendParentInvitationCommand;
use App\Administration\Application\Command\ResendParentInvitation\ResendParentInvitationHandler;
use App\Administration\Application\Service\InvitationCodeGenerator;
use App\Administration\Domain\Model\Invitation\InvitationCode;
use App\Administration\Domain\Model\Invitation\InvitationStatus;
use App\Administration\Domain\Model\Invitation\ParentInvitation;
use App\Administration\Domain\Model\User\Email;
use App\Administration\Domain\Model\User\UserId;
use App\Administration\Infrastructure\Persistence\InMemory\InMemoryParentInvitationRepository;
use App\Shared\Domain\Clock;
use App\Shared\Domain\Tenant\TenantId;
use DateTimeImmutable;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;

final class ResendParentInvitationHandlerTest extends TestCase
{
    private const string TENANT_ID = '550e8400-e29b-41d4-a716-446655440002';

    private InMemoryParentInvitationRepository $repository;
    private ResendParentInvitationHandler $handler;

    protected function setUp(): void
    {
        $this->repository = new InMemoryParentInvitationRepository();

        $clock = new class implements Clock {
            public function now(): DateTimeImmutable
            {
                return new DateTimeImmutable('2026-02-14 10:00:00');
            }
        };

        $this->handler = new ResendParentInvitationHandler(
            $this->repository,
            new InvitationCodeGenerator(),
            $clock,
        );
    }

    #[Test]
    public function itResendsInvitationWithNewCode(): void
    {
        $invitation = $this->createSentInvitation();
        $oldCode = (string) $invitation->code;

        $result = ($this->handler)(new ResendParentInvitationCommand(
            invitationId: (string) $invitation->id,
            tenantId: self::TENANT_ID,
        ));

        self::assertSame(InvitationStatus::SENT, $result->status);
        self::assertNotSame($oldCode, (string) $result->code);
    }

    #[Test]
    public function itUpdatesExpirationDate(): void
    {
        $invitation = $this->createSentInvitation();
        $oldExpiresAt = $invitation->expiresAt;

        $result = ($this->handler)(new ResendParentInvitationCommand(
            invitationId: (string) $invitation->id,
            tenantId: self::TENANT_ID,
        ));

        self::assertGreaterThan($oldExpiresAt, $result->expiresAt);
    }

    private function createSentInvitation(): ParentInvitation
    {
        $invitation = ParentInvitation::creer(
            tenantId: TenantId::fromString(self::TENANT_ID),
            studentId: UserId::generate(),
            parentEmail: new Email('parent@example.com'),
            code: new InvitationCode(str_repeat('a', 32)),
            createdAt: new DateTimeImmutable('2026-02-07 10:00:00'),
            createdBy: UserId::generate(),
        );
        $invitation->envoyer(new DateTimeImmutable('2026-02-07 10:00:00'));
        $invitation->pullDomainEvents();
        $this->repository->save($invitation);

        return $invitation;
    }
}
