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

import com.qqchen.deploy.backend.deploy.dto.GitInstanceDTO;
import com.qqchen.deploy.backend.deploy.service.IRepositoryManagerService;
import com.qqchen.deploy.backend.framework.api.Response;
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 lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * Git仓库管理API控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/repository-manager")
@Tag(name = "Git仓库管理", description = "Git仓库管理相关接口")
public class RepositoryManagerApiController {

    @Resource
    private IRepositoryManagerService repositoryManagerService;

    @Operation(summary = "同步Git仓库组", description = "同步指定外部系统的所有仓库组")
    @PostMapping("/{externalSystemId}/sync-groups")
    public Response<Void> syncGroups(
        @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
    ) {
        repositoryManagerService.syncGroups(externalSystemId);
        return Response.success();
    }

    @Operation(summary = "同步Git项目", description = "同步指定外部系统下指定仓库组的所有项目")
    @PostMapping("/{externalSystemId}/groups/sync-projects")
    public Response<Void> syncProjects(
        @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
    ) {
        repositoryManagerService.syncProjects(externalSystemId);
        return Response.success();
    }

    @Operation(summary = "同步Git分支", description = "同步指定外部系统下指定项目的所有分支")
    @PostMapping("/{externalSystemId}/projects/sync-branches")
    public Response<Void> syncBranches(
        @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
    ) {
        repositoryManagerService.syncBranches(externalSystemId);
        return Response.success();
    }

    @Operation(summary = "获取Git实例信息", description = "获取指定外部系统的Git实例信息，包括仓库组、项目和分支的数量及最后同步时间")
    @GetMapping("/{externalSystemId}/instance")
    public Response<GitInstanceDTO> instance(
        @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
    ) {
        return Response.success(repositoryManagerService.instance(externalSystemId));
    }
} 