package logic

import (
	"context"
	"encoding/json"
	"errors"
	"fusenapi/constants"
	"fusenapi/model/gmodel"
	"fusenapi/server/shopping-cart/internal/svc"
	"fusenapi/server/shopping-cart/internal/types"
	"fusenapi/utils/auth"
	"fusenapi/utils/basic"
	"fusenapi/utils/shopping_cart"
	"gorm.io/gorm"
	"time"

	"github.com/zeromicro/go-zero/core/logx"
)

type AddToCartLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewAddToCartLogic(ctx context.Context, svcCtx *svc.ServiceContext) *AddToCartLogic {
	return &AddToCartLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// 处理进入前逻辑w,r
// func (l *AddToCartLogic) BeforeLogic(w http.ResponseWriter, r *http.Request) {
// }

func (l *AddToCartLogic) AddToCart(req *types.AddToCartReq, userinfo *auth.UserInfo) (resp *basic.Response) {
	if !userinfo.IsUser() {
		return resp.SetStatusWithMessage(basic.CodeUnAuth, "please sign in")
	}
	//校验参数
	if err := l.AddToCartParamVerify(req); err != nil {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, err.Error())
	}
	//查询该用户购物车数量
	cartCount, err := l.svcCtx.AllModels.FsShoppingCart.CountUserCart(l.ctx, userinfo.UserId)
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get the count of your shopping cart")
	}
	if cartCount >= 100 {
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "sorry,the count of your carts can`t greater than 100")
	}
	//获取产品是否存在
	productInfo, err := l.svcCtx.AllModels.FsProduct.FindOne(l.ctx, req.ProductId)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the product is not exists")
		}
		logx.Error("获取产品信息错误:", err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product info")
	}
	var (
		templateJson string //模板表的记录中的json设计信息
		templateTag  string //模板表的模板标签
		fittingJson  string //配件的json设计信息
		fittingName  string //配件名
	)
	//有模板
	if req.TemplateId > 0 {
		templateInfo, err := l.svcCtx.AllModels.FsProductTemplateV2.FindOne(l.ctx, req.TemplateId)
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the template is not exists")
			}
			logx.Error("获取模板信息错误:", err)
			return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template info")
		}
		if *templateInfo.IsDel == 1 {
			return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template is deleted")
		}
		if *templateInfo.Status != 1 {
			return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template`s status is unNormal")
		}
		if templateInfo.TemplateInfo == nil || *templateInfo.TemplateInfo == "" {
			return resp.SetStatusWithMessage(basic.CodeServiceErr, "the template`s design info is empty")
		}
		templateJson = *templateInfo.TemplateInfo
		templateTag = *templateInfo.TemplateTag
	}
	//有配件
	if req.FittingId > 0 {
		fittingInfo, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, req.FittingId)
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the fitting is not exists")
			}
			logx.Error("获取配件信息错误:", err)
			return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get fitting info")
		}
		if *fittingInfo.Status != 1 {
			return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the fitting`s status is unNormal")
		}
		if fittingInfo.ModelInfo == nil || *fittingInfo.ModelInfo == "" {
			return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the fitting`s design info is empty")
		}
		fittingJson = *fittingInfo.ModelInfo
		fittingName = *fittingInfo.Title
	}
	//获取尺寸信息
	sizeInfo, err := l.svcCtx.AllModels.FsProductSize.FindOne(l.ctx, req.SizeId)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the size is not exists")
		}
		logx.Error("获取尺寸信息错误:", err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size info")
	}
	//状态
	if *sizeInfo.Status != 1 {
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "size info status is not normal")
	}
	//获取模型信息
	modelInfo, err := l.svcCtx.AllModels.FsProductModel3d.GetOneBySizeIdTag(l.ctx, sizeInfo.Id, constants.TAG_MODEL)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "the template`s model is not exists")
		}
		logx.Error("获取模型信息错误:", err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get template`s model info")
	}
	if *modelInfo.Status != 1 {
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model` status is unNormal")
	}
	//如果模型是配件则返回
	if *modelInfo.Tag != 1 {
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model your post is not a model but fitting")
	}
	if modelInfo.ModelInfo == nil || *modelInfo.ModelInfo == "" {
		return resp.SetStatusWithMessage(basic.CodeServiceErr, "the model`s design info is empty")
	}
	var sizeKeyInfo shopping_cart.SizeInfo
	if err = json.Unmarshal([]byte(*sizeInfo.Title), &sizeKeyInfo); err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeJsonErr, "failed to parse size info`s title ")
	}
	sizeKeyInfo.Capacity = *sizeInfo.Capacity
	//快照数据
	snapshot := shopping_cart.CartSnapshot{
		Logo:         req.Logo,
		CombineImage: req.CombineImage,
		RenderImage:  req.RenderImage,
		TemplateInfo: shopping_cart.TemplateInfo{
			TemplateJson: templateJson,
			TemplateTag:  templateTag,
		},
		ModelInfo: shopping_cart.ModelInfo{
			ModelJson: *modelInfo.ModelInfo,
		},
		FittingInfo: shopping_cart.FittingInfo{
			FittingJson: fittingJson,
			FittingName: fittingName,
		},
		SizeInfo: sizeKeyInfo,
		ProductInfo: shopping_cart.ProductInfo{
			ProductName: *productInfo.Title,
			ProductSn:   *productInfo.Sn,
		},
		UserDiyInformation: shopping_cart.UserDiyInformation{
			Phone:   req.DiyInfo.Phone,
			Address: req.DiyInfo.Address,
			Website: req.DiyInfo.Website,
			Qrcode:  req.DiyInfo.Qrcode,
			Slogan:  req.DiyInfo.Slogan,
		},
	}
	snapshotJsonBytes, _ := json.Marshal(snapshot)
	snapshotJsonStr := string(snapshotJsonBytes)
	now := time.Now().UTC()
	err = l.svcCtx.AllModels.FsShoppingCart.Create(l.ctx, &gmodel.FsShoppingCart{
		UserId:           &userinfo.UserId,
		ProductId:        &req.ProductId,
		TemplateId:       &req.TemplateId,
		ModelId:          &modelInfo.Id,
		SizeId:           &req.SizeId,
		FittingId:        &req.FittingId,
		PurchaseQuantity: &req.PurchaseQuantity,
		Snapshot:         &snapshotJsonStr,
		Ctime:            &now,
		Utime:            &now,
	})
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "system err:failed to add to cart ")
	}
	return resp.SetStatus(basic.CodeOK, "success")
}

// 参数校验
func (l *AddToCartLogic) AddToCartParamVerify(req *types.AddToCartReq) error {
	if req.ProductId <= 0 {
		return errors.New("product_id is required")
	}
	if req.SizeId <= 0 {
		return errors.New("product size is required")
	}
	if req.PurchaseQuantity <= 0 {
		return errors.New("purchase quantity can not less than 0 or equal 0")
	}
	return nil
}

// 处理逻辑后 w,r 如:重定向, resp 必须重新处理
// func (l *AddToCartLogic) AfterLogic(w http.ResponseWriter, r *http.Request, resp *basic.Response) {
// // httpx.OkJsonCtx(r.Context(), w, resp)
// }