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

namespace BBM.ChatGLM.PromptWordType
{
    public class PromptWordTypesAppService : ChatGLMAppService, IPromptWordTypesAppService
    {
        private readonly PromptWordTypesManager _promptWordTypesManager;
        private readonly IGuidGenerator _guidGenerator;
        public PromptWordTypesAppService(PromptWordTypesManager promptWordTypesManager, IGuidGenerator guidGenerator)
        {
            _promptWordTypesManager = promptWordTypesManager;
            _guidGenerator = guidGenerator;
        }

        [Authorize(ChatGLMPermissions.PromptWordTypeManagement.Create)]
        public async Task<PromptWordTypeDto> CreateAsync(PromptWordTypeCreateInput input)
        {
            var data = await _promptWordTypesManager.CreateAsync(new PromptWordTypes(_guidGenerator.Create(), input.Type, CurrentTenant.Id));
            return ObjectMapper.Map<PromptWordTypes, PromptWordTypeDto>(data);
        }

        [Authorize(ChatGLMPermissions.PromptWordTypeManagement.Update)]
        public async Task<PromptWordTypeDto> UpdateAsync(PromptWordTypeUpdateInput input)
        {
            var data = await _promptWordTypesManager.UpdateAsync(new PromptWordTypes(input.Id, input.Type));
            return ObjectMapper.Map<PromptWordTypes, PromptWordTypeDto>(data);
        }

        [Authorize(ChatGLMPermissions.PromptWordTypeManagement.Delete)]
        public async Task<int> DeleteAsync(GuidIdInput input)
        {
            return await _promptWordTypesManager.DeleteAsync(input.Id);
        }

        [Authorize(ChatGLMPermissions.PromptWordTypeManagement.Query)]
        public async Task<PagedResultDto<PromptWordTypeDto>> PageAsync(PromptWordTypePageInput input)
        {
            var result = new PagedResultDto<PromptWordTypeDto>
            {
                TotalCount = await _promptWordTypesManager.CountAsync(input.Filter)
            };

            var data = await _promptWordTypesManager.PageAsync(input.Filter, input.PageSize, input.PageIndex);
            result.Items = ObjectMapper.Map<List<PromptWordTypes>, List<PromptWordTypeDto>>(data);
            return result;
        }

        [Authorize(ChatGLMPermissions.PromptWordTypeManagement.Query)]
        public async Task<List<string>> Top1000Async()
        {
            var data = await _promptWordTypesManager.PageAsync(null, 1000, 0);
            return data.Select(x => x.Type).ToList();
        }
    }
}
