package logic

import (
	"encoding/json"
	"errors"
	"fusenapi/constants"
	"fusenapi/utils/auth"
	"fusenapi/utils/basic"
	"fusenapi/utils/format"
	"gorm.io/gorm"
	"strings"

	"context"

	"fusenapi/server/product/internal/svc"
	"fusenapi/server/product/internal/types"

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

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

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

func (l *GetSizeByPidLogic) GetSizeByPid(req *types.GetSizeByPidReq, userinfo *auth.UserInfo) (resp *basic.Response) {
	req.Pid = strings.Trim(req.Pid, " ")
	if req.Pid == "" {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:pid is empty")
	}
	if req.TemplateTag == "" {
		return resp.SetStatusWithMessage(basic.CodeRequestParamsErr, "err param:template_tag is empty")
	}
	//获取产品信息(只是获取id)
	productInfo, err := l.svcCtx.AllModels.FsProduct.FindOneBySn(l.ctx, req.Pid, "id")
	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")
	}
	//获取跟列表页云渲染一样的默认渲染尺寸(模板->尺寸)
	defaultSizeId := int64(0)
	defaultTemplate, err := l.svcCtx.AllModels.FsProductTemplateV2.FindOneCloudRenderByProductIdTemplateTag(l.ctx, productInfo.Id, req.TemplateTag, "sort ASC", "model_id")
	if err != nil && !errors.Is(err, gorm.ErrRecordNotFound) {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get default template ")
	} else {
		//根据模板找到模型sizeId
		defaultModel3d, err := l.svcCtx.AllModels.FsProductModel3d.FindOne(l.ctx, *defaultTemplate.ModelId, "size_id")
		if err != nil {
			if errors.Is(err, gorm.ErrRecordNotFound) {
				return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "the template`s model is not exists ")
			}
			logx.Error(err)
			return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get default model ")
		}
		defaultSizeId = *defaultModel3d.SizeId
	}
	//获取产品尺寸列表(需要正序排序)
	sizeList, err := l.svcCtx.AllModels.FsProductSize.GetAllByProductIds(l.ctx, []int64{productInfo.Id}, "is_hot DESC,sort ASC")
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get size list")
	}
	sizeIds := make([]int64, 0, len(sizeList))
	for _, v := range sizeList {
		sizeIds = append(sizeIds, v.Id)
	}
	//获取产品价格列表
	mapProductMinPrice := make(map[int64]int64)
	//获取产品模型
	modelList, err := l.svcCtx.AllModels.FsProductModel3d.GetAllByProductIdsTags(l.ctx, []int64{productInfo.Id}, []int{constants.TAG_MODEL, constants.TAG_PARTS}, "id,size_id,product_id,price,tag,part_id,step_price")
	if err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get model list")
	}
	if err = l.svcCtx.AllModels.FsProductModel3d.GetProductMinPrice(modelList, mapProductMinPrice); err != nil {
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product min price")
	}
	mapSizeModel := make(map[int64]int) //size id为key
	for k, v := range modelList {
		if *v.Tag != constants.TAG_MODEL {
			continue
		}
		mapSizeModel[*v.SizeId] = k
	}
	//处理
	listRsp := make([]types.GetSizeByPidRsp, 0, len(sizeList))
	for _, sizeInfo := range sizeList {
		//没有模型的不能使用
		modelIndex, ok := mapSizeModel[sizeInfo.Id]
		if !ok {
			continue
		}
		var title interface{}
		_ = json.Unmarshal([]byte(*sizeInfo.Title), &title)
		minPrice := int64(0)
		if price, ok := mapProductMinPrice[*sizeInfo.ProductId]; ok {
			minPrice = price
		}
		listRsp = append(listRsp, types.GetSizeByPidRsp{
			Id:              sizeInfo.Id,
			Title:           title,
			Capacity:        *sizeInfo.Capacity,
			Cover:           *sizeInfo.Cover,
			PartsCanDeleted: *sizeInfo.PartsCanDeleted > 0,
			ModelId:         modelList[modelIndex].Id,
			IsPopular:       *sizeInfo.IsHot > 0,
			MinPrice:        format.CentitoDollar(minPrice, 3),
			IsDefault:       defaultSizeId == sizeInfo.Id,
		})
	}
	return resp.SetStatusWithMessage(basic.CodeOK, "success", listRsp)
}