package schema

import (
	"github.guxuan/haibei/pkg/util"
	"time"
)

// Defining the `Product` struct.
type Product struct {
	util.BaseModel
	CategoryID   uint            `json:"categoryId" gorm:"index;comment:分类id"`
	Typer        string          `json:"type" gorm:"index;comment:产品类别 product-产品，activity-活动"`
	Name         string          `json:"name" gorm:"size:255;not null;index;comment:名称"`
	Price        int             `json:"price" gorm:"not null;comment:兑换积分金额"`
	Stock        int             `json:"stock" gorm:"not null;default:0;comment:库存"`
	MaxNum       int             `json:"maxNum" gorm:"not null;default:0;comment:最大兑换数量"`
	Cover        string          `json:"cover" gorm:"size:2048;not null;comment:封面图"`
	BusinessImgs []string        `json:"businessImgs" gorm:"serializer:json;comment:商详图数组"`
	Details      string          `json:"details" gorm:"type:text;comment:详情"`
	Instructions string          `json:"instructions" gorm:"type:text;comment:使用说明"`
	Policy       string          `json:"policy" gorm:"type:text;comment:退货说明"`
	IsHot        bool            `json:"isHot" gorm:"comment:是否热门"`
	AreaID       uint            `json:"areaId" gorm:"index;comment:城市id"`
	Status       string          `json:"status" gorm:"size:20;index;comment:分类状态"`
	ExpireAt     *time.Time      `json:"expireAt" gorm:"comment:兑换有效期"`
	Category     ProductCategory `gorm:"foreignKey:CategoryID;references:ID"` // 关联分类

}

// Defining the query parameters for the `Product` struct.
type ProductQueryParam struct {
	util.PaginationParam
	CategoryID uint   `form:"categoryId"`
	LikeName   string `form:"name"`
	IsHot      bool   `form:"isHot"`
	Typer      string `form:"type"`
	Status     string `form:"status"`
	AreaID     uint   `form:"areaId"`
}

// Defining the query options for the `Product` struct.
type ProductQueryOptions struct {
	util.QueryOptions
}

// Defining the query result for the `Product` struct.
type ProductQueryResult struct {
	Data       Products
	PageResult *util.PaginationResult
}

// Defining the slice of `Product` struct.
type Products []*Product

// Defining the data structure for creating a `Product` struct.
type ProductForm struct {
	CategoryID   uint       `json:"categoryId" `
	Name         string     `json:"name" `
	Typer        string     `json:"type"`
	Price        int        `json:"price" `
	Stock        int        `json:"stock" `
	Cover        string     `json:"cover"`
	BusinessImgs []string   `json:"businessImgs" `
	IsHot        bool       `json:"isHot" `
	MaxNum       int        `json:"maxNum" `
	Details      string     `json:"details" `
	AreaID       uint       `json:"areaId"`
	ExpireAt     *time.Time `json:"expireAt" `

	Instructions string `json:"instructions" `
	Policy       string `json:"policy" `
	Status       string `json:"status" `
}

// A validation function for the `ProductForm` struct.
func (a *ProductForm) Validate() error {
	return nil
}

// Convert `ProductForm` to `Product` object.
func (a *ProductForm) FillTo(product *Product) error {
	product.CategoryID = a.CategoryID
	product.Name = a.Name
	product.Price = a.Price
	product.MaxNum = a.MaxNum
	product.Stock = a.Stock
	product.Cover = a.Cover
	product.Typer = a.Typer
	product.ExpireAt = a.ExpireAt
	product.BusinessImgs = a.BusinessImgs
	product.IsHot = a.IsHot
	product.Details = a.Details
	product.Instructions = a.Instructions
	product.Policy = a.Policy
	product.AreaID = a.AreaID
	product.Status = a.Status
	return nil
}
