using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
//using Minio;
using Minio.AspNetCore;

namespace Modules.Media.Api;

public static class ServiceCollectionExtensions
{
    public static IServiceCollection AddMediaStorage(this IServiceCollection services, IConfigurationManager configuration, string fileStorageSettingsSection)
    {
        var endpoint = configuration.GetValue<string>(string.Concat(fileStorageSettingsSection, ":Endpoint"));
        if (string.IsNullOrWhiteSpace(endpoint))
        {
            throw new Exception("Data store [Endpoint] is not specified");
        }

        var accessKey = configuration.GetValue<string>(string.Concat(fileStorageSettingsSection, ":AccessKey"));
        if (string.IsNullOrWhiteSpace(accessKey))
        {
            throw new Exception("Data store [AccessKey] is not specified");
        }

        var secretKey = configuration.GetValue<string>(string.Concat(fileStorageSettingsSection, ":SecretKey"));
        if (string.IsNullOrWhiteSpace(secretKey))
        {
            throw new Exception("Data store [SecretKey] is not specified");
        }

        services.AddMinio(options =>
        {
            options.Endpoint = endpoint;
            options.AccessKey = accessKey;
            options.SecretKey = secretKey;
            //.WithSSL()
            //.Build()
        });

        services.AddMediatR(q => q.RegisterServicesFromAssemblies(AppDomain.CurrentDomain.GetAssemblies()));

        return services;
    }
}