<?php

declare(strict_types=1);

namespace App\Framework\Cache;

final readonly class LoggingCacheDecorator implements Cache
{
    public function __construct(
        private Cache $innerCache,
    ) {}

    public function get(string $key): CacheItem
    {
        $cacheItem = $this->innerCache->get($key);
        $status = $cacheItem->isHit ? 'HIT' : 'MISS';
        $valueType = $cacheItem->value === null ? 'NULL' : gettype($cacheItem->value);
        error_log("Cache {$status}: {$key} (value: {$valueType})");
        return $cacheItem;
    }

    public function set(string $key, mixed $value, ?int $ttl = null): bool
    {
        $result = $this->innerCache->set($key, $value, $ttl);
        $valueType = $value === null ? 'NULL' : gettype($value);
        $ttlStr = $ttl === null ? 'default' : (string)$ttl;
        $success = $result ? 'YES' : 'NO';
        error_log("Cache SET: {$key} = {$valueType}, TTL: {$ttlStr}, Success: {$success}");
        return $result;
    }

    public function has(string $key): bool
    {
        $exists = $this->innerCache->has($key);
        $existsStr = $exists ? 'YES' : 'NO';
        error_log("Cache HAS: {$key} = {$existsStr}");
        return $exists;
    }

    public function forget(string $key): bool
    {
        $result = $this->innerCache->forget($key);
        $success = $result ? 'YES' : 'NO';
        error_log("Cache FORGET: {$key}, Success: {$success}");
        return $result;
    }

    public function clear(): bool
    {
        $result = $this->innerCache->clear();
        $success = $result ? 'YES' : 'NO';
        error_log("Cache CLEAR: Success: {$success}");
        return $result;
    }

    public function getMultiple(array $keys): array
    {
        $items = $this->innerCache->getMultiple($keys);
        $hitCount = 0;
        $missCount = 0;

        foreach ($items as $item) {
            if ($item->isHit) {
                $hitCount++;
            } else {
                $missCount++;
            }
        }

        $keyList = implode(', ', $keys);
        error_log("Cache GET_MULTIPLE: [{$keyList}] - Hits: {$hitCount}, Misses: {$missCount}");
        return $items;
    }

    public function setMultiple(array $items, ?int $ttl = null): bool
    {
        $result = $this->innerCache->setMultiple($items, $ttl);
        $count = count($items);
        $ttlStr = $ttl === null ? 'default' : (string)$ttl;
        $success = $result ? 'YES' : 'NO';
        error_log("Cache SET_MULTIPLE: {$count} items, TTL: {$ttlStr}, Success: {$success}");
        return $result;
    }

    public function deleteMultiple(array $keys): bool
    {
        $result = $this->innerCache->deleteMultiple($keys);
        $count = count($keys);
        $success = $result ? 'YES' : 'NO';
        error_log("Cache DELETE_MULTIPLE: {$count} keys, Success: {$success}");
        return $result;
    }


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

        $success = $result->isHit ? 'YES' : 'NO';
        error_log("Cache REMEMBER: {$key}, Success: {$success}");
        return $result;
    }
}
