package beans

import (
	"fafa-crawler/src/models"
	"fafa-crawler/src/util"
	"fmt"
	"strconv"
	"strings"
)

type GoodsID struct {
	ID string `json:"id"`
}

type ProductBean struct {
	Source    string   `json:"id"`
	Name      string   `json:"title"`      // 商品名
	Barcode   string   `json:"goods_code"` // 条形码/Barcode
	Category  string   `json:"category"`   // 商品分类
	Price     string   `json:"price"`      // 商品原价
	Keywords  string   `json:"sub_title"`  // 商品关键字
	Thumb     string   `json:"thumb"`      // 商品缩略图
	ImageList []string `json:"thumbs"`     // 商品图片列表
}

func (p *ProductBean) ConvProductModel() (*models.LibraryProduct, []*models.LibraryProductImage, error) {
	priceFloat, err := strconv.ParseFloat(p.Price, 64)
	if err != nil {
		priceFloat = 0 // or handle error as needed
	}
	imgDomain := "http://shop.yunmadian.com/data/attachment/"

	product := &models.LibraryProduct{
		Source:   fmt.Sprintf("heidouyun_%s", p.Source),
		Name:     p.Name,
		Barcode:  p.Barcode,
		Category: p.Category,
		Price:    priceFloat,
		Keywords: util.SplitTags2(p.Keywords),
	}
	if p.Thumb != "" {
		product.Thumb = imgDomain + p.Thumb
	}

	var productImages []*models.LibraryProductImage
	if product.Thumb != "" {
		productImages = append(productImages, &models.LibraryProductImage{
			ProductID: product.ID,
			ImageURL:  product.Thumb,
			IsMain:    1,
		})
	}

	for _, img := range p.ImageList {
		if img != "" {
			productImages = append(productImages, &models.LibraryProductImage{
				ProductID: product.ID,
				ImageURL:  imgDomain + img,
				IsMain:    2,
			})
		}

	}

	return product, productImages, nil
}

func (p *ProductBean) IsValidBean() bool {
	return p != nil &&
		!util.IsEmpty(p.Name) &&
		!strings.Contains(p.Name, "test") &&
		!strings.Contains(p.Name, "测试") &&
		len(p.ImageList) > 0
}

type ProductMeiTuanBean struct {
	Product   *models.LibraryProduct
	ImageList []*models.LibraryProductImage
}

type SiXunGoodsBean struct {
	Product   *models.LibraryProduct
	ImageList []*models.LibraryProductImage
}
