using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using TakeoutSaaS.Application.Dictionary.Abstractions;
using TakeoutSaaS.Domain.Dictionary.Repositories;
using TakeoutSaaS.Domain.SystemParameters.Repositories;
using TakeoutSaaS.Infrastructure.Common.Extensions;
using TakeoutSaaS.Infrastructure.Dictionary.Options;
using TakeoutSaaS.Infrastructure.Dictionary.Persistence;
using TakeoutSaaS.Infrastructure.Dictionary.Repositories;
using TakeoutSaaS.Infrastructure.Dictionary.Services;
using TakeoutSaaS.Shared.Abstractions.Constants;

namespace TakeoutSaaS.Infrastructure.Dictionary.Extensions;

/// <summary>
/// 字典基础设施注册扩展。
/// </summary>
public static class DictionaryServiceCollectionExtensions
{
    /// <summary>
    /// 注册字典模块基础设施。
    /// </summary>
    /// <param name="services">服务集合。</param>
    /// <param name="configuration">配置源。</param>
    /// <returns>服务集合。</returns>
    /// <exception cref="InvalidOperationException">缺少数据库配置时抛出。</exception>
    public static IServiceCollection AddDictionaryInfrastructure(this IServiceCollection services, IConfiguration configuration)
    {
        services.AddDatabaseInfrastructure(configuration);
        services.AddPostgresDbContext<DictionaryDbContext>(DatabaseConstants.DictionaryDataSource);

        services.AddScoped<IDictionaryRepository, EfDictionaryRepository>();
        services.AddScoped<ISystemParameterRepository, EfSystemParameterRepository>();
        services.AddScoped<IDictionaryCache, DistributedDictionaryCache>();

        services.AddOptions<DictionaryCacheOptions>()
            .Bind(configuration.GetSection("Dictionary:Cache"))
            .ValidateDataAnnotations();

        return services;
    }

    /// <summary>
    /// 确保数据库连接已配置（Database 节或 ConnectionStrings）。
    /// </summary>
    /// <param name="configuration">配置源。</param>
    /// <param name="dataSourceName">数据源名称。</param>
    /// <exception cref="InvalidOperationException">未配置时抛出。</exception>
    private static void EnsureDatabaseConnectionConfigured(IConfiguration configuration, string dataSourceName)
    {
        // 保留兼容接口，当前逻辑在 DatabaseConnectionFactory 中兜底并记录日志。
    }
}
