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 TagInstanceService : ITagInstanceService
    {
        private readonly ITagInstanceRepository _tagInstanceRepository;
        private readonly ICacheService _cacheService;

        public TagInstanceService(ITagInstanceRepository tagInstanceRepository, ICacheService cacheService)
        {
            _tagInstanceRepository = tagInstanceRepository;
            _cacheService = cacheService;
        }

        public async Task<TagInstanceDTO> CreateInstanceAsync(CreateTagInstanceDTO dto)
        {
            var entity = new TagInstanceEntity
            {
                TagType = dto.TagType,
                TemplateId = 1, // 默认模板ID，实际应根据TagType获取
                NeutralWaybillNumber = dto.NeutralWaybillNumber,
                CustomerId = dto.CustomerId,
                Status = "ACTIVE",
                TriggerTime = System.DateTime.Now
            };

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

        public async Task<TagInstanceDTO> GetInstanceAsync(long id)
        {
            // 尝试从缓存获取
            var cached = await _cacheService.GetAsync<TagInstanceDTO>($"tag_instance:{id}");
            if (cached != null)
                return cached;

            var entity = await _tagInstanceRepository.GetByIdAsync(id);
            var dto = MapToDTO(entity);

            // 存入缓存
            await _cacheService.SetAsync($"tag_instance:{id}", dto);
            
            return dto;
        }

        public async Task<List<TagInstanceDTO>> GetInstancesByWaybillAsync(string neutralWaybillNumber)
        {
            var entities = await _tagInstanceRepository.GetByWaybillAsync(neutralWaybillNumber);
            return entities.ConvertAll(MapToDTO);
        }

        public async Task<List<TagInstanceDTO>> GetInstancesByTypeAsync(string tagType)
        {
            var entities = await _tagInstanceRepository.GetByTagTypeAsync(tagType);
            return entities.ConvertAll(MapToDTO);
        }

        public async Task<bool> UpdateInstanceStatusAsync(long id, string status)
        {
            var entity = await _tagInstanceRepository.GetByIdAsync(id);
            if (entity == null)
                return false;

            entity.Status = status;
            entity.UpdatedAt = System.DateTime.Now;

            await _tagInstanceRepository.UpdateAsync(entity);
            
            // 清除缓存
            await _cacheService.RemoveAsync($"tag_instance:{id}");
            
            return true;
        }

        private TagInstanceDTO MapToDTO(TagInstanceEntity entity)
        {
            return new TagInstanceDTO
            {
                Id = entity.Id,
                TagType = entity.TagType,
                TemplateId = entity.TemplateId,
                NeutralWaybillNumber = entity.NeutralWaybillNumber,
                CustomerId = entity.CustomerId,
                Status = entity.Status,
                TriggerTime = entity.TriggerTime,
                CreatedAt = entity.CreatedAt,
                UpdatedAt = entity.UpdatedAt
            };
        }
    }
}
