package api

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

// Defining the `Point` api.
type Point struct {
	PointBIZ *biz.Point
}

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

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

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

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

// @Tags PointAPI
// @Security ApiKeyAuth
// @Summary Update point record by ID
// @Param id path string true "unique id"
// @Param body body schema.PointForm 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/points/{id} [put]
func (a *Point) Update(c *gin.Context) {
	ctx := c.Request.Context()
	item := new(schema.PointForm)
	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
	}

	err := a.PointBIZ.Update(ctx, c.Param("id"), item)
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}

// @Tags PointAPI
// @Security ApiKeyAuth
// @Summary Delete point 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/points/{id} [delete]
func (a *Point) Delete(c *gin.Context) {
	ctx := c.Request.Context()
	err := a.PointBIZ.Delete(ctx, c.Param("id"))
	if err != nil {
		util.ResError(c, err)
		return
	}
	util.ResOK(c)
}
