<?php

namespace Oney\ThreeByFour\Model;

use Oney\ThreeByFour\Api\CacheInterface as OneyCache;
use Magento\Framework\App\CacheInterface;
use Oney\ThreeByFour\Logger\Logger;

/**
 * Cache Model
 * 
 * Handles caching operations for Oney ThreeByFour module.
 * Provides methods to save, load, and manage cached data with logging.
 * 
 * @package Oney\ThreeByFour\Model
 * @version 1.0.0
 * @author Oney
 * @since 1.0.0
 */
class Cache implements OneyCache
{
    /**
     * Cache lifetime in seconds (24 hours)
     * 
     * @const int
     * @version 1.0.0
     */
    const LIFETIME = 86400;
    
    /**
     * Magento cache manager instance
     * 
     * @var CacheInterface
     * @version 1.0.0
     */
    protected $cacheManager;
    
    /**
     * Cache type identifier
     * 
     * @var string
     * @version 1.0.0
     */
    protected $cache = "";
    
    /**
     * Oney country code
     * 
     * @var string
     * @version 1.0.0
     */
    protected $oneyCountry = "";
    
    /**
     * Specifications data
     * 
     * @var string
     * @version 1.0.0
     */
    protected $specs = "";
    
    /**
     * Logger instance
     * 
     * @var Logger
     * @version 1.0.0
     */
    protected $logger;

    /**
     * Constructor
     * 
     * @param CacheInterface $cacheManager Magento cache manager
     * @param Logger $logger Logger instance
     * @version 1.0.0
     */
    public function __construct(
        CacheInterface $cacheManager,
        Logger $logger
    ) {
        $this->cacheManager = $cacheManager;
        $this->logger = $logger;
    }

    /**
     * Save data to cache
     * 
     * @param mixed $data Data to cache
     * @return bool True if saved successfully
     * @version 1.0.0
     */
    public function save($data): bool
    {
        $this->logger->info("[SUCCESS] [CACHE] Saved in cache", [
            'key' => "oney/" . $this->oneyCountry . "/" . $this->cache,
            'value' => $data
        ]);
        return $this->cacheManager->save(json_encode($data),
            "oney/". $this->oneyCountry ."/". $this->cache,
            ["Oney"],
            self::LIFETIME
        );
    }

    /**
     * Load data from cache
     * 
     * @return array Cached data or empty array if not found
     * @version 1.0.0
     */
    public function load(): array
    {
         $data = $this->cacheManager->load("oney/". $this->oneyCountry  ."/". $this->cache);
        if($data) {
            if(is_array($data)){
                $this->logger->info("[SUCCESS] [CACHE] Requested from cache", [
                    'key' => "oney/" . $this->oneyCountry . "/" . $this->cache,
                    'value' => $data
                ]);
                return $data;
            }
            $this->logger->info("[SUCCESS] [CACHE] Requested from cache", [
                'key' => "oney/" . $this->oneyCountry . "/" . $this->cache,
                'value' => json_decode($data, true)
            ]);
            return json_decode($data, true);
        }
        return [];
    }

    /**
     * Set cache type
     * 
     * @param string $type Cache type identifier
     * @return $this
     * @version 1.0.0
     */
    public function setCache($type)
    {
        $this->cache = $type;
        return $this;
    }

    /**
     * Set Oney country code
     * 
     * @param string $country Country code
     * @return $this
     * @version 1.0.0
     */
    public function setOneyCountry($country)
    {
        $this->oneyCountry = $country;
        return $this;
    }
}
