using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using OpenAI.Chat;
using SqlSugar;
using Volo.Abp.Application.Services;
using Yi.Framework.AiHub.Application.Contracts.Dtos.OpenAiDto;
using Yi.Framework.AiHub.Domain.Entities.Model;
using Yi.Framework.AiHub.Domain.Managers;
using Yi.Framework.SqlSugarCore.Abstractions;

namespace Yi.Framework.AiHub.Application.Services;

public class OpenApiService : ApplicationService
{
    private readonly IHttpContextAccessor _httpContextAccessor;
    private readonly ILogger<OpenApiService> _logger;
    private readonly TokenManager _tokenManager;
    private readonly AiGateWayManager _aiGateWayManager;
    private readonly ISqlSugarRepository<AiModelEntity> _aiModelRepository;

    public OpenApiService(IHttpContextAccessor httpContextAccessor, ILogger<OpenApiService> logger,
        TokenManager tokenManager, AiGateWayManager aiGateWayManager,
        ISqlSugarRepository<AiModelEntity> aiModelRepository)
    {
        _httpContextAccessor = httpContextAccessor;
        _logger = logger;
        _tokenManager = tokenManager;
        _aiGateWayManager = aiGateWayManager;
        _aiModelRepository = aiModelRepository;
    }

    /// <summary>
    /// 对话
    /// </summary>
    /// <param name="input"></param>
    /// <param name="cancellationToken"></param>
    [HttpPost("openApi/v1/chat/completions")]
    public async Task ChatCompletionsAsync([FromBody]ChatCompletionsInput input, CancellationToken cancellationToken)
    {
        //前面都是校验，后面才是真正的调用
        var httpContext = this._httpContextAccessor.HttpContext;
        
        var userId = await _tokenManager.GetUserIdAsync(GetTokenByHttpContext(httpContext));
        var history = new List<ChatMessage>();
        foreach (var aiChatContextDto in input.Messages)
        {
            if (aiChatContextDto.Role == "assistant")
            {
                history.Add(ChatMessage.CreateAssistantMessage(aiChatContextDto.ConvertContent()));
            }
            else if (aiChatContextDto.Role == "user")
            {
                history.Add(ChatMessage.CreateUserMessage(aiChatContextDto.ConvertContent()));
            }
            else  if (aiChatContextDto.Role == "system")
            {
                history.Add(ChatMessage.CreateSystemMessage(aiChatContextDto.ConvertContent()));
            }
        }

        //是否使用流式传输
        if (input.Stream == true)
        {
            //ai网关代理httpcontext
            await _aiGateWayManager.CompleteChatStreamForStatisticsAsync(_httpContextAccessor.HttpContext, input.Model,
                history,
                userId, null, cancellationToken);
        }
        else
        {
            await _aiGateWayManager.CompleteChatForStatisticsAsync(_httpContextAccessor.HttpContext, input.Model,
                history, userId, null,
                cancellationToken);
        }
    }

    /// <summary>
    /// 获取模型列表
    /// </summary>
    /// <returns></returns>
    [HttpGet("openApi/v1/models")]
    public async Task<ModelGetOutput> ModelsAsync()
    {
        var data = await _aiModelRepository._DbQueryable
            .OrderByDescending(x => x.OrderNum)
            .Select(x => new ModelDataOutput
            {
                ModelId = x.ModelId,
                Object = "model",
                Owned_by = "organization-owner",
                Permission = new List<string>()
            }).ToListAsync();

        return new ModelGetOutput()
        {
            Data = data
        };
    }

    private string? GetTokenByHttpContext(HttpContext httpContext)
    {
        // 获取Authorization头
        string authHeader = httpContext.Request.Headers["Authorization"];

        // 检查是否有Bearer token
        if (authHeader != null && authHeader.StartsWith("Bearer "))
        {
            return authHeader.Substring("Bearer ".Length).Trim();
        }

        return null;
    }
}