package com.mini.capi.biz;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.mini.capi.api.service.sysService;
import com.mini.capi.biz.domain.*;
import com.mini.capi.biz.service.*;
import com.mini.capi.model.info.RunInfo;
import com.mini.capi.utils.DateUtils;
import com.mini.capi.utils.SqlUtils;
import com.mini.capi.utils.vDate;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpSession;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

@Controller
public class viewController {


    @Resource
    private BizMonitorHostService hostService;


    @Resource
    private BizQuickLoginService loginService;

    @Resource
    private BizFilesService filesService;

    @Resource
    private BizFileFoldersService foldersService;

    @Resource
    private sysService service;


    @Resource
    private BizServerInfoService bizServerInfoService;

    @Resource
    private BizDeviceInfoService bizDeviceInfoService;

    @Resource
    private DataTableInfoService dataTableInfoService;

    @Resource
    private DataTableFieldService dataTableFieldService;

    @Resource
    private BizTodoTaskViewService bizTodoTaskViewService;

    @Resource
    private BizBaseNoticeViewService bizBaseNoticeViewService;


    @GetMapping("/login")
    public String showLoginPage() {
        return "login";
    }


    @GetMapping("/swagger")
    public String redirectToSwagger() {
        return "redirect:/swagger-ui/index.html";
    }

    /**
     * 首页
     */
    @GetMapping("/biz/index")
    public String getUserHome(Model model, HttpSession session) throws Exception {
        RunInfo runInfo = service.getRunInfo();
        List<BizMonitorHost> hosts = hostService.list();
        List<BizQuickLogin> storages = loginService.list();
        BizHomeUser user = (BizHomeUser) session.getAttribute("currentUser");
        QueryWrapper<BizTodoTaskView> taskViewQueryWrapper = new QueryWrapper<>();
        taskViewQueryWrapper.notIn("ustatus", "CPT");
        List<BizTodoTaskView> todoViews = bizTodoTaskViewService.list(taskViewQueryWrapper);

        List<BizBaseNoticeView> noticeViews = bizBaseNoticeViewService.list();

        model.addAttribute("hosts", hosts);
        model.addAttribute("storages", storages);
        model.addAttribute("uname", user.getUname());
        model.addAttribute("todoViews", todoViews);
        model.addAttribute("noticeViews", noticeViews);
        model.addAttribute("times", vDate.getRunTimes(runInfo.getProcessInfo().getUptime()));
        return "index";
    }

    /**
     * 主机运行状态
     */
    @GetMapping("/biz/getServerInfo")
    public String getServerInfo(Model model, String hostId) {
        // 1. 查询服务器信息，确保不为null
        QueryWrapper<BizServerInfo> infoQueryWrapper = new QueryWrapper<>();
        infoQueryWrapper.eq("host_id", hostId);
        BizServerInfo info = bizServerInfoService.getOne(infoQueryWrapper);
        // 若查询不到数据，创建空对象避免前端null指针
        model.addAttribute("info", info != null ? info : new BizServerInfo());
        // 2. 查询设备列表，确保集合始终存在
        QueryWrapper<BizDeviceInfo> deviceQueryWrapper = new QueryWrapper<>();
        deviceQueryWrapper.eq("host_id", hostId);
        List<BizDeviceInfo> devices = bizDeviceInfoService.list(deviceQueryWrapper);
        // 若查询不到数据，返回空集合而非null
        model.addAttribute("devices", devices != null ? devices : Collections.emptyList());
        return "server";
    }

    /**
     * 数据地图
     */
    @GetMapping("/biz/dataMap")
    public String getDataMap(Model model) {
        QueryWrapper<DataTableInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("ds", DateUtils.dsValue());
        List<DataTableInfo> tables = dataTableInfoService.list(queryWrapper);
        tables.sort(Comparator.nullsLast(Comparator.comparing(DataTableInfo::getCreateTime)).reversed());
        model.addAttribute("tables", tables);
        return "data";
    }

    /**
     * 数据地图明细
     */
    @GetMapping("/biz/getFieldDetail")
    public String getFieldDetail(Model model, String tableId) {
        DataTableInfo tableInfo = dataTableInfoService.getById(tableId);
        QueryWrapper<DataTableField> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("table_id", tableId).eq("ds", DateUtils.dsValue());
        queryWrapper.orderByAsc("field_order");
        List<DataTableField> fields = dataTableFieldService.list(queryWrapper);
        String createSql = SqlUtils.CreateTableSql(tableInfo, fields);
        String selectSql = SqlUtils.SelectSqlComments(tableInfo, fields);
        model.addAttribute("createSql", createSql);
        model.addAttribute("selectSql", selectSql);
        model.addAttribute("tableInfo", tableInfo);
        model.addAttribute("fields", fields);
        return "field";
    }

    /**
     * 文档中心
     */

    @GetMapping("/biz/dataDoc")
    public String getDataDox(Model model) {
        List<BizFiles> files = filesService.list();
        List<BizFileFolders> folders = foldersService.list();

        model.addAttribute("files", files);
        model.addAttribute("folders", folders);
        return "file";
    }

    /**
     * 系统管理
     */
    @GetMapping("/biz/system")
    public String getSystem(Model model) {

        return "system";
    }

}
