<?php

namespace Tests\Unit\Auth\UseCases;

use App\Auth\CreateSessionDto;
use App\Auth\UseCases\Logout\Logout;
use App\Shared\ValueObject\EmailAddress;
use App\User\User;
use DateTimeImmutable;
use DateTimeZone;
use Tests\Fakes\FakeSessionRepository;
use Tests\TestCase;

class LogoutTest extends TestCase
{
    private FakeSessionRepository $sessionRepo;

    private Logout $useCase;

    private DateTimeImmutable $now;

    protected function setUp(): void
    {
        $this->now = new DateTimeImmutable(
            '2026-04-29T12:00:00',
            new DateTimeZone('UTC')
        );
        $this->sessionRepo = new FakeSessionRepository();
        $this->useCase = new Logout($this->sessionRepo);
    }

    public function testExistingTokenSessionIsRemoved(): void
    {
        $this->sessionRepo->create(new CreateSessionDto(
            token: 'token-abc',
            user: new User(
                id: 7,
                email: new EmailAddress('a@b.com'),
                passwordHash: 'password',
            ),
            createdAt: $this->now,
            expiresAt: $this->now->modify('+7 days'),
        ));

        $this->useCase->execute('token-abc');

        $this->assertNull($this->sessionRepo->findByToken('token-abc'));
    }

    public function testUnknownTokenDoesNotThrow(): void
    {
        $this->useCase->execute('unknown-token');

        $this->assertNull($this->sessionRepo->findByToken('unknown-token'));
    }
}
