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

import com.qqchen.deploy.backend.deploy.entity.RepositoryProject;
import com.qqchen.deploy.backend.deploy.dto.RepositoryProjectDTO;
import com.qqchen.deploy.backend.deploy.query.RepositoryProjectQuery;
import com.qqchen.deploy.backend.deploy.service.IRepositoryProjectService;
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-project")
@Tag(name = "Git仓库项目管理", description = "Git仓库项目管理相关接口")
public class RepositoryProjectApiController extends BaseController<RepositoryProject, RepositoryProjectDTO, Long, RepositoryProjectQuery> {

    @Resource
    private IRepositoryProjectService repositoryProjectService;

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

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

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

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


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

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

    }
}