package schema

import (
	"entgo.io/ent/schema/edge"
	"wechat-api/ent/schema/localmixin"

	"entgo.io/ent"
	"entgo.io/ent/dialect/entsql"
	"entgo.io/ent/schema"
	"entgo.io/ent/schema/field"
	"entgo.io/ent/schema/index"
	"github.com/suyuan32/simple-admin-common/orm/ent/mixins"
)

type ApiKey struct {
	ent.Schema
}

func (ApiKey) Fields() []ent.Field {
	return []ent.Field{
		field.String("title").Optional().Default("").Comment("Title"),
		field.String("key").Optional().Default("").Comment("Key"),
		field.Uint64("organization_id").Default(0).Comment("租户ID"),
		field.Uint64("agent_id").Default(0).Comment("智能体ID"),
		field.String("custom_agent_base").Optional().Default("").Comment("定制agent服务地址"),
		field.String("custom_agent_key").Optional().Default("").Comment("定制agent服务密钥"),
		field.String("openai_base").Optional().Default("").Comment("大模型服务地址"),
		field.String("openai_key").Optional().Default("").Comment("大模型服务密钥"),
	}
}

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

func (ApiKey) Indexes() []ent.Index {
	return []ent.Index{
		index.Fields("key").Unique(),
	}
}

func (ApiKey) Edges() []ent.Edge {
	return []ent.Edge{
		edge.From("agent", Agent.Type).
			Ref("key_agent").
			Unique().
			Field("agent_id").
			Required(),
	}
}

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