<?php
namespace WorkbloomERP\Utils;

use WorkbloomERP\Services\CacheService;
use WorkbloomERP\Exceptions\AppException;

class CacheUtil {
    public static function set(string $key, int $ttl, mixed $value): bool {
        return CacheService::set(
            key: $key,
            value: is_string($value) ? $value : json_encode(
                value: match (true) {
                    is_array($value), is_object($value) => $value,
                    default => throw new AppException(message: 'Valor para cache deve ser string, array ou objeto.', code: 400)
                },
                flags: JSON_UNESCAPED_UNICODE
            ),
            ttl: max(-1, $ttl)
        );
    }

    public static function get(string $key): ?array {
        $cachedValue = CacheService::get(key: $key);
        return $cachedValue ? json_decode($cachedValue, true) : null;
    }

    public static function delete(array $keys): bool {
        return CacheService::del(keys: $keys);
    }
}