using SqlSugar;
using Volo.Abp.Domain.Entities.Auditing;

namespace Yi.Framework.AiHub.Domain.Entities;

/// <summary>
/// 用量统计
/// </summary>
[SugarTable("Ai_UsageStatistics")]
[SugarIndex($"index_{{table}}_{nameof(UserId)}_{nameof(ModelId)}_{nameof(TokenId)}",
    nameof(UserId), OrderByType.Asc,
    nameof(ModelId), OrderByType.Asc,
    nameof(TokenId), OrderByType.Asc
)]
public class UsageStatisticsAggregateRoot : FullAuditedAggregateRoot<Guid>
{
    public UsageStatisticsAggregateRoot()
    {
    }

    public UsageStatisticsAggregateRoot(Guid? userId, string modelId, Guid tokenId)
    {
        UserId = userId;
        ModelId = modelId;
        TokenId = tokenId;
    }

    /// <summary>
    /// 用户id
    /// </summary>
    public Guid? UserId { get; set; }

    /// <summary>
    /// 哪个模型
    /// </summary>
    public string ModelId { get; set; }

    /// <summary>
    /// Token密钥Id（通过API调用时记录，Web调用为Guid.Empty）
    /// </summary>
    public Guid TokenId { get; set; }

    /// <summary>
    /// 对话次数
    /// </summary>
    public int UsageTotalNumber { get; set; }

    /// <summary>
    /// 使用输出token总数
    /// </summary>
    public long UsageOutputTokenCount { get; set; }

    /// <summary>
    /// 使用输入总数
    /// </summary>
    public long UsageInputTokenCount { get; set; }

    /// <summary>
    /// 总token使用数量
    /// </summary>
    public long TotalTokenCount { get; set; }

    /// <summary>
    /// 新增一次聊天统计
    /// </summary>
    public void AddOnceChat(long inputTokenCount, long outputTokenCount)
    {
        UsageTotalNumber += 1;
        UsageOutputTokenCount += outputTokenCount;
        UsageInputTokenCount += inputTokenCount;
        TotalTokenCount += (outputTokenCount + inputTokenCount);
    }
}