using Microsoft.AspNetCore.Mvc;
using QYZH.InteractiveMagazine.IService;
using QYZH.InteractiveMagazine.Models.Dto;
using QYZH.InteractiveMagazine.Models.Enum;
using System.Text.Json;

namespace QYZH.InteractiveMagazine.WebApi.Controllers;

/// <summary>
/// AI聊天控制器
/// </summary>
[Route("api/[controller]")]
[ApiController]
[ApiExplorerSettings(GroupName = nameof(ApiVersionEnum.Platform))]
public class AiChatController : BaseController
{
    private readonly IAiChatService _aiChatService;
    private readonly ILogger<AiChatController> _logger;

    public AiChatController(IAiChatService aiChatService, ILogger<AiChatController> logger)
    {
        _aiChatService = aiChatService;
        _logger = logger;
    }

    /// <summary>
    /// AI聊天（流式返回），根据需求生成提示词
    /// </summary>
    /// <param name="input">用户需求消息</param>
    /// <returns>SSE流，逐块返回AI生成的提示词内容</returns>
    [HttpPost]
    public async Task ChatAsync([FromBody] AiChatInput input)
    {
        Response.ContentType = "text/event-stream";
        Response.Headers.Append("Cache-Control", "no-cache");
        Response.Headers.Append("Connection", "keep-alive");

        try
        {
            await foreach (var chunk in _aiChatService.ChatAsync(input, HttpContext.RequestAborted))
            {
                var data = JsonSerializer.Serialize(new { content = chunk });
                await Response.WriteAsync($"data: {data}\n\n");
                await Response.Body.FlushAsync();
            }

            await Response.WriteAsync("data: [DONE]\n\n");
            await Response.Body.FlushAsync();
        }
        catch (OperationCanceledException)
        {
            _logger.LogInformation("客户端断开连接，AI聊天流式返回终止");
        }
        catch (Exception ex)
        {
            _logger.LogError(ex, "AI聊天流式返回异常，参数：{Input}", input);
            var errorData = JsonSerializer.Serialize(new { error = "AI聊天失败，请稍后重试" });
            await Response.WriteAsync($"data: {errorData}\n\n");
            await Response.Body.FlushAsync();
        }
    }
}
