package com.usky.ai.controller.web;

import com.usky.ai.dto.*;
import com.usky.ai.service.EdgeMediaService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/edge-media")
@RequiredArgsConstructor
public class EdgeMediaController {

    private final EdgeMediaService service;

    /**
     * 新增/修改通道
     */
    @PostMapping("/channel")
    public Object addChannel(@RequestBody EdgeAppControllerRequestDTO req) {
        // 简单参数校验
        if (req.getBoardId() == null || req.getMediaName() == null || req.getMediaUrl() == null) {
            return "BoardId/MediaName/MediaUrl 不能为空";
        }
        EdgeAppControllerReplyDTO reply = service.addChannel(req);
        if (reply.isSuccess()) {
            // mediaRepository.save(req);
            return "success";
        }
        return reply.getResult();
    }


    /**
     * 查询通道
     */
    @GetMapping("/channels/{boardId}")
    public Object fetchChannels(@PathVariable String boardId) {
        EdgeAppFetchResponseDTO resp = service.fetchChannels(boardId);
        if (resp.isSuccess()) {
            return resp.getContent();
        }
        return resp.getResult();
    }

    /**
     * 删除通道
     */
    @DeleteMapping("/delchannel/{boardId}/{mediaName}")
    public Object deleteChannel(@PathVariable String boardId, @PathVariable String mediaName) {
        EdgeAppDeleteResponseDTO resp = service.deleteChannel(boardId, mediaName);
        return resp.isSuccess() ? "deleted" : resp.getResult();
    }

    @GetMapping("/algorithm/ability/{boardId}")
    public Object fetchAlgorithmAbility(@PathVariable String boardId) {
        AlgAbilityFetchResponseDTO resp = service.fetchAlgorithmAbility(boardId);
        if (resp.isSuccess()) {
            return resp.getAbility();
        }
        return resp.getResult();
    }
}
