package api

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

// Defining the `ProductOrder` api.
type ProductOrder struct {
	ProductOrderBIZ *biz.ProductOrder
}

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

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

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

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

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

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