using Microsoft.Extensions.Options;
using TakeoutSaaS.Module.Storage.Options;

namespace TakeoutSaaS.Module.Storage.Providers;

/// <summary>
/// 腾讯云 COS 存储提供商实现。
/// </summary>
public sealed class TencentCosStorageProvider(IOptionsMonitor<StorageOptions> optionsMonitor)
    : S3StorageProviderBase
{
    private StorageOptions CurrentOptions => optionsMonitor.CurrentValue;

    /// <inheritdoc />
    public override StorageProviderKind Kind => StorageProviderKind.TencentCos;

    /// <inheritdoc />
    protected override string Bucket => CurrentOptions.TencentCos.Bucket;

    /// <inheritdoc />
    protected override string ServiceUrl => string.IsNullOrWhiteSpace(CurrentOptions.TencentCos.Endpoint)
        ? $"{(CurrentOptions.TencentCos.UseHttps ? "https" : "http")}://cos.{CurrentOptions.TencentCos.Region}.myqcloud.com"
        : CurrentOptions.TencentCos.Endpoint!;

    /// <inheritdoc />
    protected override string AccessKey => CurrentOptions.TencentCos.SecretId;

    /// <inheritdoc />
    protected override string SecretKey => CurrentOptions.TencentCos.SecretKey;

    /// <inheritdoc />
    protected override bool UseHttps => CurrentOptions.TencentCos.UseHttps;

    /// <inheritdoc />
    protected override bool ForcePathStyle => CurrentOptions.TencentCos.ForcePathStyle;

    /// <inheritdoc />
    protected override string? CdnBaseUrl => !string.IsNullOrWhiteSpace(CurrentOptions.TencentCos.CdnBaseUrl)
        ? CurrentOptions.TencentCos.CdnBaseUrl
        : CurrentOptions.CdnBaseUrl;

    /// <inheritdoc />
    protected override TimeSpan SignedUrlExpiry =>
        TimeSpan.FromMinutes(Math.Max(1, CurrentOptions.Security.DefaultUrlExpirationMinutes));
}
