package cn.reghao.autodop.dmaster.machine.controller;

import cn.reghao.jdkutil.result.WebBody;
import cn.reghao.autodop.dmaster.machine.model.po.SshAuth;
import cn.reghao.autodop.dmaster.machine.service.MachineService;
import cn.reghao.autodop.dmaster.notification.model.po.NotifyGroup;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Api(tags = "机器接口")
@RestController
@RequestMapping("/api/machine")
public class MachineController {
    private MachineService machineService;

    public MachineController(MachineService machineService) {
        this.machineService = machineService;
    }

    @ApiOperation(value = "设置机器通知")
    @PostMapping(value = "/notify/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public ResponseEntity<String> setMonitorNotify(@PathVariable("machineId") String machineId,
                                                   @RequestParam("groupId") List<NotifyGroup> notifyGroups) {
        machineService.setMachineNotify(machineId, notifyGroups);
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "编辑 SSH 认证信息")
    @PostMapping(value = "/sshauth/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> editSshAuth(@PathVariable("machineId") String machineId, @Valid SshAuth sshAuth) {
        machineService.setSshAuth(machineId, sshAuth);
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "编辑机器额外信息")
    @PostMapping(value = "/host/extra/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> editExtraMachineInfo(@PathVariable("machineId") String machineId,
                                                       @RequestParam("env") String env) {
        machineService.setEnv(machineId, env);
        return ResponseEntity.ok().body(WebBody.success());
    }

    @ApiOperation(value = "删除机器")
    @DeleteMapping(value = "/host/{machineId}", produces = MediaType.APPLICATION_JSON_VALUE)
    public ResponseEntity<String> machine(@PathVariable("machineId") String machineId) {
        return ResponseEntity.ok().body(WebBody.successWithMsg("暂未实现"));
    }
}
