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

type MessageRecords struct {
	ent.Schema
}

func (MessageRecords) Fields() []ent.Field {
	return []ent.Field{
		field.String("bot_wxid").
			Annotations(entsql.WithComments(true)).
			Comment("机器人微信 id"),
		field.Uint64("contact_id").Optional().
			Annotations(entsql.WithComments(true)).
			Comment("联系人 id"),
		field.Int("contact_type").Default(1).
			Annotations(entsql.WithComments(true)).
			Comment("类型：1好友，2群组，3企业微信联系人"),
		field.String("contact_wxid").Default("").
			Annotations(entsql.WithComments(true)).
			Comment("接收方微信 id"),
		field.Int("content_type").Default(1).
			Annotations(entsql.WithComments(true)).
			Comment("内容类型 1 文本 2 文件"),
		field.String("content").Default("").
			Annotations(entsql.WithComments(true)).
			Comment("发送内容"),
		field.JSON("meta", custom_types.Meta{}).Optional().
			Annotations(entsql.WithComments(true)).
			Comment("元数据"),
		field.String("error_detail").Default("").
			Annotations(entsql.WithComments(true)).
			Comment("异常原因"),
		field.Time("send_time").Optional().
			Annotations(entsql.WithComments(true)).
			Comment("发送时间"),
		field.Int("source_type").Default(1).
			Annotations(entsql.WithComments(true)).
			Comment("源类型 1 点发 2 群发 3 SOP"),
		field.Uint64("source_id").Default(1).Optional().
			Annotations(entsql.WithComments(true)).
			Comment("源 ID"),
		field.Uint64("sub_source_id").Default(1).Optional().
			Annotations(entsql.WithComments(true)).
			Comment("次源 ID"),
		field.Uint64("organization_id").Optional().Default(1).
			Comment("机构 ID").
			Annotations(entsql.WithComments(true)),
	}
}

func (MessageRecords) Mixin() []ent.Mixin {
	return []ent.Mixin{
		mixins.IDMixin{},
		mixins.StatusMixin{},
	}
}

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

func (MessageRecords) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("sop_stage", SopStage.Type).
			Ref("stage_messages").
			Unique().
			Field("source_id"),
		edge.From("sop_node", SopNode.Type).
			Ref("node_messages").
			Unique().
			Field("sub_source_id"),
		edge.From("message_contact", Contact.Type).
			Ref("contact_messages").
			Unique().
			Field("contact_id"),
	}
}

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