package activity

import (
	"github.com/gin-gonic/gin"
	"hongze/hongze_yb/controller/response"
	activity2 "hongze/hongze_yb/models/request/activity"
	"hongze/hongze_yb/services/activity"
	"hongze/hongze_yb/services/user"
)

// RegisterActivity 报名活动
// @Tags 活动模块
// @Summary  报名活动
// @Description 报名活动
// @Security ApiKeyAuth
// @Param Authorization	header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
// @Accept  json
// @Product json
// @Param activity_id query int true "活动ID"
// @Success 200 {string} string "操作成功"
// @failure 400 {string} string "操作失败"
// @failure 4001 {object} activity.RegisterCheckInfo "指定错误"
// @Router /activity/registerActivity [post]
func RegisterActivity(c *gin.Context) {
	var req activity2.RegisterReq
	if c.ShouldBind(&req) != nil {
		response.Fail("参数异常", c)
		return
	}
	userInfo := user.GetInfoByClaims(c)
	ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, req.ActivityID)
	if !ok {
		response.AuthError(permissionCheckInfo, "暂无权限", c)
		return
	}
	err, registerCheck := activity.CreateRegister(req.ActivityID, userInfo)
	if err != nil {
		if registerCheck.Specific == 1 {
			response.SpecificFail(registerCheck, registerCheck.Msg, c)
		} else {
			response.Fail(err.Error(), c)
		}
		return
	}
	response.OkData("操作成功", "", c)
}

// CancelRegister 取消报名
// @Tags 活动模块
// @Summary  取消报名
// @Description 取消报名
// @Security ApiKeyAuth
// @Param Authorization	header string true "Bearer 31a165baebe6dec616b1f8f3207b4273"
// @Accept  json
// @Product json
// @Param activity_id query int true "活动ID"
// @Success 200 {string} string "操作成功"
// @failure 400 {string} string "操作失败"
// @Router /activity/cancelRegister [post]
func CancelRegister(c *gin.Context) {
	var req activity2.RegisterReq
	if c.ShouldBind(&req) != nil {
		response.Fail("参数异常", c)
		return
	}
	userInfo := user.GetInfoByClaims(c)
	ok, permissionCheckInfo, err := activity.CheckActivityPermission(userInfo, req.ActivityID)
	if !ok {
		response.AuthError(permissionCheckInfo, "暂无权限", c)
		return
	}
	err = activity.CancelRegister(req.ActivityID, userInfo)
	if err != nil {
		response.Fail(err.Error(), c)
		return
	}
	response.OkData("操作成功", "", c)
}