<?php

namespace OtomaticAi\Utils;

use OtomaticAi\Vendors\Carbon\Carbon;
use OtomaticAi\Vendors\Illuminate\Support\Arr;

class CacheItem
{
    public ?Carbon $cached_at = null;
    public $data = null;

    /**
     * Undocumented function
     *
     * @param integer $seconds
     * @return boolean
     */
    public function isOutdated(int $seconds): bool
    {
        return empty($this->cached_at) || $this->cached_at->lt(Carbon::now()->subSeconds($seconds));
    }

    public function toArray(): array
    {
        return [
            "data" => $this->data,
            "cached_at" => !empty($this->cached_at) ? Carbon::now()->format("Y-m-d H:i:s") : Carbon::now()->subMonth(),
        ];
    }

    /**
     * Undocumented function
     *
     * @param array $value
     * @return self
     */
    static public function make(array $value = []): self
    {
        $item = new self;
        $item->cached_at = Carbon::createFromFormat(
            "Y-m-d H:i:s",
            Arr::get(
                $value,
                "cached_at",
            )
        );

        $item->data = Arr::get($value, "data");

        return $item;
    }
}
