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.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.scenic.dto.scenic.ScenicV2WithConfigDTO;
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2ListResponse;
import com.ycwl.basic.integration.scenic.dto.scenic.ScenicV2WithConfigListResponse;
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 ScenicConfigV2Client scenicConfigV2Client;

    public ScenicV2DTO getScenic(Long scenicId) {
        log.info("获取景区信息, scenicId: {}", scenicId);
        CommonResponse<ScenicV2DTO> response = scenicV2Client.getScenic(scenicId);
        return handleResponse(response, "获取景区信息失败");
    }

    public ScenicV2WithConfigDTO getScenicWithConfig(Long scenicId) {
        log.info("获取景区配置信息, scenicId: {}", scenicId);
        CommonResponse<ScenicV2WithConfigDTO> response = scenicV2Client.getScenicWithConfig(scenicId);
        return handleResponse(response, "获取景区配置信息失败");
    }

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

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

    public ScenicV2DTO updateScenic(Long scenicId, UpdateScenicRequest request) {
        log.info("更新景区信息, scenicId: {}", scenicId);
        CommonResponse<ScenicV2DTO> response = scenicV2Client.updateScenic(scenicId, request);
        return handleResponse(response, "更新景区信息失败");
    }

    public void deleteScenic(Long scenicId) {
        log.info("删除景区, scenicId: {}", scenicId);
        CommonResponse<Void> response = scenicV2Client.deleteScenic(scenicId);
        handleResponse(response, "删除景区失败");
    }

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

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

    public ScenicV2WithConfigListResponse listScenicsWithConfig(Integer page, Integer pageSize, Integer status, String name) {
        log.info("分页查询景区带配置列表, page: {}, pageSize: {}, status: {}, name: {}", page, pageSize, status, name);
        CommonResponse<ScenicV2WithConfigListResponse> response = scenicV2Client.listScenicsWithConfig(page, pageSize, status, name);
        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();
    }
}