package entities

import (
	"time"

	"github.com/google/uuid"

	"gorm.io/gorm"
)

type Vendor struct {
	ID             uuid.UUID `gorm:"type:uuid;primary_key;default:gen_random_uuid()" json:"id"`
	OrganizationID uuid.UUID `gorm:"type:uuid;not null" json:"organization_id" validate:"required"`
	Name           string    `gorm:"not null;size:255" json:"name" validate:"required,min=1,max=255"`
	Email          *string   `gorm:"size:255" json:"email" validate:"omitempty,email"`
	PhoneNumber    *string   `gorm:"size:20" json:"phone_number" validate:"omitempty"`
	Address        *string   `gorm:"type:text" json:"address" validate:"omitempty"`
	ContactPerson  *string   `gorm:"size:255" json:"contact_person" validate:"omitempty,max=255"`
	TaxNumber      *string   `gorm:"size:50" json:"tax_number" validate:"omitempty,max=50"`
	PaymentTerms   *string   `gorm:"size:100" json:"payment_terms" validate:"omitempty,max=100"`
	Notes          *string   `gorm:"type:text" json:"notes" validate:"omitempty"`
	IsActive       bool      `gorm:"not null;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"`
}

func (v *Vendor) BeforeCreate(tx *gorm.DB) error {
	if v.ID == uuid.Nil {
		id := uuid.New()
		v.ID = id
	}
	return nil
}

func (Vendor) TableName() string {
	return "vendors"
}
