using BBM.ChatGLM.PromptWord;
using BBM.ChatGLM.PromptWordType.Aggregates;
using BBM.ChatGLM.PromptWordType.Repositorys;

namespace BBM.ChatGLM.PromptWordType
{
    public class PromptWordTypesManager : ChatGLMDomainService
    {
        private readonly IPromptWordTypeRepository _promptWordTypeRepository;
        private readonly IPromptWordsRepository _promptWordsRepository;
        public PromptWordTypesManager(IPromptWordTypeRepository promptWordTypeRepository, IPromptWordsRepository promptWordsRepository)
        {
            _promptWordTypeRepository = promptWordTypeRepository;
            _promptWordsRepository = promptWordsRepository;
        }

        public async Task<PromptWordTypes> CreateAsync(PromptWordTypes promptWords)
        {
            return await _promptWordTypeRepository.InsertAsync(promptWords);
        }

        public async Task<PromptWordTypes> UpdateAsync(PromptWordTypes promptWords)
        {
            var entity = await _promptWordTypeRepository.GetAsync(promptWords.Id);
            var originType = new String(entity.Type);
            var prompts = await _promptWordsRepository.GetListAsync();
            var promptTypes = prompts.Where(x => x.Type == originType).ToList();
            foreach (var promptWordType in promptTypes)
            {
                promptWordType.Type = promptWords.Type;
            }
            if (promptTypes.Count > 0)
            {
                await _promptWordsRepository.UpdateManyAsync(promptTypes);
            }
            entity.Type = promptWords.Type;
            return await _promptWordTypeRepository.UpdateAsync(entity);
        }

        public async Task<List<PromptWordTypes>> PageAsync(string filter, int pageSize, int pageIndex)
        {
            return await _promptWordTypeRepository.PageAsync(filter, pageSize, pageIndex, CurrentTenant.Id);
        }

        public async Task<long> CountAsync(string filter)
        {
            return await _promptWordTypeRepository.CountAsync(filter, CurrentTenant.Id);
        }

        public async Task<int> DeleteAsync(Guid id)
        {
            return await _promptWordTypeRepository.DeleteAsync(id);
        }
    }
}
