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.JenkinsView;
import com.qqchen.deploy.backend.deploy.dto.JenkinsViewDTO;
import com.qqchen.deploy.backend.deploy.query.JenkinsViewQuery;
import com.qqchen.deploy.backend.deploy.service.IJenkinsViewService;
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-view")
@Tag(name = "Jenkins视图管理", description = "Jenkins视图管理相关接口")
public class JenkinsViewApiController extends BaseController<JenkinsView, JenkinsViewDTO, Long, JenkinsViewQuery> {

    @Resource
    private IJenkinsViewService jenkinsViewService;

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

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

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

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

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

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

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

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

    @Operation(summary = "同步Jenkins视图", description = "异步同步指定外部系统的所有Jenkins视图，立即返回")
    @PostMapping("/sync")
    public Response<Void> sync(
        @Parameter(description = "外部系统ID", required = true) @RequestParam Long externalSystemId
    ) {
        jenkinsViewService.syncViews(externalSystemId);
        return Response.success();
    }

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