<?php

use Symfony\Component\Cache\Adapter\FilesystemAdapter;

class Chilexpress_Oficial_Utils {

    private $cache;

    public function __construct() {
        $cachePath = defined('_PS_CACHE_DIR_') ? _PS_CACHE_DIR_ : sys_get_temp_dir();
        $this->cache = new FilesystemAdapter('chilexpress', 300, $cachePath);
    }

    public function set_cached_cotizacion($md5_payload, $response_quote) {
        $quote = $this->cache->getItem($md5_payload);
        if (!$quote->isHit()) {
            $quote->set($response_quote);
            $quote->expiresAfter(300); // Forzar 5 minutos
            $this->cache->save($quote);
        }
    }   

    public function get_cached_cotizacion($md5_payload) {
        $response = $this->cache->getItem($md5_payload)->get();
        return $response;
    }

    /**
     * Log messages for debugging.
     *
     * @param string $message The message to log.
     */
    public function write_log($message) {
        $prefix = '[Chilexpress Oficial] ';
        if (is_array($message) || is_object($message)) {
            error_log($prefix . print_r($message, true));
        } else {
            error_log($prefix . $message);
        }
    }
}
