using BBM.ChatGLM.ChatSession.Aggregates;
using BBM.ChatGLM.Dtos;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Guids;

namespace BBM.ChatGLM.ChatSession
{
    [Authorize]
    public class ChatSessionsAppService : ChatGLMAppService, IChatSessionsAppService
    {
        private readonly IGuidGenerator _guidGenerator;
        private readonly ChatSessionsManager _chatSessionsManager;
        public ChatSessionsAppService(IGuidGenerator guidGenerator, ChatSessionsManager chatSessionsManager)
        {
            _guidGenerator = guidGenerator;
            _chatSessionsManager = chatSessionsManager;
        }

        [Authorize(ChatGLMPermissions.UsersManagement.Chat)]
        public async Task<ChatSessionDto> CreateAsync(ChatSessionCreateInput input)
        {
            var data = await _chatSessionsManager.CreateAsync(new ChatSessions(_guidGenerator.Create(), input.Content, CurrentUser.Id, CurrentTenant.Id));
            return ObjectMapper.Map<ChatSessions, ChatSessionDto>(data);
        }

        [Authorize(ChatGLMPermissions.UsersManagement.Chat)]
        public async Task<ChatSessionDto> UpdateAsync(ChatSessionUpdateInput input)
        {
            var data = await _chatSessionsManager.UpdateAsync(input.Id, input.Content);
            return ObjectMapper.Map<ChatSessions, ChatSessionDto>(data);
        }

        [Authorize(ChatGLMPermissions.PromptWordManagement.Query)]
        public async Task<PagedResultDto<ChatSessionDto>> PageAsync(ChatSessionPageInput input)
        {
            var result = new PagedResultDto<ChatSessionDto>
            {
                TotalCount = await _chatSessionsManager.CountAsync(input.Filter)
            };

            result.Items = await _chatSessionsManager.PageAsync(input.Filter, input.PageSize, input.PageIndex);
            return result;
        }
    }
}
