package com.nanri.aiimage.modules.ziniao.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.nanri.aiimage.common.exception.BusinessException;
import com.nanri.aiimage.config.ZiniaoProperties;
import com.nanri.aiimage.modules.ziniao.model.cache.ZiniaoSessionCacheDto;
import com.nanri.aiimage.modules.ziniao.model.cache.ZiniaoShopCacheDto;
import lombok.RequiredArgsConstructor;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.time.Duration;
import java.util.List;

@Service
@RequiredArgsConstructor
public class ZiniaoSessionCacheService {

    private final StringRedisTemplate stringRedisTemplate;
    private final ObjectMapper objectMapper;
    private final ZiniaoProperties ziniaoProperties;

    public void saveSession(ZiniaoSessionCacheDto session) {
        try {
            stringRedisTemplate.opsForValue().set(buildSessionKey(session.getSessionId()), objectMapper.writeValueAsString(session), Duration.ofHours(ziniaoProperties.getSessionTtlHours()));
        } catch (Exception ex) {
            throw new BusinessException("保存紫鸟会话失败");
        }
    }

    public ZiniaoSessionCacheDto getSession(String sessionId) {
        String raw = stringRedisTemplate.opsForValue().get(buildSessionKey(sessionId));
        if (raw == null || raw.isBlank()) {
            return null;
        }
        try {
            return objectMapper.readValue(raw, ZiniaoSessionCacheDto.class);
        } catch (Exception ex) {
            throw new BusinessException("读取紫鸟会话失败");
        }
    }

    public void saveShops(String sessionId, List<ZiniaoShopCacheDto> shops) {
        try {
            stringRedisTemplate.opsForValue().set(buildShopsKey(sessionId), objectMapper.writeValueAsString(shops), Duration.ofMinutes(ziniaoProperties.getShopsCacheMinutes()));
        } catch (Exception ex) {
            throw new BusinessException("保存紫鸟店铺列表失败");
        }
    }

    public List<ZiniaoShopCacheDto> getShops(String sessionId) {
        String raw = stringRedisTemplate.opsForValue().get(buildShopsKey(sessionId));
        if (raw == null || raw.isBlank()) {
            return List.of();
        }
        try {
            return objectMapper.readValue(raw, new TypeReference<List<ZiniaoShopCacheDto>>() {});
        } catch (Exception ex) {
            throw new BusinessException("读取紫鸟店铺列表失败");
        }
    }

    public void saveCurrentShop(String sessionId, ZiniaoShopCacheDto shop) {
        try {
            stringRedisTemplate.opsForValue().set(buildCurrentShopKey(sessionId), objectMapper.writeValueAsString(shop), Duration.ofHours(ziniaoProperties.getSessionTtlHours()));
        } catch (Exception ex) {
            throw new BusinessException("保存当前紫鸟店铺失败");
        }
    }

    public ZiniaoShopCacheDto getCurrentShop(String sessionId) {
        String raw = stringRedisTemplate.opsForValue().get(buildCurrentShopKey(sessionId));
        if (raw == null || raw.isBlank()) {
            return null;
        }
        try {
            return objectMapper.readValue(raw, ZiniaoShopCacheDto.class);
        } catch (Exception ex) {
            throw new BusinessException("读取当前紫鸟店铺失败");
        }
    }

    private String buildSessionKey(String sessionId) {
        return "ziniao:session:" + sessionId;
    }

    private String buildShopsKey(String sessionId) {
        return "ziniao:shops:" + sessionId;
    }

    private String buildCurrentShopKey(String sessionId) {
        return "ziniao:current-shop:" + sessionId;
    }
}
