// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"
	"wechat-api/ent/agent"
	"wechat-api/ent/token"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
)

// Token is the model entity for the Token schema.
type Token struct {
	config `json:"-"`
	// ID of the ent.
	ID uint64 `json:"id,omitempty"`
	// Create Time | 创建日期
	CreatedAt time.Time `json:"created_at,omitempty"`
	// Update Time | 修改日期
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// Delete Time | 删除日期
	DeletedAt time.Time `json:"deleted_at,omitempty"`
	// 过期时间
	ExpireAt time.Time `json:"expire_at,omitempty"`
	// Token
	Token string `json:"token,omitempty"`
	// Mac地址
	MAC string `json:"mac,omitempty"`
	// 租户ID
	OrganizationID uint64 `json:"organization_id,omitempty"`
	// 智能体ID
	AgentID uint64 `json:"agent_id,omitempty"`
	// 定制agent服务地址
	CustomAgentBase string `json:"custom_agent_base,omitempty"`
	// 定制agent服务密钥
	CustomAgentKey string `json:"custom_agent_key,omitempty"`
	// 大模型服务地址
	OpenaiBase string `json:"openai_base,omitempty"`
	// 大模型服务密钥
	OpenaiKey string `json:"openai_key,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TokenQuery when eager-loading is set.
	Edges        TokenEdges `json:"edges"`
	selectValues sql.SelectValues
}

// TokenEdges holds the relations/edges for other nodes in the graph.
type TokenEdges struct {
	// Agent holds the value of the agent edge.
	Agent *Agent `json:"agent,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
}

// AgentOrErr returns the Agent value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e TokenEdges) AgentOrErr() (*Agent, error) {
	if e.Agent != nil {
		return e.Agent, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: agent.Label}
	}
	return nil, &NotLoadedError{edge: "agent"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Token) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case token.FieldID, token.FieldOrganizationID, token.FieldAgentID:
			values[i] = new(sql.NullInt64)
		case token.FieldToken, token.FieldMAC, token.FieldCustomAgentBase, token.FieldCustomAgentKey, token.FieldOpenaiBase, token.FieldOpenaiKey:
			values[i] = new(sql.NullString)
		case token.FieldCreatedAt, token.FieldUpdatedAt, token.FieldDeletedAt, token.FieldExpireAt:
			values[i] = new(sql.NullTime)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Token fields.
func (t *Token) assignValues(columns []string, values []any) error {
	if m, n := len(values), len(columns); m < n {
		return fmt.Errorf("mismatch number of scan values: %d != %d", m, n)
	}
	for i := range columns {
		switch columns[i] {
		case token.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			t.ID = uint64(value.Int64)
		case token.FieldCreatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created_at", values[i])
			} else if value.Valid {
				t.CreatedAt = value.Time
			}
		case token.FieldUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updated_at", values[i])
			} else if value.Valid {
				t.UpdatedAt = value.Time
			}
		case token.FieldDeletedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field deleted_at", values[i])
			} else if value.Valid {
				t.DeletedAt = value.Time
			}
		case token.FieldExpireAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field expire_at", values[i])
			} else if value.Valid {
				t.ExpireAt = value.Time
			}
		case token.FieldToken:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field token", values[i])
			} else if value.Valid {
				t.Token = value.String
			}
		case token.FieldMAC:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field mac", values[i])
			} else if value.Valid {
				t.MAC = value.String
			}
		case token.FieldOrganizationID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field organization_id", values[i])
			} else if value.Valid {
				t.OrganizationID = uint64(value.Int64)
			}
		case token.FieldAgentID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field agent_id", values[i])
			} else if value.Valid {
				t.AgentID = uint64(value.Int64)
			}
		case token.FieldCustomAgentBase:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field custom_agent_base", values[i])
			} else if value.Valid {
				t.CustomAgentBase = value.String
			}
		case token.FieldCustomAgentKey:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field custom_agent_key", values[i])
			} else if value.Valid {
				t.CustomAgentKey = value.String
			}
		case token.FieldOpenaiBase:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field openai_base", values[i])
			} else if value.Valid {
				t.OpenaiBase = value.String
			}
		case token.FieldOpenaiKey:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field openai_key", values[i])
			} else if value.Valid {
				t.OpenaiKey = value.String
			}
		default:
			t.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

// Value returns the ent.Value that was dynamically selected and assigned to the Token.
// This includes values selected through modifiers, order, etc.
func (t *Token) Value(name string) (ent.Value, error) {
	return t.selectValues.Get(name)
}

// QueryAgent queries the "agent" edge of the Token entity.
func (t *Token) QueryAgent() *AgentQuery {
	return NewTokenClient(t.config).QueryAgent(t)
}

// Update returns a builder for updating this Token.
// Note that you need to call Token.Unwrap() before calling this method if this Token
// was returned from a transaction, and the transaction was committed or rolled back.
func (t *Token) Update() *TokenUpdateOne {
	return NewTokenClient(t.config).UpdateOne(t)
}

// Unwrap unwraps the Token entity that was returned from a transaction after it was closed,
// so that all future queries will be executed through the driver which created the transaction.
func (t *Token) Unwrap() *Token {
	_tx, ok := t.config.driver.(*txDriver)
	if !ok {
		panic("ent: Token is not a transactional entity")
	}
	t.config.driver = _tx.drv
	return t
}

// String implements the fmt.Stringer.
func (t *Token) String() string {
	var builder strings.Builder
	builder.WriteString("Token(")
	builder.WriteString(fmt.Sprintf("id=%v, ", t.ID))
	builder.WriteString("created_at=")
	builder.WriteString(t.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(t.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(t.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("expire_at=")
	builder.WriteString(t.ExpireAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("token=")
	builder.WriteString(t.Token)
	builder.WriteString(", ")
	builder.WriteString("mac=")
	builder.WriteString(t.MAC)
	builder.WriteString(", ")
	builder.WriteString("organization_id=")
	builder.WriteString(fmt.Sprintf("%v", t.OrganizationID))
	builder.WriteString(", ")
	builder.WriteString("agent_id=")
	builder.WriteString(fmt.Sprintf("%v", t.AgentID))
	builder.WriteString(", ")
	builder.WriteString("custom_agent_base=")
	builder.WriteString(t.CustomAgentBase)
	builder.WriteString(", ")
	builder.WriteString("custom_agent_key=")
	builder.WriteString(t.CustomAgentKey)
	builder.WriteString(", ")
	builder.WriteString("openai_base=")
	builder.WriteString(t.OpenaiBase)
	builder.WriteString(", ")
	builder.WriteString("openai_key=")
	builder.WriteString(t.OpenaiKey)
	builder.WriteByte(')')
	return builder.String()
}

// Tokens is a parsable slice of Token.
type Tokens []*Token
