<?php

namespace App\Auth\UseCases\CreateSession;

use App\Auth\Clock;
use App\Auth\CreateSessionDto;
use App\Auth\Session;
use App\Auth\SessionRepository;
use App\Auth\TokenGenerator;
use App\User\User;

class CreateSession
{
    private const SESSION_LIFETIME = '+7 days';

    public function __construct(
        private SessionRepository $sessionRepo,
        private TokenGenerator $tokenGenerator,
        private Clock $clock,
    ) {
    }

    public function execute(User $user): Session
    {
        $now = $this->clock->now();
        $expiresAt = $now->modify(self::SESSION_LIFETIME);

        return $this->sessionRepo->create(new CreateSessionDto(
            token: $this->tokenGenerator->generate(),
            user: $user,
            createdAt: $now,
            expiresAt: $expiresAt,
        ));
    }
}
