package api

import (
	"github.com/gin-gonic/gin"
	"github.guxuan/haibei/internal/mods/activity/biz"
	"github.guxuan/haibei/internal/mods/activity/schema"
	"github.guxuan/haibei/pkg/util"
)

// Defining the `Activity` api.
type Activity struct {
	ActivityBIZ *biz.Activity
}

// @Tags 活动模块
// @Security ApiKeyAuth
// @Summary Query activity list
// @Param current query int true "pagination index" default(1)
// @Param pageSize query int true "pagination size" default(10)
// @Success 200 {object} util.ResponseResult{data=[]schema.Activity}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/activities [get]
func (a *Activity) Query(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.ActivityQueryParam
	if err := util.ParseQuery(c, &params); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.ActivityBIZ.Query(ctx, params)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResPage(c, result.Data, result.PageResult)
}

// @Tags 活动模块
// @Security ApiKeyAuth
// @Summary Get activity record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult{data=schema.Activity}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/activities/{id} [get]
func (a *Activity) Get(c *gin.Context) {
	ctx := c.Request.Context()
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	item, err := a.ActivityBIZ.Get(ctx, id)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, item)
}

// @Tags 活动模块
// @Security ApiKeyAuth
// @Summary Create activity record
// @Param body body schema.ActivityForm true "Request body"
// @Success 200 {object} util.ResponseResult{data=schema.Activity}
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/activities [post]
func (a *Activity) Create(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.ActivityForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}

	result, err := a.ActivityBIZ.Create(ctx, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, result)
}

// @Tags 活动模块
// @Security ApiKeyAuth
// @Summary Update activity record by ID
// @Param id path string true "unique id"
// @Param body body schema.ActivityForm true "Request body"
// @Success 200 {object} util.ResponseResult
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/activities/{id} [put]
func (a *Activity) Update(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.ActivityForm)
	if err := util.ParseJSON(c, item); err != nil {
		util.ResError(c, err)
		return
	} else if err := item.Validate(); err != nil {
		util.ResError(c, err)
		return
	}
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.ActivityBIZ.Update(ctx, id, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags 活动模块
// @Security ApiKeyAuth
// @Summary Delete activity record by ID
// @Param id path string true "unique id"
// @Success 200 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/activities/{id} [delete]
func (a *Activity) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.ActivityBIZ.Delete(ctx, id)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
