package entities

import (
	"time"

	"github.com/google/uuid"
	"gorm.io/gorm"
)

type ProductRecipe struct {
	ID             uuid.UUID  `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	OrganizationID uuid.UUID  `gorm:"type:uuid;not null;index" json:"organization_id"`
	OutletID       *uuid.UUID `gorm:"type:uuid;index" json:"outlet_id"`
	ProductID      uuid.UUID  `gorm:"type:uuid;not null;index" json:"product_id"`
	VariantID      *uuid.UUID `gorm:"type:uuid;index" json:"variant_id"`
	IngredientID   uuid.UUID  `gorm:"type:uuid;not null;index" json:"ingredient_id"`
	Quantity       float64    `gorm:"type:decimal(12,3);not null" json:"quantity"`
	CreatedAt      time.Time  `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt      time.Time  `gorm:"autoUpdateTime" json:"updated_at"`

	// Relations
	Product        *Product        `gorm:"foreignKey:ProductID" json:"product,omitempty"`
	ProductVariant *ProductVariant `gorm:"foreignKey:VariantID" json:"product_variant,omitempty"`
	Ingredient     *Ingredient     `gorm:"foreignKey:IngredientID" json:"ingredient,omitempty"`
}

func (pr *ProductRecipe) BeforeCreate(tx *gorm.DB) error {
	if pr.ID == uuid.Nil {
		pr.ID = uuid.New()
	}
	return nil
}

func (ProductRecipe) TableName() string {
	return "product_recipes"
}