package com.pangu.web.controller;

import com.pangu.common.core.domain.AjaxResult;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 登录控制器（开发阶段简化版）
 * @author pangu
 */
@RestController
public class LoginController {

    /**
     * 获取验证码
     */
    @GetMapping("/captchaImage")
    public AjaxResult getCaptchaImage() {
        AjaxResult result = AjaxResult.success();
        // 开发阶段禁用验证码
        result.put("captchaEnabled", false);
        result.put("uuid", UUID.randomUUID().toString());
        return result;
    }

    /**
     * 登录
     */
    @PostMapping("/login")
    public AjaxResult login(@RequestBody Map<String, String> loginBody) {
        String username = loginBody.get("username");
        String password = loginBody.get("password");

        // 开发阶段简单校验：admin/admin123
        if ("admin".equals(username) && "admin123".equals(password)) {
            AjaxResult result = AjaxResult.success("登录成功");
            result.put("token", "dev-token-" + UUID.randomUUID().toString());
            return result;
        }
        return AjaxResult.error("用户名或密码错误");
    }

    /**
     * 获取用户信息
     */
    @GetMapping("/getInfo")
    public AjaxResult getInfo() {
        AjaxResult result = AjaxResult.success();

        // 用户信息
        Map<String, Object> user = new HashMap<>();
        user.put("userId", 1L);
        user.put("userName", "admin");
        user.put("nickName", "超级管理员");
        user.put("avatar", "");

        // 角色和权限
        Set<String> roles = new HashSet<>();
        roles.add("admin");
        Set<String> permissions = new HashSet<>();
        permissions.add("*:*:*");

        result.put("user", user);
        result.put("roles", roles);
        result.put("permissions", permissions);
        return result;
    }

