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 `ActivityDetail` api.
type ActivityDetail struct {
	ActivityDetailBIZ *biz.ActivityDetail
}

// @Tags ActivityDetailAPI
// @Security ApiKeyAuth
// @Summary Query activity detail 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.ActivityDetail}
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/activity-details [get]
func (a *ActivityDetail) Query(c *gin.Context) {
	ctx := c.Request.Context()
	var params schema.ActivityDetailQueryParam
	if err := util.ParseQuery(c, &params); err != nil {
		util.ResError(c, err)
		return
	}

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

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

// @Tags ActivityDetailAPI
// @Security ApiKeyAuth
// @Summary Create activity detail record
// @Param body body schema.ActivityDetailForm true "Request body"
// @Success 200 {object} util.ResponseResult{data=schema.ActivityDetail}
// @Failure 400 {object} util.ResponseResult
// @Failure 401 {object} util.ResponseResult
// @Failure 500 {object} util.ResponseResult
// @Router /api/v1/activity-details [post]
func (a *ActivityDetail) Create(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.ActivityDetailForm)
	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.ActivityDetailBIZ.Create(ctx, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResSuccess(c, result)
}

// @Tags ActivityDetailAPI
// @Security ApiKeyAuth
// @Summary Update activity detail record by ID
// @Param id path string true "unique id"
// @Param body body schema.ActivityDetailForm 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/activity-details/{id} [put]
func (a *ActivityDetail) Update(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.ActivityDetailForm)
	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.ActivityDetailBIZ.Update(ctx, id, item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags ActivityDetailAPI
// @Security ApiKeyAuth
// @Summary Delete activity detail 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/activity-details/{id} [delete]
func (a *ActivityDetail) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	id, err := util.GetQueryID(c)
	if err != nil {
		util.ResError(c, err)
		return
	}
	err = a.ActivityDetailBIZ.Delete(ctx, id)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
