package com.ycwl.basic.controller.pc;

import com.ycwl.basic.integration.render.dto.config.BatchRenderWorkerConfigRequest;
import com.ycwl.basic.integration.render.dto.config.RenderWorkerConfigV2DTO;
import com.ycwl.basic.integration.render.service.RenderWorkerConfigIntegrationService;
import com.ycwl.basic.utils.ApiResponse;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

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

/**
 * 渲染工作器配置管理 V2 版本控制器
 * 基于 zt-render-worker 微服务标准接口实现
 * 
 * @author Claude Code
 * @date 2025-09-06
 */
@Slf4j
@RestController
@RequestMapping("/api/render/worker/config/v2")
@RequiredArgsConstructor
public class RenderWorkerConfigV2Controller {

    private final RenderWorkerConfigIntegrationService configIntegrationService;

    /**
     * 获取工作器所有配置
     *
     * @param workerId 工作器ID
     * @return 工作器配置列表
     */
    @GetMapping("/{workerId}")
    public ApiResponse<List<RenderWorkerConfigV2DTO>> getWorkerConfigs(@PathVariable Long workerId) {
        log.info("获取渲染工作器配置列表, workerId: {}", workerId);
        
        try {
            List<RenderWorkerConfigV2DTO> configs = configIntegrationService.getWorkerConfigs(workerId);
            return ApiResponse.success(configs);
        } catch (Exception e) {
            log.error("获取渲染工作器配置列表失败, workerId: {}", workerId, e);
            return ApiResponse.fail("获取渲染工作器配置列表失败: " + e.getMessage());
        }
    }

    /**
     * 获取工作器平铺配置
     *
     * @param workerId 工作器ID
     * @return 平铺配置Map
     */
    @GetMapping("/{workerId}/flat")
    public ApiResponse<Map<String, Object>> getWorkerFlatConfig(@PathVariable Long workerId) {
        log.info("获取渲染工作器平铺配置, workerId: {}", workerId);
        
        try {
            Map<String, Object> flatConfig = configIntegrationService.getWorkerFlatConfig(workerId);
            return ApiResponse.success(flatConfig);
        } catch (Exception e) {
            log.error("获取渲染工作器平铺配置失败, workerId: {}", workerId, e);
            return ApiResponse.fail("获取渲染工作器平铺配置失败: " + e.getMessage());
        }
    }

    /**
     * 根据配置键获取特定配置
     *
     * @param workerId 工作器ID
     * @param configKey 配置键
     * @return 配置信息
     */
    @GetMapping("/{workerId}/key/{configKey}")
    public ApiResponse<RenderWorkerConfigV2DTO> getWorkerConfigByKey(@PathVariable Long workerId,
                                                                     @PathVariable String configKey) {
        log.info("根据配置键获取渲染工作器配置, workerId: {}, configKey: {}", workerId, configKey);
        
        try {
            RenderWorkerConfigV2DTO config = configIntegrationService.getWorkerConfigByKey(workerId, configKey);
            return ApiResponse.success(config);
        } catch (Exception e) {
            log.error("根据配置键获取渲染工作器配置失败, workerId: {}, configKey: {}", workerId, configKey, e);
            return ApiResponse.fail("根据配置键获取渲染工作器配置失败: " + e.getMessage());
        }
    }

    /**
     * 创建工作器配置
     *
     * @param workerId 工作器ID
     * @param config 配置信息
     * @return 创建的配置信息
     */
    @PostMapping("/{workerId}")
    public ApiResponse<RenderWorkerConfigV2DTO> createWorkerConfig(@PathVariable Long workerId,
                                                                   @Valid @RequestBody RenderWorkerConfigV2DTO config) {
        log.info("创建渲染工作器配置, workerId: {}, configKey: {}", workerId, config.getConfigKey());
        
        try {
            RenderWorkerConfigV2DTO result = configIntegrationService.createWorkerConfig(workerId, config);
            return ApiResponse.success(result);
        } catch (Exception e) {
            log.error("创建渲染工作器配置失败, workerId: {}", workerId, e);
            return ApiResponse.fail("创建渲染工作器配置失败: " + e.getMessage());
        }
    }

    /**
     * 更新工作器配置
     *
     * @param workerId 工作器ID
     * @param configId 配置ID
     * @param updates 更新内容
     * @return 操作结果
     */
    @PutMapping("/{workerId}/{configId}")
    public ApiResponse<Void> updateWorkerConfig(@PathVariable Long workerId,
                                                @PathVariable Long configId,
                                                @Valid @RequestBody Map<String, Object> updates) {
        log.info("更新渲染工作器配置, workerId: {}, configId: {}", workerId, configId);
        
        try {
            configIntegrationService.updateWorkerConfig(workerId, configId, updates);
            return ApiResponse.success(null);
        } catch (Exception e) {
            log.error("更新渲染工作器配置失败, workerId: {}, configId: {}", workerId, configId, e);
            return ApiResponse.fail("更新渲染工作器配置失败: " + e.getMessage());
        }
    }

    /**
     * 删除工作器配置
     *
     * @param workerId 工作器ID
     * @param configId 配置ID
     * @return 操作结果
     */
    @DeleteMapping("/{workerId}/{configId}")
    public ApiResponse<Void> deleteWorkerConfig(@PathVariable Long workerId,
                                                @PathVariable Long configId) {
        log.info("删除渲染工作器配置, workerId: {}, configId: {}", workerId, configId);
        
        try {
            configIntegrationService.deleteWorkerConfig(workerId, configId);
            return ApiResponse.success(null);
        } catch (Exception e) {
            log.error("删除渲染工作器配置失败, workerId: {}, configId: {}", workerId, configId, e);
            return ApiResponse.fail("删除渲染工作器配置失败: " + e.getMessage());
        }
    }

    /**
     * 批量更新工作器配置
     *
     * @param workerId 工作器ID
     * @param request 批量配置请求
     * @return 操作结果
     */
    @PostMapping("/{workerId}/batch")
    public ApiResponse<Void> batchUpdateWorkerConfigs(@PathVariable Long workerId,
                                                      @Valid @RequestBody BatchRenderWorkerConfigRequest request) {
        log.info("批量更新渲染工作器配置, workerId: {}, configCount: {}", 
                 workerId, request.getConfigs() != null ? request.getConfigs().size() : 0);
        
        try {
            configIntegrationService.batchUpdateWorkerConfigs(workerId, request);
            return ApiResponse.success(null);
        } catch (Exception e) {
            log.error("批量更新渲染工作器配置失败, workerId: {}", workerId, e);
            return ApiResponse.fail("批量更新渲染工作器配置失败: " + e.getMessage());
        }
    }

    /**
     * 批量平铺更新工作器配置
     *
     * @param workerId 工作器ID
     * @param flatConfigs 平铺配置Map
     * @return 操作结果
     */
    @PostMapping("/{workerId}/flat-batch")
    public ApiResponse<Void> batchFlatUpdateWorkerConfigs(@PathVariable Long workerId,
                                                          @Valid @RequestBody Map<String, Object> flatConfigs) {
        log.info("批量平铺更新渲染工作器配置, workerId: {}, configCount: {}", workerId, flatConfigs.size());
        
        try {
            configIntegrationService.batchFlatUpdateWorkerConfigs(workerId, flatConfigs);
            return ApiResponse.success(null);
        } catch (Exception e) {
            log.error("批量平铺更新渲染工作器配置失败, workerId: {}", workerId, e);
            return ApiResponse.fail("批量平铺更新渲染工作器配置失败: " + e.getMessage());
        }
    }
}