using System.Collections.Generic;
using System.Threading.Tasks;
using BLL.Interfaces;
using DAL.Interfaces;
using MDL.DTOs;
using MDL.Models;

namespace BLL.Services
{
    public class TagTemplateService : ITagTemplateService
    {
        private readonly ITagTemplateRepository _tagTemplateRepository;
        private readonly ICacheService _cacheService;

        public TagTemplateService(ITagTemplateRepository tagTemplateRepository, ICacheService cacheService)
        {
            _tagTemplateRepository = tagTemplateRepository;
            _cacheService = cacheService;
        }

        public async Task<TagTemplateDTO> CreateTemplateAsync(CreateTagTemplateDTO dto)
        {
            var entity = new TagTemplateEntity
            {
                TagType = dto.TagType,
                Name = dto.Name,
                TemplateConfig = dto.TemplateConfig,
                TriggerRule = dto.TriggerRule,
                IsActive = true
            };

            var created = await _tagTemplateRepository.CreateAsync(entity);
            return MapToDTO(created);
        }

        public async Task<TagTemplateDTO> UpdateTemplateAsync(int id, UpdateTagTemplateDTO dto)
        {
            var existing = await _tagTemplateRepository.GetByIdAsync(id);
            if (existing == null)
                throw new System.Exception("Template not found");

            existing.Name = dto.Name;
            existing.TemplateConfig = dto.TemplateConfig;
            existing.TriggerRule = dto.TriggerRule;
            existing.IsActive = dto.IsActive;
            existing.UpdatedAt = System.DateTime.Now;

            var updated = await _tagTemplateRepository.UpdateAsync(existing);
            
            // 清除缓存
            await _cacheService.RemoveAsync($"tag_template:{updated.TagType}");
            
            return MapToDTO(updated);
        }

        public async Task<bool> DeleteTemplateAsync(int id)
        {
            return await _tagTemplateRepository.DeleteAsync(id);
        }

        public async Task<TagTemplateDTO> GetTemplateAsync(int id)
        {
            var entity = await _tagTemplateRepository.GetByIdAsync(id);
            return MapToDTO(entity);
        }

        public async Task<TagTemplateDTO> GetTemplateByTypeAsync(string tagType)
        {
            // 尝试从缓存获取
            var cached = await _cacheService.GetAsync<TagTemplateDTO>($"tag_template:{tagType}");
            if (cached != null)
                return cached;

            var entity = await _tagTemplateRepository.GetByTagTypeAsync(tagType);
            var dto = MapToDTO(entity);

            // 只有当dto不为null时才存入缓存
            if (dto != null)
            {
                await _cacheService.SetAsync($"tag_template:{tagType}", dto);
            }
            
            return dto;
        }

        public async Task<List<TagTemplateDTO>> GetAllTemplatesAsync()
        {
            var entities = await _tagTemplateRepository.GetAllAsync();
            return entities.ConvertAll(MapToDTO);
        }

        private TagTemplateDTO MapToDTO(TagTemplateEntity entity)
        {
            if (entity == null)
                return null;
                
            return new TagTemplateDTO
            {
                Id = entity.Id,
                TagType = entity.TagType,
                Name = entity.Name,
                TemplateConfig = entity.TemplateConfig,
                TriggerRule = entity.TriggerRule,
                IsActive = entity.IsActive,
                CreatedAt = entity.CreatedAt,
                UpdatedAt = entity.UpdatedAt
            };
        }
    }
}
