package com.controllers;


import com.common.entity.SyslogNonNormalMessage;
import com.common.service.SyslogNonNormalMessageService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.time.LocalDateTime;
import java.util.List;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/syslog/non-normal")
@RequiredArgsConstructor

public class SyslogNonNormalMessageController {
    private final SyslogNonNormalMessageService messageService;

    /**
     * 接收单条非标日志
     */
    @PostMapping("/message")
    public ResponseEntity<Boolean> receiveMessage(@Valid @RequestBody SyslogNonNormalMessage message) {
        log.info("接收到非标日志消息: {}", message.getSyslogUuid());
        boolean result = messageService.saveMessage(message);
        return ResponseEntity.ok(result);
    }


    /**
     * 新增非标日志
     */
    @PostMapping("/save")

    public ResponseEntity<Map<String, Object>> saveMessage(@RequestBody SyslogNonNormalMessage message) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = messageService.saveMessage(message);
            if (success) {
                result.put("success", true);
                result.put("message", "保存成功");
                result.put("data", message.getId());
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "保存失败");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            log.error("保存非标日志异常: {}", e.getMessage(), e);
            result.put("success", false);
            result.put("message", "保存异常: " + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }
    /**
     * 根据ID查询
     */
    @GetMapping("/message/{id}")
    public ResponseEntity<SyslogNonNormalMessage> getMessage(@PathVariable String id) {
        SyslogNonNormalMessage message = messageService.getById(id);
        return ResponseEntity.ok(message);
    }


    /**
     * 批量新增非标日志
     */
    @PostMapping("/batch-save")

    public ResponseEntity<Map<String, Object>> batchSaveMessages(@RequestBody List<SyslogNonNormalMessage> messages) {
        Map<String, Object> result = new HashMap<>();
        try {
            int count = messageService.saveBatchMessages(messages);
            result.put("success", true);
            result.put("message", "批量保存成功");
            result.put("data", count);
            return ResponseEntity.ok(result);
        } catch (Exception e) {
            log.error("批量保存非标日志异常: {}", e.getMessage(), e);
            result.put("success", false);
            result.put("message", "批量保存异常: " + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }
    /**
     * 删除非标日志
     */
    //@DeleteMapping("/delete/{id}")
    public ResponseEntity<Map<String, Object>> deleteMessage(
           @PathVariable String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            boolean success = messageService.deleteMessage(id);
            if (success) {
                result.put("success", true);
                result.put("message", "删除成功");
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "删除失败，记录不存在");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            log.error("删除非标日志异常: {}", e.getMessage(), e);
            result.put("success", false);
            result.put("message", "删除异常: " + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }

    /**
     * 查询非标日志详情
     */
    @GetMapping("/detail/{id}")
    public ResponseEntity<Map<String, Object>> getMessageDetail(
            @PathVariable String id) {
        Map<String, Object> result = new HashMap<>();
        try {
            SyslogNonNormalMessage message = messageService.getMessageById(id);
            if (message != null) {
                result.put("success", true);
                result.put("message", "查询成功");
                result.put("data", message);
                return ResponseEntity.ok(result);
            } else {
                result.put("success", false);
                result.put("message", "记录不存在或已被删除");
                return ResponseEntity.badRequest().body(result);
            }
        } catch (Exception e) {
            log.error("查询非标日志详情异常: {}", e.getMessage(), e);
            result.put("success", false);
            result.put("message", "查询异常: " + e.getMessage());
            return ResponseEntity.internalServerError().body(result);
        }
    }
}