package entities

import (
	"time"

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

type Customer 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"`
	Name           string    `gorm:"not null;size:255" json:"name" validate:"required"`
	Email          *string   `gorm:"size:255;uniqueIndex" json:"email,omitempty"`
	Phone          *string   `gorm:"size:20" json:"phone,omitempty"`
	Address        *string   `gorm:"size:500" json:"address,omitempty"`
	IsDefault      bool      `gorm:"default:false" json:"is_default"`
	IsActive       bool      `gorm:"default:true" json:"is_active"`
	Metadata       Metadata  `gorm:"type:jsonb;default:'{}'" json:"metadata"`
	CreatedAt      time.Time `gorm:"autoCreateTime" json:"created_at"`
	UpdatedAt      time.Time `gorm:"autoUpdateTime" json:"updated_at"`

	Organization Organization `gorm:"foreignKey:OrganizationID" json:"organization,omitempty"`
	Orders       []Order      `gorm:"foreignKey:CustomerID" json:"orders,omitempty"`
}

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

func (Customer) TableName() string {
	return "customers"
}
