package schema

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

type UserSocial struct {
	ent.Schema
}

func (UserSocial) Fields() []ent.Field {
	return []ent.Field{
		field.Int64("user_id").Comment("系统用户ID"),
		field.String("provider").Comment("OAuth Provider | 服务提供商"),
		field.String("access_token").Comment("Access Token | 用户在提供商最近一次登录的访问凭证"),
		field.String("refresh_token").Comment("Refresh Token | 用户在提供商最近一次登录的刷新凭证"),
		field.String("uid").Comment("User's ID | 提供商用户ID，不一定存在"),
		field.String("open_id").Comment("OpenID | 用户在提供商的OpenID"),
		field.String("union_id").Comment("UnionID | 用户在提供商的UnionID"),
		field.String("key").Comment("Key | 用户在提供商的用于解密的key"),
		field.Uint64("expires_in").Comment("Expires in | 访问凭证过期时长，单位：秒"),
		field.Text("user_profile").
			SchemaType(map[string]string{dialect.MySQL: "json"}).
			Comment("User Profile | 提供商用户信息，不一定存在"),
	}
}

func (UserSocial) Mixin() []ent.Mixin {
	return []ent.Mixin{
		mixins.NewDistributedIDMixin(func() int64 { return nrandom.SnowflakeId() }),
		mixins.TimeMixin{},
	}
}

func (UserSocial) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("user", User.Type).
			Ref("socials").
			Field("user_id").
			Unique().
			Required(),
	}
}

func (UserSocial) Indexes() []ent.Index {
	return []ent.Index{}
}

func (UserSocial) Annotations() []schema.Annotation {
	return []schema.Annotation{
		entsql.WithComments(true),
		entsql.Annotation{Table: "sys_user_social"},
		schema.Comment("用户社交信息表"),
	}
}
