package cn.reghao.tnb.file.app.zoss.controller;

import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.tnb.common.web.WebResult;
import cn.reghao.tnb.common.db.SelectOption;
import cn.reghao.tnb.file.app.zoss.model.po.StoreVolume;
import cn.reghao.tnb.file.app.zoss.model.vo.StoreNodeInfo;
import cn.reghao.tnb.file.app.zoss.service.StoreNodeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author reghao
 * @date 2024-02-23 11:29:14
 */
@Tag(name = "存储节点接口")
@RestController
@RequestMapping("/api/oss/store")
public class StoreNodeController {
    private final StoreNodeService storeNodeService;

    public StoreNodeController(StoreNodeService storeNodeService) {
        this.storeNodeService = storeNodeService;
    }

    @Operation(summary = "存储节点列表", description = "N")
    @GetMapping(value = "/list", produces = MediaType.APPLICATION_JSON_VALUE)
    public String storeNodePage() {
        List<StoreNodeInfo> list = storeNodeService.getByPage();
        return WebResult.success(list);
    }

    @Operation(summary = "存储节点磁盘列表", description = "N")
    @GetMapping(value = "/disk", produces = MediaType.APPLICATION_JSON_VALUE)
    public String storeNodePage(@RequestParam("storeNodeId") Integer storeNodeId) {
        List<StoreVolume> list = storeNodeService.getStoreDisks(storeNodeId);
        return WebResult.success(list);
    }

    @Operation(summary = "存储节点 kv 列表", description = "N")
    @GetMapping(value = "/kv", produces = MediaType.APPLICATION_JSON_VALUE)
    public String storeNodeKeyValue() {
        List<SelectOption> storeNodes = storeNodeService.getNodeKeyValues();
        return WebResult.success(storeNodes);
    }

    @Operation(summary = "设置存储节点的状态", description = "N")
    @PostMapping(value = "/update_status/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateStoreNodeStatus(@PathVariable("id") Integer storeNodeId) {
        Result result = storeNodeService.updateStatus(storeNodeId);
        return WebResult.result(result);
    }

    @Operation(summary = "删除存储节点", description = "N")
    @PostMapping(value = "/delete/{id}", produces = MediaType.APPLICATION_JSON_VALUE)
    public String deleteStoreNode(@PathVariable("id") Integer storeNodeId) {
        Result result = storeNodeService.delete(storeNodeId);
        return WebResult.result(result);
    }
}