    /**
     * 获取路由信息（菜单），与需求文档 3.2 功能模块一致：学校管理、会员管理、学生管理、应用管理、基础数据、系统管理、系统监控、系统工具
     */
    @GetMapping("/getRouters")
    public AjaxResult getRouters() {
        List<Map<String, Object>> menus = new ArrayList<>();

        // 需求文档：学校管理、会员管理、学生管理、应用管理、基础数据（与系统管理同级）。Vue Router 要求 path 以 / 开头
        menus.add(createMenuItem("School", "/school", "school/index", "学校管理", "education"));
        menus.add(createMenuItem("Member", "/member", "member/index", "会员管理", "peoples"));
        menus.add(createMenuItem("Student", "/student", "student/index", "学生管理", "people"));
        menus.add(createMenuItem("Application", "/application", "application/index", "应用管理", "component"));

        // 基础数据（目录，子菜单：年级、班级、学科、区域）
        Map<String, Object> baseData = new HashMap<>();
        baseData.put("name", "Base");
        baseData.put("path", "/base");
        baseData.put("hidden", false);
        baseData.put("redirect", "noRedirect");
        baseData.put("component", "Layout");
        baseData.put("alwaysShow", true);
        baseData.put("meta", createMeta("基础数据", "dict", false, null));
        List<Map<String, Object>> baseChildren = new ArrayList<>();
        baseChildren.add(createMenuItem("Grade", "grade", "base/grade/index", "年级管理", "tree-table"));
        baseChildren.add(createMenuItem("Class", "class", "base/class/index", "班级管理", "list"));
        baseChildren.add(createMenuItem("Subject", "subject", "base/subject/index", "学科管理", "education"));
        baseChildren.add(createMenuItem("Region", "region", "base/region/index", "区域管理", "tree"));
        baseData.put("children", baseChildren);
        menus.add(baseData);

        // RuoYi 系统管理
        Map<String, Object> system = new HashMap<>();
        system.put("name", "System");
        system.put("path", "/system");
        system.put("hidden", false);
        system.put("redirect", "noRedirect");
        system.put("component", "Layout");
        system.put("alwaysShow", true);
        system.put("meta", createMeta("系统管理", "system", false, null));
        List<Map<String, Object>> systemChildren = new ArrayList<>();
        systemChildren.add(createMenuItem("User", "user", "system/user/index", "用户管理", "user"));
        systemChildren.add(createMenuItem("Role", "role", "system/role/index", "角色管理", "peoples"));
        systemChildren.add(createMenuItem("Menu", "menu", "system/menu/index", "菜单管理", "tree-table"));
        systemChildren.add(createMenuItem("Dept", "dept", "system/dept/index", "部门管理", "tree"));
        systemChildren.add(createMenuItem("Post", "post", "system/post/index", "岗位管理", "post"));
        systemChildren.add(createMenuItem("Dict", "dict", "system/dict/index", "字典管理", "dict"));
        systemChildren.add(createMenuItem("Config", "config", "system/config/index", "参数设置", "edit"));
        systemChildren.add(createMenuItem("Notice", "notice", "system/notice/index", "通知公告", "message"));
        system.put("children", systemChildren);
        menus.add(system);

        // RuoYi 系统监控
        Map<String, Object> monitor = new HashMap<>();
        monitor.put("name", "Monitor");
        monitor.put("path", "/monitor");
        monitor.put("hidden", false);
        monitor.put("redirect", "noRedirect");
        monitor.put("component", "Layout");
        monitor.put("alwaysShow", true);
        monitor.put("meta", createMeta("系统监控", "monitor", false, null));
        List<Map<String, Object>> monitorChildren = new ArrayList<>();
        monitorChildren.add(createMenuItem("Online", "online", "monitor/online/index", "在线用户", "online"));
        monitorChildren.add(createMenuItem("Job", "job", "monitor/job/index", "定时任务", "job"));
        monitorChildren.add(createMenuItem("Druid", "druid", "monitor/druid/index", "数据监控", "druid"));
        monitorChildren.add(createMenuItem("Server", "server", "monitor/server/index", "服务监控", "server"));
        monitorChildren.add(createMenuItem("Cache", "cache", "monitor/cache/index", "缓存监控", "redis"));
        monitorChildren.add(createMenuItem("Operlog", "operlog", "monitor/operlog/index", "操作日志", "form"));
        monitorChildren.add(createMenuItem("Logininfor", "logininfor", "monitor/logininfor/index", "登录日志", "logininfor"));
        monitor.put("children", monitorChildren);
        menus.add(monitor);

        // RuoYi 系统工具
        Map<String, Object> tool = new HashMap<>();
        tool.put("name", "Tool");
        tool.put("path", "/tool");
        tool.put("hidden", false);
        tool.put("redirect", "noRedirect");
        tool.put("component", "Layout");
        tool.put("alwaysShow", true);
        tool.put("meta", createMeta("系统工具", "guide", false, null));
        List<Map<String, Object>> toolChildren = new ArrayList<>();
        toolChildren.add(createMenuItem("Gen", "gen", "tool/gen/index", "代码生成", "code"));
        toolChildren.add(createMenuItem("Swagger", "swagger", "tool/swagger/index", "系统接口", "swagger"));
        tool.put("children", toolChildren);
        menus.add(tool);

        return AjaxResult.success(menus);
    }

    private Map<String, Object> createMenuItem(String name, String path, String component, String title, String icon) {
        Map<String, Object> item = new HashMap<>();
        item.put("name", name);
        item.put("path", path);
        item.put("hidden", false);
        item.put("component", component);
        item.put("meta", createMeta(title, icon, false, null));
        return item;
    }

    private Map<String, Object> createMeta(String title, String icon, boolean noCache, String link) {
        Map<String, Object> meta = new HashMap<>();
        meta.put("title", title);
        meta.put("icon", icon);
        meta.put("noCache", noCache);
        if (link != null) {
            meta.put("link", link);
        }
        return meta;
    }

    /**
     * 退出登录
     */
    @PostMapping("/logout")
    public AjaxResult logout() {
        return AjaxResult.success("退出成功");
    }
}
