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

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

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

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

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

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

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