package schema

import (
	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/edge"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
	"git.noahlan.cn/noahlan/ntool-biz/core/orm/nent/mixins"
	"git.noahlan.cn/noahlan/ntool/nrandom"
)

type DictionaryDetail struct {
	ent.Schema
}

func (DictionaryDetail) Fields() []ent.Field {
	return []ent.Field{
		field.String("title").
			Comment("The title shown in the ui | 展示名称 （建议配合i18n）"),
		field.String("key").
			Comment("key | 键"),
		field.String("value").
			Comment("value | 值"),
		field.Int64("dictionary_id").
			Comment("Dictionary ID | 字典ID"),
	}
}

func (DictionaryDetail) Mixin() []ent.Mixin {
	return []ent.Mixin{
		mixins.NewDistributedIDMixin(func() int64 { return nrandom.SnowflakeId() }),
		mixins.TimeMixin{},
		mixins.StatusMixin{},
		mixins.SortMixin{},
		mixins.OptimisticLockMixin{},
	}
}

func (DictionaryDetail) Edges() []ent.Edge {
	return []ent.Edge{
		// many to one ref
		edge.From("dictionary", Dictionary.Type).
			Ref("details").
			Field("dictionary_id").
			Unique().Required(),
	}
}

func (DictionaryDetail) Indexes() []ent.Index {
	return []ent.Index{
		// 唯一索引
		index.Fields("dictionary_id", "key").Unique(),
		// 查询优化索引
		index.Fields("key"),
		index.Fields("title"),
	}
}

func (DictionaryDetail) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.WithComments(true),
		entsql.Annotation{Table: "sys_dictionary_details"},
		schema.Comment("字典详情表"),
	}
}
