package cd.casic.module.execute.docker.api;

import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.framework.commons.pojo.PageResult;
import cd.casic.module.execute.docker.dataobject.dto.DockerImageDo;
import cd.casic.module.execute.docker.dataobject.model.DockerImage;
import cd.casic.module.execute.docker.dataobject.vo.DockerImagePageReqVO;
import cd.casic.module.execute.docker.service.IImageService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import static cd.casic.framework.commons.pojo.CommonResult.success;

/**
 * @description: TODO
 * @author: mianbin
 * @date: 2025/5/26 10:25
 * @version: 1.0
 */
@Tag(name = "docker管理 - 镜像管理")
@RequiredArgsConstructor
@RestController
@Validated
@RequestMapping("/api/images")
public class DockerImageController {


    @Resource
    private IImageService imageService;

    @PostMapping("/localUpload")
    @Operation(summary = "镜像上传")
    @PreAuthorize("@ss.hasPermission('docker:images:localUpload')")
    public CommonResult localImageUpload(@RequestBody DockerImage dockerImage) {
        return success(imageService.localImageUpload(dockerImage));
    }

    @GetMapping("/page")
    @Operation(summary = "本地镜像分页")
    @PreAuthorize("@ss.hasPermission('docker:images:query')")
    public CommonResult<PageResult<DockerImageDo>> getLocalImagePage(@Valid DockerImagePageReqVO pageVO) {
        return success(imageService.getLocalImagePage(pageVO));
    }

    @GetMapping("/localImagePush")
    @Operation(summary = "镜像推送,或者部署到目标服务器")
    @Parameters({
            @Parameter(name = "imageId", description = "镜像id"),
            @Parameter(name = "machineId", description = "主机id")
    })
    @PreAuthorize("@ss.hasPermission('docker:images:localImagePush')")
    public CommonResult localImagePush(@RequestParam(value = "imageId") String imageId, @RequestParam("machineId") String machineId) {
        return success(imageService.localImagePush(imageId,machineId));
    }
}
