using DramaLing.Api.Models.DTOs;

namespace DramaLing.Api.Services.AI;

/// <summary>
/// AI 提供商管理器介面，負責選擇和管理多個 AI 提供商
/// </summary>
public interface IAIProviderManager
{
    /// <summary>
    /// 取得最佳 AI 提供商
    /// </summary>
    /// <param name="strategy">選擇策略</param>
    /// <returns>AI 提供商</returns>
    Task<IAIProvider> GetBestProviderAsync(ProviderSelectionStrategy strategy = ProviderSelectionStrategy.Performance);

    /// <summary>
    /// 取得所有可用的提供商
    /// </summary>
    /// <returns>可用提供商列表</returns>
    Task<IEnumerable<IAIProvider>> GetAvailableProvidersAsync();

    /// <summary>
    /// 取得指定名稱的提供商
    /// </summary>
    /// <param name="providerName">提供商名稱</param>
    /// <returns>AI 提供商</returns>
    Task<IAIProvider?> GetProviderByNameAsync(string providerName);

    /// <summary>
    /// 檢查所有提供商的健康狀態
    /// </summary>
    /// <returns>健康狀態報告</returns>
    Task<ProviderHealthReport> CheckAllProvidersHealthAsync();

    /// <summary>
    /// 使用最佳提供商分析句子
    /// </summary>
    /// <param name="inputText">輸入文本</param>
    /// <param name="options">分析選項</param>
    /// <param name="strategy">選擇策略</param>
    /// <returns>分析結果</returns>
    Task<SentenceAnalysisData> AnalyzeSentenceAsync(string inputText, AnalysisOptions options,
        ProviderSelectionStrategy strategy = ProviderSelectionStrategy.Performance);
}

/// <summary>
/// 提供商選擇策略
/// </summary>
public enum ProviderSelectionStrategy
{
    /// <summary>
    /// 基於性能選擇（響應時間）
    /// </summary>
    Performance,

    /// <summary>
    /// 基於成本選擇（最便宜）
    /// </summary>
    Cost,

    /// <summary>
    /// 基於可靠性選擇（成功率）
    /// </summary>
    Reliability,

    /// <summary>
    /// 負載均衡
    /// </summary>
    LoadBalance,

    /// <summary>
    /// 使用主要提供商
    /// </summary>
    Primary
}

/// <summary>
/// 提供商健康狀態報告
/// </summary>
public class ProviderHealthReport
{
    public DateTime CheckedAt { get; set; }
    public int TotalProviders { get; set; }
    public int HealthyProviders { get; set; }
    public List<ProviderHealthInfo> ProviderHealthInfos { get; set; } = new();
}

/// <summary>
/// 提供商健康資訊
/// </summary>
public class ProviderHealthInfo
{
    public string ProviderName { get; set; } = string.Empty;
    public bool IsHealthy { get; set; }
    public int ResponseTimeMs { get; set; }
    public string? ErrorMessage { get; set; }
    public AIProviderStats Stats { get; set; } = new();
}