package com.qqchen.deploy.backend.deploy.api;

import com.qqchen.deploy.backend.deploy.entity.RepositoryBranch;
import com.qqchen.deploy.backend.deploy.dto.RepositoryBranchDTO;
import com.qqchen.deploy.backend.deploy.query.RepositoryBranchQuery;
import com.qqchen.deploy.backend.deploy.service.IRepositoryBranchService;
import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * Git仓库分支管理接口
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/repository-branch")
@Tag(name = "Git仓库分支管理", description = "Git仓库分支管理相关接口")
public class RepositoryBranchApiController extends BaseController<RepositoryBranch, RepositoryBranchDTO, Long, RepositoryBranchQuery> {

    @Resource
    private IRepositoryBranchService repositoryBranchService;

    @Override
    public Response<RepositoryBranchDTO> create(@Validated @RequestBody RepositoryBranchDTO dto) {
        return super.create(dto);
    }

    @Override
    public Response<RepositoryBranchDTO> update(@PathVariable Long id, @Validated @RequestBody RepositoryBranchDTO dto) {
        return super.update(id, dto);
    }

    @Override
    public Response<Void> delete(@PathVariable Long id) {
        return super.delete(id);
    }

    @Override
    public Response<RepositoryBranchDTO> findById(@PathVariable Long id) {
        return super.findById(id);
    }


    @Operation(summary = "同步Git分支", description = "异步同步，支持三种模式：1)只传externalSystemId-全量同步 2)传externalSystemId+repoGroupId-同步仓库组 3)传externalSystemId+repoGroupId+repoProjectId-同步单个项目")
    @PostMapping("/sync")
    public Response<Void> sync(
        @Parameter(description = "外部系统ID", required = true) @RequestParam Long externalSystemId,
        @Parameter(description = "仓库组ID（可选）", required = false) @RequestParam(required = false) Long repoGroupId,
        @Parameter(description = "仓库项目ID（可选，传此参数时repoGroupId必传）", required = false) @RequestParam(required = false) Long repoProjectId
    ) {
        if (repoProjectId != null) {
            // 同步单个项目
            repositoryBranchService.syncBranches(externalSystemId, repoGroupId, repoProjectId);
        } else if (repoGroupId != null) {
            // 同步仓库组
            repositoryBranchService.syncBranches(externalSystemId, repoGroupId);
        } else {
            // 全量同步
            repositoryBranchService.syncBranches(externalSystemId);
        }
        return Response.success();
    }

    @Override
    protected void exportData(HttpServletResponse response, List<RepositoryBranchDTO> data) {

    }
}