package admin

import (
	"net/http"
	"strconv"
	"time"

	"bindbox-game/internal/code"
	"bindbox-game/internal/pkg/core"
	"bindbox-game/internal/pkg/validation"
	activitysvc "bindbox-game/internal/service/activity"
)

type createActivityRequest struct {
	Name               string `json:"name" binding:"required"`
	Banner             string `json:"banner"`
	ActivityCategoryID int64  `json:"activity_category_id" binding:"required"`
	Status             int32  `json:"status"`
	PriceDraw          int64  `json:"price_draw"`
	IsBoss             int32  `json:"is_boss"`
	StartTime          string `json:"start_time"`
	EndTime            string `json:"end_time"`
}

type createActivityResponse struct {
	ID      int64  `json:"id"`
	Message string `json:"message"`
}

// CreateActivity 创建活动
// @Summary 创建活动
// @Description 创建活动，配置基本信息与分类、Boss标签
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param RequestBody body createActivityRequest true "请求参数"
// @Success 200 {object} simpleMessageResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities [post]
// @Security LoginVerifyToken
func (h *handler) CreateActivity() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(createActivityRequest)
		res := new(createActivityResponse)

		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}

		if req.ActivityCategoryID == 0 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "活动分类ID不能为空"))
			return
		}

		if ctx.SessionUserInfo().IsSuper != 1 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateActivityError, "禁止操作"))
			return
		}

		var st, et *time.Time
		if req.StartTime != "" {
			if t, err := time.Parse(time.RFC3339, req.StartTime); err == nil {
				st = &t
			}
		}
		if req.EndTime != "" {
			if t, err := time.Parse(time.RFC3339, req.EndTime); err == nil {
				et = &t
			}
		}

		item, err := h.activity.CreateActivity(ctx.RequestContext(), activitysvc.CreateActivityInput{
			Name:               req.Name,
			Banner:             req.Banner,
			ActivityCategoryID: req.ActivityCategoryID,
			Status:             req.Status,
			PriceDraw:          req.PriceDraw,
			IsBoss:             req.IsBoss,
			StartTime:          st,
			EndTime:            et,
		})
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.CreateActivityError, err.Error()))
			return
		}

		res.ID = item.ID
		res.Message = "操作成功"
		ctx.Payload(res)
	}
}

type modifyActivityRequest struct {
	Name               string `json:"name"`
	Banner             string `json:"banner"`
	ActivityCategoryID int64  `json:"activity_category_id"`
	Status             int32  `json:"status"`
	PriceDraw          int64  `json:"price_draw"`
	IsBoss             int32  `json:"is_boss"`
	StartTime          string `json:"start_time"`
	EndTime            string `json:"end_time"`
}

// ModifyActivity 修改活动
// @Summary 修改活动
// @Description 修改活动基本信息、分类、Boss标签等
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Param RequestBody body modifyActivityRequest true "请求参数"
// @Success 200 {object} simpleMessageResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities/{activity_id} [put]
// @Security LoginVerifyToken
func (h *handler) ModifyActivity() core.HandlerFunc {
	return func(ctx core.Context) {
		req := new(modifyActivityRequest)
		res := new(simpleMessageResponse)

		if err := ctx.ShouldBindJSON(req); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, validation.Error(err)))
			return
		}

		if ctx.SessionUserInfo().IsSuper != 1 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyActivityError, "禁止操作"))
			return
		}

		id, err := strconv.ParseInt(ctx.Param("activity_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递活动ID"))
			return
		}

		var st, et *time.Time
		if req.StartTime != "" {
			if t, err := time.Parse(time.RFC3339, req.StartTime); err == nil {
				st = &t
			}
		}
		if req.EndTime != "" {
			if t, err := time.Parse(time.RFC3339, req.EndTime); err == nil {
				et = &t
			}
		}

		if err := h.activity.ModifyActivity(ctx.RequestContext(), id, activitysvc.ModifyActivityInput{
			Name:               req.Name,
			Banner:             req.Banner,
			ActivityCategoryID: req.ActivityCategoryID,
			Status:             req.Status,
			PriceDraw:          req.PriceDraw,
			IsBoss:             req.IsBoss,
			StartTime:          st,
			EndTime:            et,
		}); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ModifyActivityError, err.Error()))
			return
		}

		res.Message = "操作成功"
		ctx.Payload(res)
	}
}

// DeleteActivity 删除活动
// @Summary 删除活动
// @Description 删除指定活动
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Success 200 {object} simpleMessageResponse
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities/{activity_id} [delete]
// @Security LoginVerifyToken
func (h *handler) DeleteActivity() core.HandlerFunc {
	return func(ctx core.Context) {
		res := new(simpleMessageResponse)

		if ctx.SessionUserInfo().IsSuper != 1 {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.DeleteActivityError, "禁止操作"))
			return
		}

		id, err := strconv.ParseInt(ctx.Param("activity_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递活动ID"))
			return
		}

		if err := h.activity.DeleteActivity(ctx.RequestContext(), id); err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.DeleteActivityError, err.Error()))
			return
		}

		res.Message = "操作成功"
		ctx.Payload(res)
	}
}

// GetActivityDetail 查看活动详情
// @Summary 查看活动详情
// @Description 查看指定活动的详细信息
// @Tags 管理端.活动
// @Accept json
// @Produce json
// @Param activity_id path integer true "活动ID"
// @Success 200 {object} model.Activities
// @Failure 400 {object} code.Failure
// @Router /api/admin/activities/{activity_id} [get]
// @Security LoginVerifyToken
func (h *handler) GetActivityDetail() core.HandlerFunc {
	return func(ctx core.Context) {
		id, err := strconv.ParseInt(ctx.Param("activity_id"), 10, 64)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.ParamBindError, "未传递活动ID"))
			return
		}
		item, err := h.activity.GetActivity(ctx.RequestContext(), id)
		if err != nil {
			ctx.AbortWithError(core.Error(http.StatusBadRequest, code.GetActivityError, err.Error()))
			return
		}
		ctx.Payload(item)
	}
}
