namespace ChatService.Domain.Contracts;

/// <summary>
/// EN: Interface for AI service integration (OpenAI, Azure OpenAI, etc.).
/// VI: Interface cho tích hợp AI service (OpenAI, Azure OpenAI, v.v.).
/// </summary>
public interface IAIService
{
    /// <summary>
    /// EN: Stream AI response for a given prompt with conversation context.
    /// VI: Stream AI response cho prompt với context cuộc hội thoại.
    /// </summary>
    /// <param name="prompt">User prompt / Prompt từ user</param>
    /// <param name="conversationHistory">Previous messages for context / Tin nhắn trước đó cho context</param>
    /// <param name="ct">Cancellation token</param>
    /// <returns>Async stream of response chunks / Stream async các phần response</returns>
    IAsyncEnumerable<string> StreamResponseAsync(
        string prompt,
        IEnumerable<ChatMessage> conversationHistory,
        CancellationToken ct = default);

    /// <summary>
    /// EN: Get a complete AI response (non-streaming).
    /// VI: Lấy AI response hoàn chỉnh (không streaming).
    /// </summary>
    Task<string> GetResponseAsync(
        string prompt,
        IEnumerable<ChatMessage> conversationHistory,
        CancellationToken ct = default);

    /// <summary>
    /// EN: Check if AI service is available.
    /// VI: Kiểm tra AI service có khả dụng không.
    /// </summary>
    Task<bool> IsAvailableAsync(CancellationToken ct = default);
}

/// <summary>
/// EN: Chat message DTO for AI context.
/// VI: DTO tin nhắn chat cho AI context.
/// </summary>
public record ChatMessage(
    string Role,       // "user", "assistant", "system"
    string Content,
    DateTime Timestamp
);

/// <summary>
/// EN: AI service configuration options.
/// VI: Cấu hình cho AI service.
/// </summary>
public class AIServiceOptions
{
    public const string SectionName = "AI";

    /// <summary>
    /// EN: AI provider (OpenAI, AzureOpenAI, Anthropic, etc.)
    /// VI: Nhà cung cấp AI (OpenAI, AzureOpenAI, Anthropic, v.v.)
    /// </summary>
    public string Provider { get; set; } = "OpenAI";

    /// <summary>
    /// EN: Model name to use.
    /// VI: Tên model sử dụng.
    /// </summary>
    public string Model { get; set; } = "gpt-4";

    /// <summary>
    /// EN: Maximum number of history messages to include in context.
    /// VI: Số tin nhắn lịch sử tối đa đưa vào context.
    /// </summary>
    public int MaxHistoryMessages { get; set; } = 20;

    /// <summary>
    /// EN: System prompt for the AI assistant.
    /// VI: System prompt cho AI assistant.
    /// </summary>
    public string SystemPrompt { get; set; } = 
        "You are a helpful assistant in a chat application. " +
        "Respond concisely and helpfully. " +
        "If asked about topics you don't know, admit it.";

    /// <summary>
    /// EN: Maximum tokens for response.
    /// VI: Số token tối đa cho response.
    /// </summary>
    public int MaxTokens { get; set; } = 1000;

    /// <summary>
    /// EN: Temperature for response creativity (0.0 - 2.0).
    /// VI: Temperature cho độ sáng tạo của response (0.0 - 2.0).
    /// </summary>
    public float Temperature { get; set; } = 0.7f;
}
