package com.ycwl.basic.integration.scenic.service;

import com.ycwl.basic.integration.common.exception.IntegrationException;
import com.ycwl.basic.integration.common.response.CommonResponse;
import com.ycwl.basic.integration.common.service.IntegrationFallbackService;
import com.ycwl.basic.integration.scenic.client.ScenicV2Client;
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterPageResponse;
import com.ycwl.basic.integration.scenic.dto.filter.ScenicFilterRequest;
import com.ycwl.basic.integration.scenic.dto.scenic.CreateScenicRequest;
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2DTO;
import com.ycwl.basic.integration.common.response.PageResponse;
import com.ycwl.basic.integration.scenic.dto.scenic.UpdateScenicRequest;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class ScenicIntegrationService {

    private final ScenicV2Client scenicV2Client;
    private final IntegrationFallbackService fallbackService;
    
    private static final String SERVICE_NAME = "zt-scenic";

    public ScenicV2DTO getScenic(Long scenicId) {
        log.debug("获取景区信息, scenicId: {}", scenicId);
        return fallbackService.executeWithFallback(
            SERVICE_NAME,
            "scenic:" + scenicId,
            () -> {
                CommonResponse<ScenicV2DTO> response = scenicV2Client.getScenic(scenicId);
                return handleResponse(response, "获取景区信息失败");
            },
            ScenicV2DTO.class
        );
    }

    public ScenicV2DTO createScenic(CreateScenicRequest request) {
        log.debug("创建景区, name: {}", request.getName());
        CommonResponse<ScenicV2DTO> response = scenicV2Client.createScenic(request);
        return handleResponse(response, "创建景区失败");
    }

    public ScenicV2DTO updateScenic(Long scenicId, UpdateScenicRequest request) {
        log.debug("更新景区信息, scenicId: {}", scenicId);
        CommonResponse<ScenicV2DTO> response = scenicV2Client.updateScenic(scenicId, request);
        ScenicV2DTO result = handleResponse(response, "更新景区信息失败");
        // 清理缓存，确保下次查询获取最新数据
        fallbackService.clearFallbackCache(SERVICE_NAME, "scenic:" + scenicId);
        return result;
    }

    public void deleteScenic(Long scenicId) {
        log.debug("删除景区, scenicId: {}", scenicId);
        CommonResponse<Void> response = scenicV2Client.deleteScenic(scenicId);
        handleResponse(response, "删除景区失败");
        // 清理缓存
        fallbackService.clearFallbackCache(SERVICE_NAME, "scenic:" + scenicId);
    }

    public ScenicFilterPageResponse filterScenics(ScenicFilterRequest request) {
        log.debug("筛选景区, filters: {}", request.getFilters().size());
        CommonResponse<ScenicFilterPageResponse> response = scenicV2Client.filterScenics(request);
        return handleResponse(response, "筛选景区失败");
    }

    public PageResponse<ScenicV2DTO> listScenics(Integer page, Integer pageSize, Integer status, String name, String scenicIds) {
        log.debug("分页查询景区列表, page: {}, pageSize: {}, status: {}, name: {}, scenicIds: {}",
                  page, pageSize, status, name, scenicIds);
        CommonResponse<PageResponse<ScenicV2DTO>> response = scenicV2Client.listScenics(page, pageSize, status, name, scenicIds);
        return handleResponse(response, "分页查询景区列表失败");
    }

    private <T> T handleResponse(CommonResponse<T> response, String errorMessage) {
        if (response == null || !response.isSuccess()) {
            String msg = response != null && response.getMessage() != null 
                        ? response.getMessage() 
                        : errorMessage;
            Integer code = response != null ? response.getCode() : 5000;
            throw new IntegrationException(code, msg, "zt-scenic");
        }
        return response.getData();
    }
}