package cn.reghao.bnt.web.admin.controller;

import cn.reghao.bnt.web.admin.model.po.SiteOption;
import cn.reghao.bnt.web.admin.model.vo.AccessLogInfo;
import cn.reghao.bnt.web.admin.model.vo.RuntimeLogInfo;
import cn.reghao.bnt.web.admin.service.LogService;
import cn.reghao.bnt.web.admin.service.SiteOptionService;
import cn.reghao.jutil.web.ServletUtil;
import cn.reghao.jutil.web.WebResult;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.domain.Page;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author reghao
 * @date 2021-04-04 21:24:18
 */
@Tag(name = "系统相关页面和接口")
@RestController
@RequestMapping("/api/devops/sys")
@Slf4j
public class SysController {
    private final SiteOptionService siteOptionService;
    private final LogService logService;

    public SysController(SiteOptionService siteOptionService, LogService logService) {
        this.siteOptionService = siteOptionService;
        this.logService = logService;
    }

    @Operation(summary = "站点配置页面", description = "N")
    @GetMapping("/site")
    public String siteOptionPage() {
        Page<SiteOption> page = siteOptionService.getAll();
        return WebResult.success(page.getContent());
    }

    @Operation(summary = "站点配置更新接口", description = "N")
    @PostMapping(value = "/site/update", produces = MediaType.APPLICATION_JSON_VALUE)
    public String updateSiteOption(@Validated SiteOption siteOption) {
        siteOptionService.updateSiteOption(siteOption);
        return WebResult.success();
    }

    @Operation(summary = "系统访问日志页面", description = "N")
    @GetMapping("/log/access")
    public String accessLogPage() {
        int pageNumber = Integer.parseInt(ServletUtil.getRequestParam("pageNo", "1"));
        Page<AccessLogInfo> page = logService.getAccessLogs(pageNumber);
        return "/admin/sys/access_log";
    }

    @Operation(summary = "清空系统访问日志", description = "N")
    @PostMapping(value = "/log/access/erase", produces = MediaType.APPLICATION_JSON_VALUE)
    public String eraseAccessLog() {
        logService.eraseAccessLog();
        return WebResult.success();
    }

    @Operation(summary = "系统运行日志页面", description = "N")
    @GetMapping("/log/runtime")
    public String runtimeLogPage() {
        int pageNumber = Integer.parseInt(ServletUtil.getRequestParam("pageNo", "1"));
        Page<RuntimeLogInfo> page = logService.getRuntimeLogs(pageNumber);
        return "/admin/sys/runtime_log";
    }

    @Operation(summary = "清空系统运行日志", description = "N")
    @PostMapping(value = "/log/runtime/erase", produces = MediaType.APPLICATION_JSON_VALUE)
    public String eraseRuntimeLog() {
        logService.eraseRuntimeLog();
        return WebResult.success();
    }

    @Operation(summary = "系统实时日志页面", description = "N")
    @GetMapping("/log/realtime")
    public String realtimeLogPage() {
        return "/admin/sys/realtime_log";
    }

    @Operation(summary = "状态监控页面", description = "N")
    @GetMapping("/log/monitor")
    public String monitorPage() {
        return "/admin/sys/monitor";
    }
}
