<?php

namespace Tests\Unit\Auth\UseCases;

use App\Auth\Clock;
use App\Auth\UseCases\CreateSession\CreateSession;
use App\Shared\ValueObject\EmailAddress;
use App\User\User;
use DateTimeImmutable;
use Tests\Fakes\FakeClock;
use Tests\Fakes\FakeSessionRepository;
use Tests\Fakes\FakeTokenGenerator;
use Tests\TestCase;

class CreateSessionTest extends TestCase
{
    private FakeSessionRepository $sessionRepo;
    private FakeTokenGenerator $tokenGenerator;
    private Clock $clock;
    private CreateSession $createSession;

    protected function setUp(): void
    {
        $this->sessionRepo = new FakeSessionRepository();
        $this->tokenGenerator = new FakeTokenGenerator();
        $this->clock = new FakeClock(
            new DateTimeImmutable('2026-05-18 12:00:00')
        );

        $this->createSession = new CreateSession(
            $this->sessionRepo,
            $this->tokenGenerator,
            $this->clock
        );
    }

    public function testCreatesSessionForUser(): void
    {
        $email = new EmailAddress('user@example.com');
        $user = new User(1, $email, 'hashed-password');

        $session = $this->createSession->execute($user);

        $this->assertSame('fake-token-123', $session->getToken());
        $this->assertSame($user, $session->getUser());
        $this->assertFalse($session->isExpired($this->clock->now()));
        $this->assertSame('2026-05-18 12:00:00', $session->getCreatedAt()->format('Y-m-d H:i:s'));
        $this->assertSame('2026-05-25 12:00:00', $session->getExpiresAt()->format('Y-m-d H:i:s'));

        $stored = $this->sessionRepo->findByToken($session->getToken());
        $this->assertNotNull($stored);
        $this->assertSame('fake-token-123', $stored->getToken());
    }
}
