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.scenic.client.ScenicConfigV2Client;
import com.ycwl.basic.integration.scenic.dto.config.*;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Slf4j
@Service
@RequiredArgsConstructor
public class ScenicConfigIntegrationService {

    private final ScenicConfigV2Client scenicConfigV2Client;

    public List<ScenicConfigV2DTO> listConfigs(Long scenicId) {
        log.info("获取景区配置列表, scenicId: {}", scenicId);
        CommonResponse<List<ScenicConfigV2DTO>> response = scenicConfigV2Client.listConfigs(scenicId);
        return handleResponse(response, "获取景区配置列表失败");
    }

    public ScenicConfigV2DTO getConfigByKey(Long scenicId, String configKey) {
        log.info("根据键获取景区配置, scenicId: {}, configKey: {}", scenicId, configKey);
        CommonResponse<ScenicConfigV2DTO> response = scenicConfigV2Client.getConfigByKey(scenicId, configKey);
        return handleResponse(response, "根据键获取景区配置失败");
    }

    public Map<String, Object> getFlatConfigs(Long scenicId) {
        log.info("获取景区扁平化配置, scenicId: {}", scenicId);
        CommonResponse<Map<String, Object>> response = scenicConfigV2Client.getFlatConfigs(scenicId);
        return handleResponse(response, "获取景区扁平化配置失败");
    }

    public ScenicConfigV2DTO createConfig(Long scenicId, CreateConfigRequest request) {
        log.info("创建景区配置, scenicId: {}, configKey: {}", scenicId, request.getConfigKey());
        CommonResponse<ScenicConfigV2DTO> response = scenicConfigV2Client.createConfig(scenicId, request);
        return handleResponse(response, "创建景区配置失败");
    }

    public ScenicConfigV2DTO updateConfig(Long scenicId, String id, UpdateConfigRequest request) {
        log.info("更新景区配置, scenicId: {}, id: {}", scenicId, id);
        CommonResponse<ScenicConfigV2DTO> response = scenicConfigV2Client.updateConfig(scenicId, id, request);
        return handleResponse(response, "更新景区配置失败");
    }

    public void deleteConfig(Long scenicId, String id) {
        log.info("删除景区配置, scenicId: {}, id: {}", scenicId, id);
        CommonResponse<Void> response = scenicConfigV2Client.deleteConfig(scenicId, id);
        handleResponse(response, "删除景区配置失败");
    }

    public BatchUpdateResponse batchUpdateConfigs(Long scenicId, BatchConfigRequest request) {
        log.info("批量更新景区配置, scenicId: {}, configs count: {}", scenicId, request.getConfigs().size());
        CommonResponse<BatchUpdateResponse> response = scenicConfigV2Client.batchUpdateConfigs(scenicId, request);
        return handleResponse(response, "批量更新景区配置失败");
    }

    public BatchUpdateResponse batchFlatUpdateConfigs(Long scenicId, Map<String, Object> configs) {
        log.info("扁平化批量更新景区配置, scenicId: {}, configs count: {}", scenicId, configs.size());
        CommonResponse<BatchUpdateResponse> response = scenicConfigV2Client.batchFlatUpdateConfigs(scenicId, configs);
        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();
    }
}