package cn.reghao.tnb.admin.controller;

import cn.reghao.tnb.admin.service.LoggingService;
import cn.reghao.tnb.admin.service.RuntimeLogService;
import cn.reghao.tnb.common.db.SelectOption;
import cn.reghao.tnb.common.web.WebResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author reghao
 * @date 2025-04-16 10:02:27
 */
@Tag(name = "tnb 日志接口")
@RestController
@RequestMapping("/api/admin1/tnb/sys")
public class TnbLogController {
    private final RuntimeLogService runtimeLogService;
    private final LoggingService loggingService;

    public TnbLogController(RuntimeLogService runtimeLogService, LoggingService loggingService) {
        this.runtimeLogService = runtimeLogService;
        this.loggingService = loggingService;
    }


    @Operation(summary = "网关日志页面")
    @GetMapping(value = "/log", produces = MediaType.APPLICATION_JSON_VALUE)
    public String gatewayLogPage() {
        return WebResult.success();
    }

    @Operation(summary = "访问日志页面")
    @GetMapping("/accesslog")
    public String accessLogPage() {
        return WebResult.success();
    }

    @Operation(summary = "运行日志页面", description = "N")
    @GetMapping("/runtimelog")
    public String rtLogPage(@RequestParam(value = "app", required = false) String app,
                            @RequestParam(value = "host", required = false) String host) {
        List<SelectOption> appList;
        List<SelectOption> hostList;
        Map<String, List<String>> map = runtimeLogService.getAppHostMap();
        if (!map.isEmpty()) {
            appList = map.keySet().stream()
                    .map(appName -> new SelectOption(appName, appName))
                    .collect(Collectors.toList());
            if (app == null) {
                app = appList.get(0).getValue();
            }

            hostList = map.get(app).stream()
                    .map(host1 -> new SelectOption(host1, host1))
                    .collect(Collectors.toList());
            if (host == null) {
                host = hostList.get(0).getValue();
            }
        } else {
            appList = Collections.emptyList();
            hostList = Collections.emptyList();
        }

        runtimeLogService.getAppLogs(app, host);
        /*model.addAttribute("app", app);
        model.addAttribute("host", host);
        model.addAttribute("appList", appList);
        model.addAttribute("hostList", hostList);*/
        return WebResult.success();
    }

    @Operation(summary = "前一页运行日志", description = "N")
    @GetMapping(value = "/runtimelog/prev", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String prevRtLog(@RequestParam(value = "app") String app,
                          @RequestParam(value = "host") String host,
                          @RequestParam(value = "prevId") String prevId) {
        runtimeLogService.getPrevLogs(app, host, prevId);
        return WebResult.success();
    }

    @Operation(summary = "后一页运行日志", description = "N")
    @GetMapping(value = "/runtimelog/next", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String nextRtLog(@RequestParam(value = "app") String app,
                          @RequestParam(value = "host") String host,
                          @RequestParam(value = "nextId") String nextId) {
        runtimeLogService.getNextLogs(app, host, nextId);
        return WebResult.success();
    }

    @Operation(summary = "Nginx 日志页面", description = "N")
    @GetMapping(value = "/log/nginx")
    public String nginxLog() {
        return "/devops/app/stat/nginxlog";
    }

    @Operation(summary = "Nginx 日志的 echarts 数据", description = "N")
    @GetMapping(value = "/log/nginx/chart", produces = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public String nginxLogData() throws ParseException {
        List list = loggingService.getChartData();
        return WebResult.success(list);
    }
}
