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

import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController;
import com.qqchen.deploy.backend.deploy.entity.JenkinsBuild;
import com.qqchen.deploy.backend.deploy.dto.JenkinsBuildDTO;
import com.qqchen.deploy.backend.deploy.query.JenkinsBuildQuery;
import com.qqchen.deploy.backend.deploy.service.IJenkinsBuildService;
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.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import jakarta.servlet.http.HttpServletResponse;

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

/**
 * Jenkins构建信息 Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/jenkins-build")
@Tag(name = "Jenkins构建信息管理", description = "Jenkins构建信息管理相关接口")
public class JenkinsBuildApiController extends BaseController<JenkinsBuild, JenkinsBuildDTO, Long, JenkinsBuildQuery> {

    @Resource
    private IJenkinsBuildService jenkinsBuildService;

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

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

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

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

    @Override
    public Response<List<JenkinsBuildDTO>> findAll() {
        return super.findAll();
    }

    @Override
    public Response<Page<JenkinsBuildDTO>> page(JenkinsBuildQuery query) {
        return super.page(query);
    }

    @Override
    public Response<List<JenkinsBuildDTO>> findAll(JenkinsBuildQuery query) {
        return super.findAll(query);
    }

    @Override
    public CompletableFuture<Response<Void>> batchProcess(List<JenkinsBuildDTO> dtos) {
        return super.batchProcess(dtos);
    }

    @Operation(summary = "同步Jenkins构建", description = "异步同步，支持三种模式：1)只传externalSystemId-全量同步 2)传externalSystemId+viewId-同步视图 3)传externalSystemId+viewId+jobId-同步单个任务")
    @PostMapping("/sync")
    public Response<Void> sync(
        @Parameter(description = "外部系统ID", required = true) @RequestParam Long externalSystemId,
        @Parameter(description = "视图ID（可选）", required = false) @RequestParam(required = false) Long viewId,
        @Parameter(description = "任务ID（可选，传此参数时viewId必传）", required = false) @RequestParam(required = false) Long jobId
    ) {
        if (jobId != null) {
            // 同步单个任务
            jenkinsBuildService.syncBuilds(externalSystemId, viewId, jobId);
        } else if (viewId != null) {
            // 同步视图
            jenkinsBuildService.syncBuilds(externalSystemId, viewId);
        } else {
            // 全量同步
            jenkinsBuildService.syncBuilds(externalSystemId);
        }
        return Response.success();
    }

    @Override
    protected void exportData(HttpServletResponse response, List<JenkinsBuildDTO> data) {
        // TODO: 实现导出逻辑
        log.info("导出Jenkins构建信息数据，数据量：{}", data.size());
    }
} 