package com.ycwl.basic.integration.device.client;

import com.ycwl.basic.integration.common.response.CommonResponse;
import com.ycwl.basic.integration.common.response.PageResponse;
import com.ycwl.basic.integration.device.dto.config.*;
import com.ycwl.basic.integration.device.dto.defaults.*;
import com.ycwl.basic.integration.device.dto.device.*;
import com.ycwl.basic.integration.device.dto.status.DeviceStatusDTO;
import com.ycwl.basic.integration.device.dto.status.OnlineStatusResponseDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;

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

@FeignClient(name = "zt-device", contextId = "device-v2", path = "/api/device")
public interface DeviceV2Client {

    // ==================== Device V2 Operations ====================

    /**
     * 获取设备核心信息
     */
    @GetMapping("v2/{id}")
    CommonResponse<DeviceV2DTO> getDevice(@PathVariable("id") Long id);
    
    /**
     * 根据设备编号获取设备核心信息
     */
    @GetMapping("v2/no/{no}")
    CommonResponse<DeviceV2DTO> getDeviceByNo(@PathVariable("no") String no);

    /**
     * 创建设备
     */
    @PostMapping("v2/")
    CommonResponse<DeviceV2DTO> createDevice(@RequestBody CreateDeviceRequest request);
    
    /**
     * 更新设备
     */
    @PutMapping("v2/{id}")
    CommonResponse<String> updateDevice(@PathVariable("id") Long id, 
                                       @RequestBody UpdateDeviceRequest request);
    
    /**
     * 删除设备
     */
    @DeleteMapping("v2/{id}")
    CommonResponse<String> deleteDevice(@PathVariable("id") Long id);
    
    /**
     * 分页获取设备列表（核心信息）
     */
    @GetMapping("v2/")
    CommonResponse<PageResponse<DeviceV2DTO>> listDevices(
            @RequestParam(value = "page", defaultValue = "1") Integer page,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "no", required = false) String no,
            @RequestParam(value = "type", required = false) String type,
            @RequestParam(value = "isActive", required = false) Integer isActive,
            @RequestParam(value = "scenicId", required = false) Long scenicId,
            @RequestParam(value = "scenicIds", required = false) String scenicIds);

    /**
     * 根据配置条件筛选设备
     */
    @PostMapping("v2/filter-by-configs")
    CommonResponse<FilterDevicesByConfigsResponse> filterDevicesByConfigs(
            @RequestBody FilterDevicesByConfigsRequest request);

    // ==================== Device Config V2 Operations ====================

    /**
     * 获取设备所有配置
     */
    @GetMapping("config/v2/{deviceId}")
    CommonResponse<List<DeviceConfigV2DTO>> getDeviceConfigs(@PathVariable("deviceId") Long deviceId);
    
    /**
     * 根据设备编号获取设备所有配置
     */
    @GetMapping("config/v2/no/{no}")
    CommonResponse<List<DeviceConfigV2DTO>> getDeviceConfigsByNo(@PathVariable("no") String no);
    
    /**
     * 获取设备特定配置
     */
    @GetMapping("config/v2/{deviceId}/key/{configKey}")
    CommonResponse<DeviceConfigV2DTO> getDeviceConfigByKey(@PathVariable("deviceId") Long deviceId,
                                                          @PathVariable("configKey") String configKey);

    /**
     * 创建设备配置
     */
    @PostMapping("config/v2/{deviceId}")
    CommonResponse<DeviceConfigV2DTO> createDeviceConfig(@PathVariable("deviceId") Long deviceId,
                                                        @RequestBody CreateDeviceConfigRequest request);
    
    /**
     * 更新设备配置
     */
    @PutMapping("config/v2/{deviceId}/{id}")
    CommonResponse<String> updateDeviceConfig(@PathVariable("deviceId") Long deviceId,
                                             @PathVariable("id") Long id,
                                             @RequestBody UpdateDeviceConfigRequest request);
    
    /**
     * 删除设备配置
     */
    @DeleteMapping("config/v2/{deviceId}/{id}")
    CommonResponse<String> deleteDeviceConfig(@PathVariable("deviceId") Long deviceId,
                                             @PathVariable("id") Long id);
    
    /**
     * 批量更新设备配置
     */
    @PostMapping("config/v2/{deviceId}/batch")
    CommonResponse<BatchUpdateResponse> batchUpdateDeviceConfig(@PathVariable("deviceId") Long deviceId,
                                                               @RequestBody BatchDeviceConfigRequest request);

    // ==================== Default Config Operations ====================

    /**
     * 获取默认配置列表
     */
    @GetMapping("config/v2/defaults")
    CommonResponse<PageResponse<DefaultConfigResponse>> listDefaultConfigs(@RequestParam(value = "page", defaultValue = "1") int page,
                                                                          @RequestParam(value = "pageSize", defaultValue = "10") int pageSize);

    /**
     * 根据配置键获取默认配置
     */
    @GetMapping("config/v2/defaults/{configKey}")
    CommonResponse<DefaultConfigResponse> getDefaultConfig(@PathVariable("configKey") String configKey);

    /**
     * 创建默认配置
     */
    @PostMapping("config/v2/defaults")
    CommonResponse<String> createDefaultConfig(@RequestBody DefaultConfigRequest request);

    /**
     * 更新默认配置
     */
    @PutMapping("config/v2/defaults/{configKey}")
    CommonResponse<Map<String, Object>> updateDefaultConfig(@PathVariable("configKey") String configKey,
                                                           @RequestBody Map<String, Object> updates);

    /**
     * 删除默认配置
     */
    @DeleteMapping("config/v2/defaults/{configKey}")
    CommonResponse<String> deleteDefaultConfig(@PathVariable("configKey") String configKey);

    /**
     * 批量更新默认配置
     */
    @PostMapping("config/v2/defaults/batch")
    CommonResponse<BatchDefaultConfigResponse> batchUpdateDefaultConfigs(@RequestBody BatchDefaultConfigRequest request);

    // ==================== Device Status Operations ====================

    /**
     * 获取设备状态
     */
    @GetMapping("status/{deviceNo}")
    CommonResponse<DeviceStatusDTO> getDeviceStatus(@PathVariable("deviceNo") String deviceNo);

    /**
     * 检查设备是否在线
     */
    @GetMapping("status/{deviceNo}/online")
    CommonResponse<OnlineStatusResponseDTO> isDeviceOnline(@PathVariable("deviceNo") String deviceNo);

    /**
     * 获取所有在线设备
     */
    @GetMapping("status/online")
    CommonResponse<List<DeviceStatusDTO>> getAllOnlineDevices();

    /**
     * 设置设备离线
     */
    @PostMapping("status/{deviceNo}/offline")
    CommonResponse<String> setDeviceOffline(@PathVariable("deviceNo") String deviceNo);

    /**
     * 设置设备在线
     */
    @PostMapping("status/{deviceNo}/online")
    CommonResponse<String> setDeviceOnline(@PathVariable("deviceNo") String deviceNo);

    /**
     * 清理过期设备状态
     */
    @PostMapping("status/clean")
    CommonResponse<String> cleanExpiredDevices();
}