package merchant

import (
	"errors"
	logger "eta/eta_mini_ht_api/common/component/log"
	"eta/eta_mini_ht_api/common/utils/page"
	merchantDao "eta/eta_mini_ht_api/models/merchant"
	"github.com/shopspring/decimal"
	"gorm.io/gorm"
	"time"
)

var (
	typeTransfer = map[merchantDao.MerchantProductType]string{
		merchantDao.Report:  "单篇报告",
		merchantDao.Video:   "单个视频",
		merchantDao.Audio:   "单个音频",
		merchantDao.Package: "套餐",
	}
	typeKindTransfer = map[string]merchantDao.MerchantProductType{
		"report":  merchantDao.Report,
		"video":   merchantDao.Video,
		"audio":   merchantDao.Audio,
		"package": merchantDao.Package,
	}
)

type MerchantProductDTO struct {
	Id          int
	Title       string
	CoverSrc    int
	CoverUrl    string
	Description string
	Price       decimal.Decimal
	ProductTile string
	RiskLevel   string
	Type        string
	TypeCN      string
	IsPermanent bool
	ValidDays   int
	SaleStatus  string
	SourceId    int
	CreatedTime time.Time
}

func GetMerchantProductById(id int) (productDTO MerchantProductDTO, err error) {
	product, err := merchantDao.GetMerchantProductById(id)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			logger.Error("商品信息不存在[productId:%d]", id)
		} else {
			logger.Error("获取商品信息失败[productId:%d],err:", id, err)
		}
	}
	productDTO = convertToDTO(product)
	return
}
func convertToDTO(product merchantDao.MerchantProduct) MerchantProductDTO {
	return MerchantProductDTO{
		Id:          product.ID,
		Title:       product.Title,
		CoverSrc:    product.CoverSrc,
		CoverUrl:    product.CoverUrl,
		Description: product.Description,
		ProductTile: product.Title,
		Price:       product.Price,
		RiskLevel:   "",
		Type:        string(product.Type),
		TypeCN:      typeTransfer[product.Type],
		IsPermanent: product.IsPermanent,
		ValidDays:   product.ValidDays,
		SourceId:    product.SourceID,
		CreatedTime: product.CreatedTime,
	}
}

func GetProductBySourceId(id int, productType merchantDao.MerchantProductType) (product MerchantProductDTO, err error) {
	productDao, err := merchantDao.GetMerchantProductBySourceId(id, productType)
	if err != nil {
		if errors.Is(err, gorm.ErrRecordNotFound) {
			logger.Error("商品信息不存在[sourceId:%d,type:%v]", id, productType)
		} else {
			logger.Error("获取商品信息失败[sourceId:%d,type:%v],err:%v", id, productType, err)
		}
		return
	}
	product = convertToDTO(productDao)
	return
}

func GetProductListBySourceIds(ids []int, productType string) (productDTOS []MerchantProductDTO, err error) {
	productList, err := merchantDao.GetProductListBySourceIds(ids, true, typeKindTransfer[productType])
	if err != nil {
		logger.Error("获取商品列表失败[sourceIds:%v,type:%v],err:%v", ids, productType, err)
		return
	}
	for _, product := range productList {
		productDTO := convertToDTO(product)
		productDTOS = append(productDTOS, productDTO)
	}
	return
}

func GetTotalPageCountByProductType(productType string) (total int64, latestId int64) {
	return merchantDao.GetTotalPageCountByProductType(typeKindTransfer[productType])
}

func GetProductPageByProductType(productType string, info page.PageInfo) (dtoList []MerchantProductDTO, err error) {
	offset := page.StartIndex(info.Current, info.PageSize)
	productList, err := merchantDao.GetProductPageByProductType(typeKindTransfer[productType], info.LatestId, offset, info.PageSize)
	if err != nil {
		logger.Error("获取商品列表失败[productType:%v,pageInfo:%v],err:%v", productType, info, err)
		return
	}
	for _, product := range productList {
		productDTO := convertToDTO(product)
		dtoList = append(dtoList, productDTO)
	}
	return
}

func GetProductListByProductType(productType string) (dtoList []MerchantProductDTO, err error) {
	productList, err := merchantDao.GetProductListByProductType(typeKindTransfer[productType], false)
	if err != nil {
		logger.Error("获取商品列表失败[productType:%v,pageInfo:%v],err:%v", productType, err)
		return
	}
	for _, product := range productList {
		productDTO := convertToDTO(product)
		dtoList = append(dtoList, productDTO)
	}
	return
}