package facility

import (
	"time"

	"gorm.io/gorm"

	"wucher/internal/shared/pkg/uuidv7"
)

// Facility stores configurable HEMS / Dry Lease (and other) operational equipment rows.
type Facility struct {
	ID        []byte     `gorm:"type:binary(16);primaryKey;column:id"`
	Category  string     `gorm:"type:varchar(64);index;not null;column:category"`
	Name      string     `gorm:"type:varchar(150);not null;column:name"`
	Type      string     `gorm:"type:varchar(100);not null;column:type"`
	SortKey   *int       `gorm:"column:sortkey"`
	Length    string     `gorm:"type:varchar(64);column:length"`
	Weight    string     `gorm:"type:varchar(64);column:weight"`
	Electric  string     `gorm:"type:varchar(255);not null;default:'';column:electric"`
	Note      string     `gorm:"type:text;column:note"`
	IsActive  bool       `gorm:"type:tinyint(1);index;not null;default:1;column:is_active"`
	CreatedBy []byte     `gorm:"type:binary(16);column:created_by"`
	UpdatedBy []byte     `gorm:"type:binary(16);column:updated_by"`
	DeletedBy []byte     `gorm:"type:binary(16);column:deleted_by"`
	CreatedAt time.Time
	UpdatedAt time.Time
	DeletedAt *time.Time `gorm:"column:deleted_at;index"`
}

func (Facility) TableName() string { return "facilities" }

func (f *Facility) BeforeCreate(tx *gorm.DB) error {
	if len(f.ID) == 0 {
		f.ID = uuidv7.MustBytes()
	}
	return nil
}
