package filemanager

import (
	"time"

	"gorm.io/gorm"

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

type Folder struct {
	ID             []byte     `gorm:"type:binary(16);primaryKey;column:id"`
	ParentID       []byte     `gorm:"type:binary(16);column:parent_id;index:idx_file_folders_parent_active,priority:1;index:uq_file_folders_parent_name_slot,priority:1"`
	Name           string     `gorm:"type:varchar(128);not null;column:name"`
	NameNormalized string     `gorm:"type:varchar(128);not null;column:name_normalized;index:uq_file_folders_parent_name_slot,priority:2"`
	Depth          int        `gorm:"type:smallint unsigned;not null;default:0;column:depth"`
	PathCache      string     `gorm:"type:varchar(2048);column:path_cache"`
	NameSlot       string     `gorm:"type:char(36);not null;default:'live';column:name_slot;index:uq_file_folders_parent_name_slot,priority:3"`
	CreatedAt      time.Time  `gorm:"column:created_at"`
	CreatedBy      []byte     `gorm:"type:binary(16);column:created_by"`
	UpdatedAt      time.Time  `gorm:"column:updated_at"`
	UpdatedBy      []byte     `gorm:"type:binary(16);column:updated_by"`
	DeletedAt      *time.Time `gorm:"column:deleted_at;index:idx_file_folders_parent_active,priority:2;index:idx_file_folders_deleted_at"`
	DeletedBy      []byte     `gorm:"type:binary(16);column:deleted_by"`
	PurgeAt        *time.Time `gorm:"column:purge_at;index:idx_file_folders_purge_at"`

	Parent *Folder `gorm:"foreignKey:ParentID;references:ID;constraint:OnUpdate:CASCADE,OnDelete:RESTRICT"`
}

func (Folder) TableName() string { return "file_folders" }

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