namespace DramaLing.Api.Services.Infrastructure;

/// <summary>
/// 統一配置管理服務介面
/// </summary>
public interface IConfigurationService
{
    /// <summary>
    /// 取得 AI 相關配置
    /// </summary>
    Task<AIConfiguration> GetAIConfigurationAsync();

    /// <summary>
    /// 取得認證相關配置
    /// </summary>
    Task<AuthConfiguration> GetAuthConfigurationAsync();

    /// <summary>
    /// 取得外部服務配置
    /// </summary>
    Task<ExternalServicesConfiguration> GetExternalServicesConfigurationAsync();

    /// <summary>
    /// 取得快取配置
    /// </summary>
    Task<CacheConfiguration> GetCacheConfigurationAsync();

    /// <summary>
    /// 檢查配置是否完整
    /// </summary>
    Task<ConfigurationValidationResult> ValidateConfigurationAsync();

    /// <summary>
    /// 取得環境特定配置
    /// </summary>
    T GetEnvironmentConfiguration<T>(string sectionName) where T : class, new();
}

/// <summary>
/// AI 相關配置
/// </summary>
public class AIConfiguration
{
    public string GeminiApiKey { get; set; } = string.Empty;
    public string GeminiModel { get; set; } = "gemini-1.5-flash";
    public int TimeoutSeconds { get; set; } = 30;
    public double Temperature { get; set; } = 0.7;
    public int MaxOutputTokens { get; set; } = 2000;
    public int MaxRetries { get; set; } = 3;
}

/// <summary>
/// 認證相關配置
/// </summary>
public class AuthConfiguration
{
    public string JwtSecret { get; set; } = string.Empty;
    public string SupabaseUrl { get; set; } = string.Empty;
    public string ValidAudience { get; set; } = "authenticated";
    public int ClockSkewMinutes { get; set; } = 5;
}

/// <summary>
/// 外部服務配置
/// </summary>
public class ExternalServicesConfiguration
{
    public AzureSpeechConfiguration AzureSpeech { get; set; } = new();
    public DatabaseConfiguration Database { get; set; } = new();
}

/// <summary>
/// Azure Speech 配置
/// </summary>
public class AzureSpeechConfiguration
{
    public string SubscriptionKey { get; set; } = string.Empty;
    public string Region { get; set; } = string.Empty;
    public bool IsConfigured => !string.IsNullOrEmpty(SubscriptionKey) && !string.IsNullOrEmpty(Region);
}

/// <summary>
/// 資料庫配置
/// </summary>
public class DatabaseConfiguration
{
    public string ConnectionString { get; set; } = string.Empty;
    public bool UseInMemoryDb { get; set; } = false;
    public int CommandTimeoutSeconds { get; set; } = 30;
}

/// <summary>
/// 快取配置
/// </summary>
public class CacheConfiguration
{
    public bool EnableDistributedCache { get; set; } = false;
    public TimeSpan DefaultExpiry { get; set; } = TimeSpan.FromMinutes(10);
    public TimeSpan AnalysisCacheExpiry { get; set; } = TimeSpan.FromHours(2);
    public TimeSpan UserCacheExpiry { get; set; } = TimeSpan.FromMinutes(30);
    public int MaxMemoryCacheSizeMB { get; set; } = 100;
}

/// <summary>
/// 配置驗證結果
/// </summary>
public class ConfigurationValidationResult
{
    public bool IsValid { get; set; }
    public List<string> Errors { get; set; } = new();
    public List<string> Warnings { get; set; } = new();
    public Dictionary<string, object> ConfigurationSummary { get; set; } = new();
}