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

import com.qqchen.deploy.backend.deploy.dto.JenkinsInstanceDTO;
import com.qqchen.deploy.backend.deploy.service.IJenkinsManagerService;
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.*;

/**
 * Jenkins管理API控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/jenkins-manager")
@Tag(name = "Jenkins管理", description = "Jenkins管理相关接口")
public class JenkinsManagerApiController {

    @Resource
    private IJenkinsManagerService jenkinsManagerService;

    @Operation(summary = "同步所有Jenkins数据")
    @PostMapping("/{externalSystemId}/sync-all")
    public Response<Void> syncAll(
        @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
    ) {
        jenkinsManagerService.syncAll(externalSystemId);
        return Response.success();
    }

    @Operation(summary = "同步Jenkins视图")
    @PostMapping("/{externalSystemId}/sync-views")
    public Response<Void> syncViews(
        @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
    ) {
        jenkinsManagerService.syncViews(externalSystemId);
        return Response.success();
    }

    @Operation(summary = "同步Jenkins任务")
    @PostMapping("/{externalSystemId}/sync-jobs")
    public Response<Void> syncJobs(
        @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
    ) {
        jenkinsManagerService.syncJobs(externalSystemId);
        return Response.success();
    }

    @Operation(summary = "同步Jenkins构建信息")
    @PostMapping("/{externalSystemId}/sync-builds")
    public Response<Void> syncBuilds(
        @Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId
    ) {
        jenkinsManagerService.syncBuilds(externalSystemId);
        return Response.success();
    }

    @Operation(summary = "获取Jenkins实例信息")
    @GetMapping("/{externalSystemId}/instance")
    public Response<JenkinsInstanceDTO> instance(@Parameter(description = "外部系统ID", required = true) @PathVariable Long externalSystemId) {
        return Response.success(jenkinsManagerService.instance(externalSystemId));
    }
}