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

import com.qqchen.deploy.backend.deploy.dto.ServerDTO;
import com.qqchen.deploy.backend.deploy.dto.ServerInfoDTO;
import com.qqchen.deploy.backend.deploy.dto.ServerInitializeDTO;
import com.qqchen.deploy.backend.deploy.entity.Server;
import com.qqchen.deploy.backend.deploy.query.ServerQuery;
import com.qqchen.deploy.backend.deploy.service.IServerService;
import com.qqchen.deploy.backend.framework.api.Response;
import com.qqchen.deploy.backend.framework.controller.BaseController;
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 jakarta.validation.Valid;
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;

/**
 * 服务器管理 Controller
 */
@Slf4j
@RestController
@RequestMapping("/api/v1/server")
@Tag(name = "服务器管理", description = "服务器管理相关接口")
public class ServerApiController
    extends BaseController<Server, ServerDTO, Long, ServerQuery> {

    @Resource
    private IServerService serverService;

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

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

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

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

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

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

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

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

    @Operation(summary = "初始化服务器硬件信息", description = "前端SSH采集服务器硬件信息后回调此接口")
    @PostMapping("/{id}/initialize")
    public Response<ServerDTO> initializeServerInfo(
        @Parameter(description = "服务器ID", required = true) @PathVariable Long id,
        @Parameter(description = "服务器硬件信息", required = true) @Valid @RequestBody ServerInitializeDTO dto
    ) {
        ServerDTO result = serverService.initializeServerInfo(id, dto);
        return Response.success(result);
    }

    @Operation(summary = "测试SSH连接", description = "轻量级连接测试，只验证连通性，不采集硬件信息（性能优化）")
    @PostMapping("/{id}/test-connection")
    public Response<ServerInfoDTO> testConnection(
        @Parameter(description = "服务器ID", required = true) @PathVariable Long id
    ) {
        ServerInfoDTO info = serverService.testConnection(id);
        return Response.success(info);
    }

    @Operation(summary = "采集服务器硬件信息", description = "采集并更新服务器硬件信息（hostname、CPU、内存、磁盘等）")
    @PostMapping("/{id}/collect-hardware")
    public Response<ServerInfoDTO> collectHardwareInfo(
        @Parameter(description = "服务器ID", required = true) @PathVariable Long id
    ) {
        ServerInfoDTO info = serverService.collectHardwareInfo(id);
        return Response.success(info);
    }

    @Override
    protected void exportData(HttpServletResponse response, List<ServerDTO> data) {
        log.info("导出服务器数据，数据量：{}", data.size());
    }
}

