package com.backendsys.modules.sdk.comfyui.controller;

import com.backendsys.modules.common.config.security.annotations.Anonymous;
import com.backendsys.modules.sdk.comfyui.service.ComfyuiService;
import com.backendsys.modules.sdk.comfyui.service.ComfyuiSocketService;
import com.backendsys.modules.sdk.tencentcloud.cos.service.TencentCosService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ComfyuiDemoController {

    @Autowired
    private TencentCosService tencentCosService;

    @Autowired
    private ComfyuiService comfyuiService;
    @Autowired
    private ComfyuiSocketService comfyuiSocketService;

    /**
     * [ComfyUI] 创建 WebSocket 监听连接
     */
    @Anonymous
    @PostMapping("/api/comfyui/ws/connect")
    public String connect(String clientId) {
        comfyuiSocketService.connect(clientId, 8000).subscribe();
        return "Connection initiated for client_id: " + clientId;
    }

    /**
     * [ComfyUI] 断开 WebSocket 监听连接
     */
    @Anonymous
    @PostMapping("/api/comfyui/ws/disconnect")
    public String disconnect(String clientId) {
        comfyuiSocketService.disconnect(clientId).subscribe();
        return "Disconnected: " + clientId;
    }

//    /**
//     * 转存测试
//     */
//    @GetMapping("/api/comfyui/testToCos")
//    public SysFileResult testToCos(String url) {
//        return tencentCosService.urlToCOS(url, "png");
//    }

    /**
     * [ComfyUI] 获取最佳端口
     */
    @Anonymous
    @GetMapping("/api/comfyui/getBestPort")
    public Integer getBestPort() {
        return comfyuiService.getBestPort();
    }

}
