package com.controllers;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import com.common.service.SyslogService;
import com.common.entity.*;

@RestController
@RequestMapping("/syslog")
@Validated
public class SyslogPushController {

    private static final Logger logger = LoggerFactory.getLogger(SyslogPushController.class);

    @Autowired
    private SyslogService syslogService;

    /**
     * 发送syslog消息
     * POST /syslog/send
     */
    @PostMapping("/send")
    public ApiResponse<String> sendSyslog( @RequestBody SyslogRequest request) {
        logger.info("收到syslog发送请求: {}", request);

        try {
            boolean success = syslogService.sendSyslogMessage(request);
            if (success) {
                logger.info("Syslog消息发送成功: IP={}, Port={}", request.getIp(), request.getPort());
                return ApiResponse.success("Syslog消息发送成功", "消息已成功发送到 " + request.getIp() + ":" + request.getPort());
            } else {
                logger.error("Syslog消息发送失败: IP={}, Port={}", request.getIp(), request.getPort());
                return ApiResponse.error("Syslog消息发送失败", "SYSLOG_SEND_FAILED");
            }

        } catch (Exception e) {
            logger.error("处理syslog请求时发生异常", e);
            return ApiResponse.error("系统内部错误: " + e.getMessage(), "INTERNAL_ERROR");
        }
    }

    /**
     * 批量发送syslog消息
     * POST /api/syslog/batch-send
     */
    @PostMapping("/batch-send")
    public ApiResponse<BatchSendResult> batchSendSyslog(@Valid @RequestBody BatchSyslogRequest request) {
        logger.info("收到批量syslog发送请求，共{}条消息", request.getLogs().size());

        int successCount = 0;
        int failCount = 0;

        for (SyslogRequest logRequest : request.getLogs()) {
            boolean success = syslogService.sendSyslogMessage(logRequest);
            if (success) {
                successCount++;
            } else {
                failCount++;
            }
        }

        BatchSendResult result = new BatchSendResult(successCount, failCount);
        logger.info("批量发送完成: 成功={}, 失败={}", successCount, failCount);

        return ApiResponse.success("批量发送完成", result);
    }

    /**
     * 批量发送请求DTO
     */
    public static class BatchSyslogRequest {
        @Valid
        private java.util.List<SyslogRequest> logs;

        public java.util.List<SyslogRequest> getLogs() {
            return logs;
        }

        public void setLogs(java.util.List<SyslogRequest> logs) {
            this.logs = logs;
        }
    }

    /**
     * 批量发送结果DTO
     */
    public static class BatchSendResult {
        private int successCount;
        private int failCount;

        public BatchSendResult(int successCount, int failCount) {
            this.successCount = successCount;
            this.failCount = failCount;
        }

        // Getter方法
        public int getSuccessCount() {
            return successCount;
        }

        public int getFailCount() {
            return failCount;
        }
    }
}