package app

import (
	"net/http"
	"strconv"

	"git.echol.cn/loser/st/server/global"
	"git.echol.cn/loser/st/server/model/app/request"
	"git.echol.cn/loser/st/server/model/common"
	commonResponse "git.echol.cn/loser/st/server/model/common/response"
	"git.echol.cn/loser/st/server/service"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
)

type RegexScriptApi struct{}

// CreateRegexScript
// @Tags     AppRegexScript
// @Summary  创建正则脚本
// @Router   /app/regex [post]
// @Security ApiKeyAuth
func (a *RegexScriptApi) CreateRegexScript(c *gin.Context) {
	userID := common.GetAppUserID(c)
	var req request.CreateRegexScriptRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}
	resp, err := service.ServiceGroupApp.AppServiceGroup.RegexScriptService.CreateRegexScript(userID, &req)
	if err != nil {
		global.GVA_LOG.Error("创建正则脚本失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}
	commonResponse.OkWithData(resp, c)
}

// GetRegexScriptList
// @Tags     AppRegexScript
// @Summary  获取正则脚本列表
// @Router   /app/regex [get]
// @Security ApiKeyAuth
func (a *RegexScriptApi) GetRegexScriptList(c *gin.Context) {
	userID := common.GetAppUserID(c)
	var req request.GetRegexScriptListRequest
	req.Page, _ = strconv.Atoi(c.DefaultQuery("page", "1"))
	req.PageSize, _ = strconv.Atoi(c.DefaultQuery("pageSize", "20"))
	req.Keyword = c.Query("keyword")
	if scope := c.Query("scope"); scope != "" {
		scopeInt, _ := strconv.Atoi(scope)
		req.Scope = &scopeInt
	}
	if ownerCharID := c.Query("ownerCharId"); ownerCharID != "" {
		ownerCharIDUint, err := strconv.ParseUint(ownerCharID, 10, 32)
		if err == nil {
			v := uint(ownerCharIDUint)
			req.OwnerCharID = &v
		}
	}

	if req.Page < 1 {
		req.Page = 1
	}
	if req.PageSize < 1 || req.PageSize > 100 {
		req.PageSize = 20
	}

	list, total, err := service.ServiceGroupApp.AppServiceGroup.RegexScriptService.GetRegexScriptList(userID, &req)
	if err != nil {
		global.GVA_LOG.Error("获取正则脚本列表失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}

	commonResponse.OkWithDetailed(commonResponse.PageResult{
		List:     list,
		Total:    total,
		Page:     req.Page,
		PageSize: req.PageSize,
	}, "获取成功", c)
}

// GetRegexScriptByID
// @Tags     AppRegexScript
// @Summary  获取正则脚本详情
// @Router   /app/regex/:id [get]
// @Security ApiKeyAuth
func (a *RegexScriptApi) GetRegexScriptByID(c *gin.Context) {
	userID := common.GetAppUserID(c)
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		commonResponse.FailWithMessage("无效的脚本ID", c)
		return
	}
	resp, err := service.ServiceGroupApp.AppServiceGroup.RegexScriptService.GetRegexScriptByID(userID, uint(id))
	if err != nil {
		global.GVA_LOG.Error("获取正则脚本失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}
	commonResponse.OkWithData(resp, c)
}

// UpdateRegexScript
// @Tags     AppRegexScript
// @Summary  更新正则脚本
// @Router   /app/regex/:id [put]
// @Security ApiKeyAuth
func (a *RegexScriptApi) UpdateRegexScript(c *gin.Context) {
	userID := common.GetAppUserID(c)
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		commonResponse.FailWithMessage("无效的脚本ID", c)
		return
	}
	var req request.UpdateRegexScriptRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}
	if err := service.ServiceGroupApp.AppServiceGroup.RegexScriptService.UpdateRegexScript(userID, uint(id), &req); err != nil {
		global.GVA_LOG.Error("更新正则脚本失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}
	commonResponse.OkWithMessage("更新成功", c)
}

// DeleteRegexScript
// @Tags     AppRegexScript
// @Summary  删除正则脚本
// @Router   /app/regex/:id [delete]
// @Security ApiKeyAuth
func (a *RegexScriptApi) DeleteRegexScript(c *gin.Context) {
	userID := common.GetAppUserID(c)
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		commonResponse.FailWithMessage("无效的脚本ID", c)
		return
	}
	if err := service.ServiceGroupApp.AppServiceGroup.RegexScriptService.DeleteRegexScript(userID, uint(id)); err != nil {
		global.GVA_LOG.Error("删除正则脚本失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}
	commonResponse.OkWithMessage("删除成功", c)
}

// TestRegexScript
// @Tags     AppRegexScript
// @Summary  测试正则脚本
// @Router   /app/regex/:id/test [post]
// @Security ApiKeyAuth
func (a *RegexScriptApi) TestRegexScript(c *gin.Context) {
	userID := common.GetAppUserID(c)
	id, err := strconv.ParseUint(c.Param("id"), 10, 32)
	if err != nil {
		commonResponse.FailWithMessage("无效的脚本ID", c)
		return
	}
	var req request.TestRegexScriptRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}
	resp, err := service.ServiceGroupApp.AppServiceGroup.RegexScriptService.TestRegexScript(userID, uint(id), req.TestString)
	if err != nil {
		global.GVA_LOG.Error("测试正则脚本失败", zap.Error(err))
		commonResponse.FailWithMessage(err.Error(), c)
		return
	}
	c.JSON(http.StatusOK, resp)
}
