package cn.reghao.bnt.web.devops.app.controller;

import cn.reghao.bnt.web.devops.app.model.dto.DeployConfigUpdateDto;
import cn.reghao.bnt.web.util.SelectOption;
import cn.reghao.bnt.web.devops.app.db.query.AppBuildQuery;
import cn.reghao.bnt.web.devops.app.db.query.AppDeployQuery;
import cn.reghao.bnt.web.devops.app.model.dto.DeployConfigDto;
import cn.reghao.bnt.web.devops.app.model.po.config.AppDeployConfig;
import cn.reghao.bnt.web.devops.app.model.vo.AppDeployConfigVO;
import cn.reghao.bnt.web.devops.app.service.AppDeployService;
import cn.reghao.bnt.web.devops.machine.service.MachineQuery;
import cn.reghao.jutil.jdk.web.result.Result;
import cn.reghao.jutil.web.WebResult;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2019-08-30 18:49:15
 */
@Slf4j
@Tag(name = "应用部署配置页面")
@RestController
@RequestMapping("/api/devops/app/config/app/deploy")
public class AppDeployConfigPageController {
    private final AppDeployQuery appDeployQuery;
    private final AppBuildQuery appBuildQuery;
    private final MachineQuery machineQuery;
    private final AppDeployService appDeployService;

    public AppDeployConfigPageController(AppDeployQuery appDeployQuery, AppBuildQuery appBuildQuery,
                                         MachineQuery machineQuery, AppDeployService appDeployService) {
        this.appDeployQuery = appDeployQuery;
        this.appBuildQuery = appBuildQuery;
        this.machineQuery = machineQuery;
        this.appDeployService = appDeployService;
    }

    @Operation(summary = "部署配置列表页面", description = "N")
    @GetMapping("")
    public String deployConfigIndexPage(@RequestParam("appId") String appId) {
        List<AppDeployConfigVO> list = appDeployQuery.getByAppId(appId).stream()
                .map(AppDeployConfigVO::new)
                .collect(Collectors.toList());
        return WebResult.success(list);
    }

    @Operation(summary = "机器列表", description = "N")
    @GetMapping("/machine")
    public String addAppConfigPage(@RequestParam("env") String env) {
        List<SelectOption> machines = machineQuery.getOnlineMachines(env);
        return WebResult.success(machines);
    }

    @Operation(summary = "编辑部署配置页面", description = "N")
    @GetMapping("/edit")
    public String editAppConfigPage(@RequestParam("appId") String appId,
                                    @RequestParam("machineId") String machineId,
                                    Model model) {
        AppDeployConfig appDeployConfig = appDeployQuery.getByAppIdAndMachineId(appId, machineId);
        model.addAttribute("appDeployConfig", appDeployConfig);
        return "/admin/devops/app/deploy_edit";
    }

    @Operation(summary = "添加应用部署配置", description = "N")
    @PostMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
    public String add(@Validated DeployConfigDto deployConfigDto) {
        Result result = appDeployService.addDeployConfig(deployConfigDto);
        return WebResult.result(result);
    }

    @Operation(summary = "修改应用部署配置", description = "N")
    @PostMapping(value = "/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public String update(@Validated DeployConfigUpdateDto deployConfigUpdateDto) {
        Result result = appDeployService.updateStartScript(deployConfigUpdateDto);
        return WebResult.result(result);
    }

    @Operation(summary = "删除应用部署配置", description = "N")
    @PostMapping(value = "/delete", produces = MediaType.APPLICATION_JSON_VALUE)
    public String delete(Integer appDeployConfigId) {
        Result result = appDeployService.deleteDeployConfig(appDeployConfigId);
        return WebResult.result(result);
    }
}
