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.JenkinsJob;
import com.qqchen.deploy.backend.deploy.dto.JenkinsJobDTO;
import com.qqchen.deploy.backend.deploy.query.JenkinsJobQuery;
import com.qqchen.deploy.backend.deploy.service.IJenkinsJobService;
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-job")
@Tag(name = "Jenkins工作管理", description = "Jenkins工作管理相关接口")
public class JenkinsJobApiController extends BaseController<JenkinsJob, JenkinsJobDTO, Long, JenkinsJobQuery> {

    @Resource
    private IJenkinsJobService jenkinsJobService;

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

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

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

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

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

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

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

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

    @Operation(summary = "同步Jenkins任务", description = "异步同步，支持两种模式：1)只传externalSystemId-全量同步 2)传externalSystemId+viewId-同步指定视图")
    @PostMapping("/sync")
    public Response<Void> sync(
        @Parameter(description = "外部系统ID", required = true) @RequestParam Long externalSystemId,
        @Parameter(description = "视图ID（可选）", required = false) @RequestParam(required = false) Long viewId
    ) {
        if (viewId != null) {
            // 同步指定视图
            jenkinsJobService.syncJobs(externalSystemId, viewId);
        } else {
            // 全量同步
            jenkinsJobService.syncJobs(externalSystemId);
        }
        return Response.success();
    }

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