package schema

import (
	"wechat-api/ent/custom_types"
	"wechat-api/ent/schema/localmixin"

	"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"
	"github.com/suyuan32/simple-admin-common/orm/ent/mixins"
)

type SopStage struct {
	ent.Schema
}

func (SopStage) Fields() []ent.Field {
	return []ent.Field{
		field.Uint64("task_id").
			Annotations(entsql.WithComments(true)).
			Comment("SOP 任务 ID"),
		field.String("name").Default("").
			Annotations(entsql.WithComments(true)).
			Comment("阶段名称"),
		field.Int("condition_type").Default(1).
			Annotations(entsql.WithComments(true)).
			Comment("客群筛选条件类型  1 按标签筛选 2 按客户基本信息筛选"),
		field.Int("condition_operator").Default(1).
			Annotations(entsql.WithComments(true)).
			Comment("筛选条件关系  1 满足所有条件（and） 2 满足任意条件（or）"),
		field.JSON("condition_list", []custom_types.Condition{}).
			Annotations(entsql.WithComments(true)).
			Comment("筛选条件列表"),
		field.JSON("action_message", []custom_types.Action{}).Optional().
			Annotations(entsql.WithComments(true)).
			Comment("命中后发送的消息内容"),
		field.JSON("action_label_add", []uint64{}).Optional().
			Annotations(entsql.WithComments(true)).
			Comment("命中后需要打的标签"),
		field.JSON("action_label_del", []uint64{}).Optional().
			Annotations(entsql.WithComments(true)).
			Comment("命中后需要移除的标签"),
		field.JSON("action_forward", &custom_types.ActionForward{}).Optional().
			Annotations(entsql.WithComments(true)).
			Comment("命中后转发的消息"),
		field.Int("index_sort").Default(1).Optional().
			Annotations(entsql.WithComments(true)).
			Comment("阶段顺序"),
	}
}

func (SopStage) Mixin() []ent.Mixin {
	return []ent.Mixin{
		mixins.IDMixin{},
		mixins.StatusMixin{},
		localmixin.SoftDeleteMixin{},
	}
}

func (SopStage) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("name"),
	}
}

func (SopStage) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("sop_task", SopTask.Type).
			Ref("task_stages").
			Unique().
			Field("task_id").
			Required(),
		edge.To("stage_nodes", SopNode.Type),
		edge.To("stage_messages", MessageRecords.Type),
	}
}

func (SopStage) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.WithComments(true),
		entsql.Annotation{Table: "sop_stage"},
	}
}
