<?php

namespace ZenoChatbot\Helper;

class Resources
{
    /**
     * @return string
     */
    public static function getBaseDirPath()
    {
        $upload_dir = wp_upload_dir();
        $plugin_dir = $upload_dir['basedir'] . '/zeno-chatbot-ai';

        if (!file_exists($plugin_dir)) {
            wp_mkdir_p($plugin_dir);
        }

        return $plugin_dir;
    }

    /**
     * @return string
     */
    public static function getCacheDirPath()
    {
        $cache_dir = static::getBaseDirPath() . '/cache';

        if (!file_exists($cache_dir)) {
            wp_mkdir_p($cache_dir);
        }

        return $cache_dir;
    }

    public static function getResources($files = [])
    {
        $expectMultiples = is_array($files);

        if(!is_array($files)) {
            $files = [$files];
        }

        $plugin_dir = static::getBaseDirPath();

        $resources = [];

        foreach ($files as $file) {

            $file_path = $plugin_dir . '/' . $file;

            if (file_exists($file_path)) {
                $resources[] = [
                    'name' => $file,
                    'path' => $file_path,
                    'type' => 'application/json'
                ];
            }
        }

        return $expectMultiples ? $resources : $resources[0] ?? null;
    }
}
