package cd.casic.ci.repository.controller.admin;

import cd.casic.ci.repository.dal.dataobject.OpsVcsDO;
import cd.casic.ci.repository.service.OpsRepositoryService;
import cd.casic.framework.commons.pojo.CommonResult;
import cd.casic.framework.commons.pojo.PageResult;
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 static cd.casic.framework.commons.pojo.CommonResult.success;

/**
 * 应用版本仓库控制器
 */
@Tag(name = "版本仓库接口")
@RestController
@RequestMapping("/ci/repository")
@Validated
public class OpsVcsController {

    @Resource
    private OpsRepositoryService opsRepositoryService;

    /**
     * 查询分页应用版本仓库
     */
    @GetMapping("/page")
    @Operation(summary = "应用版本仓库_分页")
    @PreAuthorize("@ss.hasPermission('ci:Repository:query')")
    public CommonResult<PageResult<OpsVcsDO>> getCIRepositoryPage(@Valid OpsVcsVO opsVcsVO) {
        PageResult<OpsVcsDO> pageResult = opsRepositoryService.getVcsPage(opsVcsVO);
        return success(pageResult);
    }

    /**
     * 添加应用版本仓库
     */
    @PostMapping("/add")
    @Operation(summary = "应用版本仓库_增加")
    @PreAuthorize("@ss.hasPermission('ci:Repository:create')")
    public CommonResult add(@Valid @RequestBody OpsVcsVO opsVcs) {
        return success(opsRepositoryService.add(opsVcs));
    }

    /**
     * 删除应用版本仓库
     */
    @DeleteMapping("/delete/{opsVcsId}")
    @Operation(summary = "应用版本仓库_根据id删除")
    @PreAuthorize("@ss.hasPermission('ci:Repository:delete')")
    public CommonResult delete(@PathVariable Long opsVcsId) {
        return success(opsRepositoryService.delete(opsVcsId));
    }

    /**
     * 编辑应用版本仓库
     */
    @PutMapping("/edit")
    @Operation(summary = "应用版本仓库_编辑")
    @PreAuthorize("@ss.hasPermission('ci:Repository:edit')")
    public CommonResult edit(@Valid @RequestBody OpsVcsVO opsVcsVO) {
        return success(opsRepositoryService.edit(opsVcsVO));
    }

    /**
     * 查看应用版本仓库
     */
    @GetMapping("/detail/{opsVcsId}")
    @Operation(summary = "应用版本仓库_根据id获取详情")
//    @PreAuthorize("@ss.hasPermission('ci:Repository:detail')")
    public CommonResult detail(@PathVariable Long opsVcsId) {
        return success(opsRepositoryService.detail(opsVcsId));
    }

    /**
     * 应用版本仓库列表
     */
    @GetMapping("/list")
    @Operation(summary = "应用版本仓库_列表")
//    @PreAuthorize("@ss.hasPermission('ci:Repository:list')")
    public CommonResult vcsList() {
        return success(opsRepositoryService.vcsList());
    }

    /**
     * 应用版本仓库获取分支
     *
     */
    @GetMapping("/getBranch/{opsVcsId}")
    @Operation(summary = "应用版本仓库_获取分支")
    public CommonResult getBranch(@PathVariable Long opsVcsId) {
        return success(opsRepositoryService.getBranch(opsVcsId));
    }

    @PostMapping("/checkVcsUrl")
    @Operation(summary = "应用版本仓库_检测存在，是否有权限")
    public CommonResult checkVcsUrl(@RequestBody OpsVcsVO opsVcs) {
        return opsRepositoryService.checkVcsUrl(opsVcs);
    }

}
