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

import com.qqchen.deploy.backend.deploy.entity.ExternalSystem;
import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController;
import com.qqchen.deploy.backend.system.model.ExternalSystemDTO;
import com.qqchen.deploy.backend.system.model.query.ExternalSystemQuery;
import com.qqchen.deploy.backend.deploy.service.IExternalSystemService;
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 jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

/**
 * 外部系统管理API控制器
 * 提供外部系统的REST接口，包括：
 * 1. 基础CRUD操作
 * 2. 连接测试
 * 3. 数据同步
 * 4. 状态管理
 *
 * @author QQChen
 * @version 1.0.0
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/external-system")
@Tag(name = "第三方系统管理API", description = "对外提供的第三方系统管理接口")
public class ExternalSystemApiController extends BaseController<ExternalSystem, ExternalSystemDTO, Long, ExternalSystemQuery> {

    @Resource
    private IExternalSystemService externalSystemService;

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

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

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

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

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

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

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

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

    @Operation(summary = "测试连接")
    @GetMapping("/{id}/test-connection")
    public Response<Boolean> testConnection(
        @Parameter(description = "系统ID", required = true) @PathVariable Long id
    ) {
        return Response.success(externalSystemService.testConnection(id));
    }

    @Operation(summary = "更新状态")
    @PutMapping("/{id}/status")
    public Response<Void> updateStatus(
        @Parameter(description = "系统ID", required = true) @PathVariable Long id,
        @Parameter(description = "是否启用", required = true) @RequestParam boolean enabled
    ) {
        externalSystemService.updateStatus(id, enabled);
        return Response.success();
    }

    @Operation(summary = "获取Jenkins实例信息")
    @GetMapping("/{id}/jenkins-instance")
    public Response<com.qqchen.deploy.backend.deploy.dto.JenkinsInstanceDTO> getJenkinsInstance(
        @Parameter(description = "外部系统ID", required = true) @PathVariable Long id
    ) {
        return Response.success(externalSystemService.getJenkinsInstance(id));
    }

    @Operation(summary = "获取Git实例信息")
    @GetMapping("/{id}/git-instance")
    public Response<com.qqchen.deploy.backend.deploy.dto.GitInstanceDTO> getGitInstance(
        @Parameter(description = "外部系统ID", required = true) @PathVariable Long id
    ) {
        return Response.success(externalSystemService.getGitInstance(id));
    }

    @Override
    protected void exportData(HttpServletResponse response, List<ExternalSystemDTO> data) {
        // TODO: 实现导出功能
    }
} 