<?php
// +----------------------------------------------------------------------
// | onezan-admin 快速开发的管理平台
// +----------------------------------------------------------------------
// | 官方网址：https://www.onezan.cn
// +----------------------------------------------------------------------
// | onezan团队 版权所有 开源版本可自由商用
// +----------------------------------------------------------------------
// | Author: 543885307@qq.com
// +----------------------------------------------------------------------
namespace app\service\domain\storage;

use Core\service\BaseCoreService;
use app\service\domain\storage\gateway\AliyunOssGateway;
use app\service\domain\storage\gateway\LocalStorageGateway;
use app\service\domain\storage\gateway\QiniuKodoGateway;
use app\service\domain\storage\gateway\StorageGatewayInterface;
use app\service\domain\storage\gateway\TencentCosGateway;
use think\App;


/**
 * Core Storage Service
 * @package app\service\domain\storage
 */
class CoreStorageService extends BaseCoreService
{

    private array $gateways = [];

    private StorageConfigService $config;

    /**
     * 构造函数
     * @param App $app 应用实例
     * @param StorageConfigService $config 存储配置服务
     */
    public function __construct(App $app, StorageConfigService $config)
    {
        parent::__construct($app);
        $this->config = $config;
        $this->registerDefaultGateways();
    }

    /**
     * 注册默认存储网关
     */
    private function registerDefaultGateways(): void
    {
        $this->registerAll([
            new LocalStorageGateway(),
            new AliyunOssGateway($this->app, $this->config),
            new TencentCosGateway($this->app, $this->config),
            new QiniuKodoGateway($this->app, $this->config),
        ]);
    }

    /**
     * 注册存储网关
     * @param StorageGatewayInterface $gateway 存储网关实例
     * @return self
     */
    public function register(StorageGatewayInterface $gateway): self
    {
        $this->gateways[$gateway->provider()] = $gateway;
        return $this;
    }

    /**
     * 注册多个存储网关
     * @param array $gateways 存储网关实例数组
     * @return self
     */
    public function registerAll(array $gateways): self
    {
        foreach ($gateways as $gateway) {
            $this->register($gateway);
        }
        return $this;
    }


    /**
     * 获取当前存储网关
     * @return StorageGatewayInterface 当前存储网关实例
     */
    public function current(): StorageGatewayInterface
    {
        $driver = $this->config->driver();
        $gateway = $this->gateways[$driver] ?? null;


        if (!$gateway || !$gateway->isConfigured()) {
            return $this->gateways['local'];
        }

        return $gateway;
    }

    /**
     * 上传文件到存储存储网关
     * @param string $localPath 本地文件路径
     * @param string $remotePath 远程文件路径
     * @param array $options 上传选项
     * @return array 上传结果
     */
    public function upload(string $localPath, string $remotePath, array $options = []): array
    {
        return $this->current()->upload($localPath, $remotePath, $options);
    }

    /**
     * 删除从存储存储网关
     * @param string $remotePath 远程文件路径
     * @return bool 是否删除成功
     */
    public function delete(string $remotePath): bool
    {
        return $this->current()->delete($remotePath);
    }

    /**
     * 获取文件URL
     * @param string $remotePath 远程文件路径
     * @param int $expireSeconds 过期时间（秒）
     * @return string 文件URL
     */
    public function url(string $remotePath, int $expireSeconds = 0): string
    {
        return $this->current()->url($remotePath, $expireSeconds);
    }


    /**
     * 获取所有注册的存储网关
     * @return array 所有注册的存储网关数组
     */
    public function providers(): array
    {
        $items = [];
        foreach ($this->gateways as $provider => $gateway) {
            $items[] = [
                'key' => $provider,
                'name' => $gateway->name(),
                'configured' => $gateway->isConfigured(),
            ];
        }
        return $items;
    }
}