using Volo.Abp.Domain.Services;
using Volo.Abp.Users;
using Yi.Framework.AiHub.Domain.Entities;
using Yi.Framework.AiHub.Domain.Entities.Chat;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.SqlSugarCore.Abstractions;

namespace Yi.Framework.AiHub.Domain.Managers;

public class AiMessageManager : DomainService
{
    private readonly ISqlSugarRepository<MessageAggregateRoot> _repository;

    public AiMessageManager(ISqlSugarRepository<MessageAggregateRoot> repository)
    {
        _repository = repository;
    }

    /// <summary>
    /// 创建系统消息
    /// </summary>
    /// <param name="userId">用户Id</param>
    /// <param name="sessionId">会话Id</param>
    /// <param name="input">消息输入</param>
    /// <param name="tokenId">Token Id（Web端传Guid.Empty）</param>
    /// <returns></returns>
    public async Task CreateSystemMessageAsync(Guid? userId, Guid? sessionId, MessageInputDto input, Guid? tokenId = null)
    {
        input.Role = "system";
        var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId, input.TokenUsage, tokenId);
        await _repository.InsertAsync(message);
    }

    /// <summary>
    /// 创建用户消息
    /// </summary>
    /// <param name="userId">用户Id</param>
    /// <param name="sessionId">会话Id</param>
    /// <param name="input">消息输入</param>
    /// <param name="tokenId">Token Id（Web端传Guid.Empty）</param>
    /// <returns></returns>
    public async Task CreateUserMessageAsync(Guid? userId, Guid? sessionId, MessageInputDto input, Guid? tokenId = null)
    {
        input.Role = "user";
        var message = new MessageAggregateRoot(userId, sessionId, input.Content, input.Role, input.ModelId, input.TokenUsage, tokenId);
        await _repository.InsertAsync(message);
    }
}