<?php

namespace Tests\Fakes;

use App\Auth\CreateSessionDto;
use App\Auth\Session;
use App\Auth\SessionRepository;
use DateTimeImmutable;

class FakeSessionRepository implements SessionRepository
{
    private array $sessionsByToken = [];

    public function create(CreateSessionDto $dto): Session
    {
        $session = new Session(
            token: $dto->token,
            user: $dto->user,
            createdAt: $dto->createdAt,
            expiresAt: $dto->expiresAt,
        );
        $this->sessionsByToken[$dto->token] = $session;

        return $session;
    }

    public function findByToken(string $token): ?Session
    {
        $session = $this->sessionsByToken[$token] ?? null;
        if ($session === null) {
            return null;
        }

        return new Session(
            token: $session->getToken(),
            user: $session->getUser(),
            createdAt: $session->getCreatedAt(),
            expiresAt: $session->getExpiresAt(),
        );
    }

    public function deleteByToken(string $token): void
    {
        unset($this->sessionsByToken[$token]);
    }
}
