package controller

import (
	"ems.agt/src/framework/constants/cachekey"
	"ems.agt/src/framework/redis"
	"ems.agt/src/framework/vo/result"
	"ems.agt/src/modules/monitor/model"

	"github.com/gin-gonic/gin"
)

// 实例化控制层 SysCacheController 结构体
var NewSysCache = &SysCacheController{}

// 缓存监控信息
//
// PATH /monitor/cache
type SysCacheController struct{}

// Redis信息
//
// GET /
func (s *SysCacheController) Info(c *gin.Context) {
	c.JSON(200, result.OkData(map[string]any{
		"info":         redis.Info(""),
		"dbSize":       redis.KeySize(""),
		"commandStats": redis.CommandStats(""),
	}))
}

// 缓存名称列表
//
// GET /getNames
func (s *SysCacheController) Names(c *gin.Context) {
	caches := []model.SysCache{
		model.NewSysCacheNames("user", cachekey.LOGIN_TOKEN_KEY),
		model.NewSysCacheNames("configuration", cachekey.SYS_CONFIG_KEY),
		model.NewSysCacheNames("dictionary", cachekey.SYS_DICT_KEY),
		model.NewSysCacheNames("captcha", cachekey.CAPTCHA_CODE_KEY),
		model.NewSysCacheNames("anti-submission", cachekey.REPEAT_SUBMIT_KEY),
		model.NewSysCacheNames("current-limiting", cachekey.RATE_LIMIT_KEY),
		model.NewSysCacheNames("password-errors-number", cachekey.PWD_ERR_CNT_KEY),
	}
	c.JSON(200, result.OkData(caches))
}

// 缓存名称下键名列表
//
// GET /getKeys/:cacheName
func (s *SysCacheController) Keys(c *gin.Context) {
	cacheName := c.Param("cacheName")
	if cacheName == "" {
		c.JSON(400, result.CodeMsg(400, "parameter error"))
		return
	}
	caches := []model.SysCache{}

	// 遍历组装
	cacheKeys, _ := redis.GetKeys("", cacheName+":*")
	for _, key := range cacheKeys {
		caches = append(caches, model.NewSysCacheKeys(cacheName, key))
	}

	c.JSON(200, result.OkData(caches))
}

// 缓存内容
//
// GET /getValue/:cacheName/:cacheKey
func (s *SysCacheController) Value(c *gin.Context) {
	cacheName := c.Param("cacheName")
	cacheKey := c.Param("cacheKey")
	if cacheName == "" || cacheKey == "" {
		c.JSON(400, result.CodeMsg(400, "parameter error"))
		return
	}

	cacheValue, err := redis.Get("", cacheName+":"+cacheKey)
	if err != nil {
		c.JSON(200, result.ErrMsg(err.Error()))
		return
	}
	sysCache := model.NewSysCacheValue(cacheName, cacheKey, cacheValue)
	c.JSON(200, result.OkData(sysCache))
}

// 删除缓存名称下键名列表
//
// DELETE /clearCacheName/:cacheName
func (s *SysCacheController) ClearCacheName(c *gin.Context) {
	cacheName := c.Param("cacheName")
	if cacheName == "" {
		c.JSON(400, result.CodeMsg(400, "parameter error"))
		return
	}

	cacheKeys, err := redis.GetKeys("", cacheName+":*")
	if err != nil {
		c.JSON(200, result.ErrMsg(err.Error()))
		return
	}
	ok, _ := redis.DelKeys("", cacheKeys)
	if ok {
		c.JSON(200, result.Ok(nil))
		return
	}
	c.JSON(200, result.Err(nil))
}

// 删除缓存键名
//
// DELETE /clearCacheKey/:cacheName/:cacheKey
func (s *SysCacheController) ClearCacheKey(c *gin.Context) {
	cacheName := c.Param("cacheName")
	cacheKey := c.Param("cacheKey")
	if cacheName == "" || cacheKey == "" {
		c.JSON(400, result.CodeMsg(400, "parameter error"))
		return
	}

	ok, _ := redis.Del("", cacheName+":"+cacheKey)
	if ok {
		c.JSON(200, result.Ok(nil))
		return
	}
	c.JSON(200, result.Err(nil))
}

// 安全清理缓存名称
//
// DELETE /clearCacheSafe
func (s *SysCacheController) ClearCacheSafe(c *gin.Context) {
	caches := []model.SysCache{
		model.NewSysCacheNames("configuration", cachekey.SYS_CONFIG_KEY),
		model.NewSysCacheNames("dictionary", cachekey.SYS_DICT_KEY),
		model.NewSysCacheNames("captcha", cachekey.CAPTCHA_CODE_KEY),
		model.NewSysCacheNames("anti-submission", cachekey.REPEAT_SUBMIT_KEY),
		model.NewSysCacheNames("current-limiting", cachekey.RATE_LIMIT_KEY),
		model.NewSysCacheNames("password-errors-number", cachekey.PWD_ERR_CNT_KEY),
	}
	for _, v := range caches {
		cacheKeys, err := redis.GetKeys("", v.CacheName+":*")
		if err != nil {
			continue
		}
		redis.DelKeys("", cacheKeys)
	}
	c.JSON(200, result.Ok(nil))
}
