using Mapster;
using SqlSugar;
using Volo.Abp.Domain.Entities.Auditing;
using Yi.Framework.AiHub.Domain.Entities.ValueObjects;
using Yi.Framework.AiHub.Domain.Shared.Dtos;
using Yi.Framework.AiHub.Domain.Shared.Enums;

namespace Yi.Framework.AiHub.Domain.Entities.Chat;

[SugarTable("Ai_Message")]
[SugarIndex($"index_{{table}}_{nameof(UserId)}_{nameof(SessionId)}",
    $"{nameof(UserId)}", OrderByType.Desc,
    $"{nameof(SessionId)}", OrderByType.Desc
)]
public class MessageAggregateRoot : FullAuditedAggregateRoot<Guid>
{
    public MessageAggregateRoot()
    {
    }

    public MessageAggregateRoot(Guid userId, Guid? sessionId, string content, string role, string modelId,
        TokenUsage? tokenUsage)
    {
        UserId = userId;
        SessionId = sessionId;
        Content = content;
        Role = role;
        ModelId = modelId;
        if (tokenUsage is not null)
        {
            this.TokenUsage = tokenUsage.Adapt<TokenUsageValueObject>();
        }

        this.MessageType = sessionId is null ? MessageTypeEnum.Api : MessageTypeEnum.Web;
    }

    public Guid UserId { get; set; }
    public Guid? SessionId { get; set; }

    [SugarColumn(ColumnDataType = StaticConfig.CodeFirst_BigString)]
    public string Content { get; set; }

    public string Role { get; set; }
    public string ModelId { get; set; }
    public string? Remark { get; set; }

    [SugarColumn(IsOwnsOne = true)] public TokenUsageValueObject TokenUsage { get; set; } = new TokenUsageValueObject();

    public MessageTypeEnum MessageType { get; set; }
}