package entities

import (
	"time"

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

type Product 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" validate:"required"`
	CategoryID     uuid.UUID  `gorm:"type:uuid;not null;index" json:"category_id" validate:"required"`
	SKU            *string    `gorm:"size:100;index" json:"sku"`
	Name           string     `gorm:"not null;size:255" json:"name" validate:"required,min=1,max=255"`
	Description    *string    `gorm:"type:text" json:"description"`
	Price          float64    `gorm:"type:decimal(10,2);not null" json:"price" validate:"required,min=0"`
	Cost           float64    `gorm:"type:decimal(10,2);default:0.00" json:"cost" validate:"min=0"`
	BusinessType   string     `gorm:"size:50;default:'restaurant'" json:"business_type"`
	ImageURL       *string    `gorm:"size:500" json:"image_url"`
	PrinterType    string     `gorm:"size:50;default:'kitchen'" json:"printer_type"`
	UnitID         *uuid.UUID `gorm:"type:uuid;index" json:"unit_id"`
	HasIngredients bool       `gorm:"default:false" json:"has_ingredients"`
	Metadata       Metadata   `gorm:"type:jsonb;default:'{}'" json:"metadata"`
	IsActive       bool       `gorm:"default:true" json:"is_active"`
	CreatedAt      time.Time  `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt      time.Time  `gorm:"autoUpdateTime" json:"updated_at"`

	Organization       Organization        `gorm:"foreignKey:OrganizationID" json:"organization,omitempty"`
	Category           Category            `gorm:"foreignKey:CategoryID" json:"category,omitempty"`
	Unit               *Unit               `gorm:"foreignKey:UnitID" json:"unit,omitempty"`
	ProductVariants    []ProductVariant    `gorm:"foreignKey:ProductID" json:"variants,omitempty"`
	ProductRecipes []ProductRecipe `gorm:"foreignKey:ProductID" json:"product_recipes,omitempty"`
	Inventory          []Inventory         `gorm:"foreignKey:ProductID" json:"inventory,omitempty"`
	OrderItems         []OrderItem         `gorm:"foreignKey:ProductID" json:"order_items,omitempty"`
}

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

func (Product) TableName() string {
	return "products"
}

type ProductVariant struct {
	ID            uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	ProductID     uuid.UUID `gorm:"type:uuid;not null;index" json:"product_id" validate:"required"`
	Name          string    `gorm:"not null;size:255" json:"name" validate:"required,min=1,max=255"`
	PriceModifier float64   `gorm:"type:decimal(10,2);default:0.00" json:"price_modifier"`
	Cost          float64   `gorm:"type:decimal(10,2);default:0.00" json:"cost" validate:"min=0"`
	Metadata      Metadata  `gorm:"type:jsonb;default:'{}'" json:"metadata"`
	CreatedAt     time.Time `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt     time.Time `gorm:"autoUpdateTime" json:"updated_at"`

	Product    Product     `gorm:"foreignKey:ProductID" json:"product,omitempty"`
	OrderItems []OrderItem `gorm:"foreignKey:ProductVariantID" json:"order_items,omitempty"`
}

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

func (ProductVariant) TableName() string {
	return "product_variants"
}
