package entities

import (
	"time"

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

type Outlet 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,min=1,max=255"`
	Address        *string   `gorm:"type:text" json:"address"`
	Timezone       *string   `gorm:"size:50" json:"timezone"`
	Currency       string    `gorm:"size:3;default:'USD'" json:"currency" validate:"len=3"`
	TaxRate        float64   `gorm:"type:decimal(5,4);default:0.0000" json:"tax_rate" validate:"min=0,max=1"`
	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"`
	Users        []User          `gorm:"foreignKey:OutletID" json:"users,omitempty"`
	Orders       []Order         `gorm:"foreignKey:OutletID" json:"orders,omitempty"`
	Inventory    []Inventory     `gorm:"foreignKey:OutletID" json:"inventory,omitempty"`
	Settings     []OutletSetting `gorm:"foreignKey:OutletID" json:"settings,omitempty"`
}

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

func (Outlet) TableName() string {
	return "outlets"
}
