<?php

namespace Kylin\Storage\Adapters;

use Kylin\Storage\Contracts\StorageAdapterInterface;

/**
 * 百度云 BOS 适配器
 * 百度 BOS 支持 S3 兼容协议，复用 S3Adapter 实现
 * 文档：https://cloud.baidu.com/doc/BOS/s/Fk1rn8sln
 */
class BaiduAdapter implements StorageAdapterInterface
{
    private S3Adapter $s3Adapter;

    public function __construct(array $config)
    {
        // 百度 BOS S3 兼容端点格式：https://s3.bj.bcebos.com
        $s3Config = [
            'access_key' => $config['access_key'],
            'secret_key' => $config['secret_key'],
            'bucket' => $config['bucket'],
            'region' => $config['region'] ?? 'bj',
            'endpoint' => $config['endpoint'] ?? "https://s3.{$config['region']}.bcebos.com",
            'domain' => $config['domain'] ?? '',
            'path_style' => false,
            'verify_ssl' => true,
        ];

        $this->s3Adapter = new S3Adapter($s3Config);
    }

    public function upload(string $localPath, string $remotePath, array $options = []): string
    {
        return $this->s3Adapter->upload($localPath, $remotePath, $options);
    }

    public function download(string $remotePath, ?string $localPath = null): string
    {
        return $this->s3Adapter->download($remotePath, $localPath);
    }

    public function delete(string $remotePath): bool
    {
        return $this->s3Adapter->delete($remotePath);
    }

    public function getUrl(string $remotePath): string
    {
        return $this->s3Adapter->getUrl($remotePath);
    }

    public function getSignedUrl(string $remotePath, int $expires = 3600): string
    {
        return $this->s3Adapter->getSignedUrl($remotePath, $expires);
    }

    public function exists(string $remotePath): bool
    {
        return $this->s3Adapter->exists($remotePath);
    }

    public function getSize(string $remotePath): int
    {
        return $this->s3Adapter->getSize($remotePath);
    }

    public function getMimeType(string $remotePath): string
    {
        return $this->s3Adapter->getMimeType($remotePath);
    }
}
