package cd.casic.module.machine.controller;

import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.framework.commons.pojo.PageResult;
import cd.casic.framework.commons.util.object.BeanUtils;
import cd.casic.module.machine.dal.dataobject.MachineEnvDO;
import cd.casic.module.machine.service.MachineEnvService;
import cd.casic.module.machine.controller.vo.MachineEnvVO;
import cn.hutool.core.collection.CollUtil;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import static cd.casic.framework.commons.pojo.CommonResult.success;

/**
 * 环境变量控制器
 */
@Tag(name = "环境变量管理")
@RestController
@RequestMapping("/ci/machineEnv")
@Validated
public class MachineEnvController {

    @Resource
    private MachineEnvService machineEnvService;

    @PostMapping("/create")
    @Operation(summary = "新增环境变量")
    @PreAuthorize("@ss.hasPermission('ci:machineEnv:create')")
    public CommonResult<Long> createEnv(@Valid @RequestBody MachineEnvVO machineEnvVO) {
        Long id = machineEnvService.createEnv(machineEnvVO);
        return success(id);
    }

    @PutMapping("/update")
    @Operation(summary = "修改环境变量")
    @PreAuthorize("@ss.hasPermission('ci:machineEnv:update')")
    public CommonResult<Boolean> updateEnv(@Valid @RequestBody MachineEnvVO machineEnvVO) {
        machineEnvService.updateEnv(machineEnvVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除机器的环境变量")
    @PreAuthorize("@ss.hasPermission('ci:machineEnv:delete')")
    public CommonResult<Boolean> deleteEnv(@RequestParam("id") Long id) {
        machineEnvService.deleteEnv(id);
        return success(true);
    }

    @DeleteMapping("/deleteList")
    @Operation(summary = "批量删除机器环境变量")
    @PreAuthorize("@ss.hasPermission('ci:machineEnv:delete')")
    public CommonResult<Boolean> deleteEnvList(@RequestParam("ids") String ids) {
        machineEnvService.deleteEnvList(ids);
        return success(true);
    }

    @GetMapping("/getEnv")
    @Operation(summary = "获取机器的环境变量")
    public CommonResult<MachineEnvVO> getEnv(@RequestParam("id") Long id) {
        MachineEnvVO machineEnvVO = machineEnvService.getEnv(id);
        return success(machineEnvVO);
    }


    @PostMapping("/list")
    @Operation(summary = "获取环境变量列表")
    public CommonResult<PageResult<MachineEnvVO>> getEnvPage(@Valid @RequestBody MachineEnvVO machineEnvVO) {
        PageResult<MachineEnvDO> pageResult = machineEnvService.getEnvPage(machineEnvVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        return success(BeanUtils.toBean(pageResult, MachineEnvVO.class));
    }
}

