package logic

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

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

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

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

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

func (l *GetLastProductDesignLogic) GetLastProductDesign(req *types.Request, userinfo *auth.UserInfo) (resp *basic.Response) {
	if !userinfo.IsUser() {
		return resp.SetStatusAddMessage(basic.CodeUnAuth, "please login")
	}
	//获取用户信息
	user, err := l.svcCtx.AllModels.FsUser.FindUserById(l.ctx, userinfo.UserId)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeUnAuth, "user not found")
		}
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get user info")
	}
	//若没打开了个性化渲染按钮
	if *user.IsOpenRender != 1 {
		return resp.SetStatusWithMessage(basic.CodeOK, "success:IsOpenRender switch is closed")
	}
	//查询用户最近下单成功的数据
	orderInfo, err := l.svcCtx.AllModels.FsOrder.FindLastSuccessOneOrder(l.ctx, user.Id, int64(constants.STATUS_NEW_NOT_PAY))
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "none of order is found")
		}
		logx.Error(err)
		return resp.SetStatusAddMessage(basic.CodeDbSqlErr, "failed to get your last order")
	}
	//获取该订单相关设计信息
	orderDetail, err := l.svcCtx.AllModels.FsOrderDetail.GetOneOrderDetailByOrderId(l.ctx, orderInfo.Id)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order detail is not found")
		}
		logx.Error(err)
		return resp.SetStatusAddMessage(basic.CodeDbSqlErr, "failed to get order detail")
	}
	//获取设计模板详情,便于获得design_id
	orderDetailTemplate, err := l.svcCtx.AllModels.FsOrderDetailTemplate.FindOne(l.ctx, *orderDetail.OrderDetailTemplateId)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "order detail template is not found")
		}
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get order detail template")
	}
	//最后一次设计不存在,则不返回该设计相关数据
	if *orderDetailTemplate.DesignId <= 0 {
		return resp.SetStatusWithMessage(basic.CodeOK, "success:last design id is not set")
	}
	//获取设计数据
	productDesign, err := l.svcCtx.AllModels.FsProductDesign.FindOne(l.ctx, *orderDetailTemplate.DesignId, user.Id)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			return resp.SetStatusWithMessage(basic.CodeDbRecordNotFoundErr, "product design is not found")
		}
		logx.Error(err)
		return resp.SetStatusWithMessage(basic.CodeDbSqlErr, "failed to get product design")
	}
	var info interface{}
	if productDesign.Info != nil && *productDesign.Info != "" {
		if err := json.Unmarshal([]byte(*productDesign.Info), &info); err != nil {
			logx.Error(err)
			return nil
		}
	}
	var logoColor interface{}
	if productDesign.LogoColor != nil && *productDesign.LogoColor != "" {
		if err := json.Unmarshal([]byte(*productDesign.LogoColor), &logoColor); err != nil {
			logx.Error(err)
			return nil
		}
	}
	return resp.SetStatusWithMessage(basic.CodeOK, "success", types.GetLastProductDesignRsp{
		Id:         productDesign.Id,
		OptionalId: *productDesign.OptionalId,
		SizeId:     *productDesign.SizeId,
		LogoColor:  logoColor,
		Info:       info,
	})
}