<?php

namespace App\Framework\Cache;

final readonly class GeneralCache implements Cache
{
    public function __construct(
        private CacheDriver $adapter
    ) {}

    public function get(string $key): CacheItem
    {
        return $this->adapter->get($key);
    }

    public function set(string $key, mixed $value, ?int $ttl = null): bool
    {
        return $this->adapter->set($key, $value, $ttl);
    }

    public function has(string $key): bool
    {
        return $this->adapter->has($key);
    }

    public function forget(string $key): bool
    {
        return $this->adapter->forget($key);
    }

    public function clear(): bool
    {
        return $this->adapter->clear();
    }

    public function remember(string $key, callable $callback, int $ttl = 3600): CacheItem
    {
        $item = $this->get($key);

        if ($item->isHit) {
            return $item;
        }
        $value = $callback();
        $this->set($key, $value, $ttl);
        return $this->get($key)->isHit ? $this->get($key) : CacheItem::hit($key, $value);

    }
}
