// Code generated by hertz generator.

package product

import (
	"apis/biz/utils"
	"apis/rpc"
	"context"
	"github.com/cloudwego/kitex/pkg/klog"
	"strconv"

	product "apis/hertz_gen/api/product"
	product_kitex "github.com/BeroKiTeer/MyGoMall/common/kitex_gen/product"
	"github.com/cloudwego/hertz/pkg/app"
	"github.com/cloudwego/hertz/pkg/protocol/consts"
)

// ListProducts .
// @router /api/products [GET]
func ListProducts(ctx context.Context, c *app.RequestContext) {
	var err error
	var req product.ListProductsReq
	err = c.BindAndValidate(&req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	klog.Infof("ListProducts req: %#v", req)
	r, err := rpc.ProductClient.ListProducts(ctx, &product_kitex.ListProductsReq{
		Page:         req.Page,
		PageSize:     req.PageSize,
		CategoryName: req.CategoryName,
	})
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}
	resp := new(product.ListProductsResp)
	for _, an := range r.Products {
		resp.Products = append(resp.Products, &product.Product{
			Id:            an.Id,
			Name:          an.Name,
			Description:   an.Description,
			Price:         an.Price,
			OriginalPrice: an.OriginalPrice,
			Stock:         an.Stock,
			Images:        an.Images,
			Status:        an.Status,
			Categories:    an.Categories,
		})
	}
	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// SearchProducts .
// @router /api/products/search [GET]
func SearchProducts(ctx context.Context, c *app.RequestContext) {
	var err error
	var req product.SearchProductsReq
	err = c.BindAndValidate(&req)
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		klog.Error(err)
		return
	}

	r, err := rpc.ProductClient.SearchProducts(ctx, &product_kitex.SearchProductsReq{
		Name: req.Name,
	})
	if err != nil {
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		klog.Error(err)
		return
	}
	ans := r.Results
	resp := new(product.SearchProductsResp)
	for _, an := range ans {
		resp.Results = append(resp.Results, &product.Product{
			Id:            an.Id,
			Name:          an.Name,
			Description:   an.Description,
			Price:         an.Price,
			OriginalPrice: an.OriginalPrice,
			Stock:         an.Stock,
			Images:        an.Images,
			Status:        an.Status,
			Categories:    an.Categories,
		})
	}
	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// CreateProduct .
// @router /api/products [POST]
func CreateProduct(ctx context.Context, c *app.RequestContext) {
	var err error
	var req product.CreateProductReq
	err = c.BindAndValidate(&req)
	err = utils.BindJson(c, &req.Product)
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	r, err := rpc.ProductClient.CreateProduct(ctx, &product_kitex.CreateProductReq{
		Product: &product_kitex.Product{
			Name:          req.Product.Name,
			Description:   req.Product.Description,
			Price:         req.Product.Price,
			OriginalPrice: req.Product.OriginalPrice,
			Stock:         req.Product.Stock,
			Images:        req.Product.Images,
			Categories:    req.Product.Categories,
		},
	})
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}
	resp := new(product.CreateProductResp)
	resp.ProductId = r.ProductId

	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// UpdateProduct .
// @router /api/products/{id} [PUT]
func UpdateProduct(ctx context.Context, c *app.RequestContext) {
	var err error
	var req product.UpdateProductReq
	err = c.BindAndValidate(&req)
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	ans := req.Product
	id, _ := strconv.ParseInt(c.Param("id"), 2, 64)
	arg := &product_kitex.UpdateProductReq{
		Product: &product_kitex.Product{
			Id:            id,
			Name:          ans.Name,
			Description:   ans.Description,
			Price:         ans.Price,
			OriginalPrice: ans.OriginalPrice,
			Stock:         ans.Stock,
			Images:        ans.Images,
			Status:        ans.Status,
			Categories:    ans.Categories,
		},
	}
	r, err := rpc.ProductClient.UpdateProduct(ctx, arg)
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}
	resp := new(product.UpdateProductResp)
	resp.Success = r.Success

	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// DeleteProduct .
// @router /api/products/{id} [DELETE]
func DeleteProduct(ctx context.Context, c *app.RequestContext) {
	var err error
	var req product.DeleteProductReq
	err = c.BindAndValidate(&req)
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	id, _ := strconv.ParseInt(c.Param("id"), 2, 64)
	r, err := rpc.ProductClient.DeleteProduct(ctx, &product_kitex.DeleteProductReq{
		Id: id,
	})
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}
	resp := new(product.DeleteProductResp)
	resp.Success = r.Success

	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// GetProduct .
// @router /api/products/{id} [GET]
func GetProduct(ctx context.Context, c *app.RequestContext) {
	var err error
	var req product.GetProductReq
	err = c.BindAndValidate(&req)
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	id, _ := strconv.ParseInt(c.Param("id"), 2, 64)
	r, err := rpc.ProductClient.GetProduct(ctx, &product_kitex.GetProductReq{
		Id: id,
	})
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}
	ans := r.Product
	resp := new(product.GetProductResp)
	resp.Product = &product.Product{
		Id:            ans.Id,
		Name:          ans.Name,
		Description:   ans.Description,
		Price:         ans.Price,
		OriginalPrice: ans.OriginalPrice,
		Stock:         ans.Stock,
		Images:        ans.Images,
		Status:        ans.Status,
		Categories:    ans.Categories,
	}

	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}

// GetProductsBatch .
// @router /api/products/batch [GET]
func GetProductsBatch(ctx context.Context, c *app.RequestContext) {
	var err error
	var req product.GetProductsBatchReq
	err = c.BindAndValidate(&req)
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusBadRequest, err)
		return
	}
	r, err := rpc.ProductClient.GetProductsBatch(ctx, &product_kitex.GetProductsBatchReq{
		Ids: req.GetIds(),
	})
	if err != nil {
		klog.Error(err)
		utils.SendErrResponse(ctx, c, consts.StatusServiceUnavailable, err)
		return
	}
	ans := r.Products
	resp := new(product.GetProductsBatchResp)
	for _, an := range ans {
		resp.Products = append(resp.Products, &product.Product{
			Id:            an.Id,
			Name:          an.Name,
			Description:   an.Description,
			Price:         an.Price,
			OriginalPrice: an.OriginalPrice,
			Stock:         an.Stock,
			Images:        an.Images,
			Status:        an.Status,
			Categories:    an.Categories,
		})
	}
	utils.SendSuccessResponse(ctx, c, consts.StatusOK, resp)
}
