<?php

declare(strict_types=1);

namespace App\Services\Uploads;

use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Str;

final class UploadTokenService
{
    public function generate(string $sessionId, int $userId): string
    {
        $token = Str::random(64);
        $ttl = (int) config('uploads.tokens.ttl_minutes', 60);

        Cache::put($this->cacheKey($token), [
            'session_id' => $sessionId,
            'user_id' => $userId,
        ], now()->addMinutes($ttl));

        return $token;
    }

    public function get(string $token): ?array
    {
        $data = Cache::get($this->cacheKey($token));

        return is_array($data) ? $data : null;
    }

    private function cacheKey(string $token): string
    {
        return 'uploads:token:' . $token;
    }
}
