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.MachineInfoDO;
import cd.casic.module.machine.dal.dataobject.SecretKeyDO;
import cd.casic.module.machine.service.SecretKeyService;
import cd.casic.module.machine.controller.vo.SecretKeyVO;
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.*;

import java.util.List;

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

@RestController
@RequestMapping("/ci/secretKey")
@Tag(name = "密钥管理")
@Validated
public class SecretKeyController {
    @Resource
    private SecretKeyService secretKeyService;

    @PostMapping(value = "/create")
    @Operation(summary = "新增密钥")
//    @PreAuthorize("@ss.hasPermission('ci:secretKey:create')")
    public CommonResult<Long> createSecretKey(@Valid @RequestBody SecretKeyVO secretKeyVO) {
        Long secretKeyId = secretKeyService.createSecretKey(secretKeyVO);
        return success(secretKeyId);
    }

    @PutMapping("/update")
    @Operation(summary = "编辑密钥信息")
//    @PreAuthorize("@ss.hasPermission('ci:secretKey:update')")
    public CommonResult<Boolean> updateSecretKey(@Valid @RequestBody SecretKeyVO secretKeyVO) {
        secretKeyService.updateSecretKey(secretKeyVO);
        return success(true);
    }

    @GetMapping("/getBindMachine")
    @Operation(summary = "获取密钥绑定的机器列表")
    public CommonResult<List<MachineInfoDO>> getBindMachine(@RequestParam Long secretKeyId) {
        return success(secretKeyService.getBindMachine(secretKeyId));
    }

    @GetMapping("/getSecretKey")
    @Operation(summary = "获取机器的密钥")
    public CommonResult<SecretKeyVO> getSecretKey(@RequestParam("id") Long id) {
        SecretKeyVO secretKeyVO = secretKeyService.getSecretKey(id);
        return success(secretKeyVO);
    }

    @DeleteMapping("/deleteList")
    @Operation(summary = "批量删除密钥")
//    @PreAuthorize("@ss.hasPermission('ci:secretKey:delete')")
    public CommonResult<Boolean> deleteSecretKeyList(@RequestParam("ids") List<Long> ids) {
        secretKeyService.deleteSecretKeyList(ids);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除密钥")
    @PreAuthorize("@ss.hasPermission('ci:secretKey:delete')")
    public CommonResult deleteSecretKeyList(@RequestParam("id") Long id) {
        return success(secretKeyService.deleteById(id));
    }

    @PostMapping("/list")
    @Operation(summary = "获取密钥信息列表")
    public CommonResult<PageResult<SecretKeyVO>> getSecretKeyPage(@Valid @RequestBody SecretKeyVO secretKeyVO) {
        PageResult<SecretKeyDO> pageResult = secretKeyService.getSecretKeyPage(secretKeyVO);
        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(new PageResult<>(pageResult.getTotal()));
        }
        return success(BeanUtils.toBean(pageResult, SecretKeyVO.class));
    }
}
