// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"
	"wechat-api/ent/agentbase"

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

// AgentBase is the model entity for the AgentBase schema.
type AgentBase struct {
	config `json:"-"`
	// ID of the ent.
	// id
	ID string `json:"id,omitempty"`
	// q
	Q string `json:"q,omitempty"`
	// a
	A string `json:"a,omitempty"`
	// chunk_index
	ChunkIndex uint64 `json:"chunk_index,omitempty"`
	// indexes
	Indexes []string `json:"indexes,omitempty"`
	// dataset_id
	DatasetID string `json:"dataset_id,omitempty"`
	// collection_id
	CollectionID string `json:"collection_id,omitempty"`
	// source_name
	SourceName string `json:"source_name,omitempty"`
	// can_write
	CanWrite []bool `json:"can_write,omitempty"`
	// is_owner
	IsOwner []bool `json:"is_owner,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the AgentBaseQuery when eager-loading is set.
	Edges        AgentBaseEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

// WxAgentOrErr returns the WxAgent value or an error if the edge
// was not loaded in eager-loading.
func (e AgentBaseEdges) WxAgentOrErr() ([]*Wx, error) {
	if e.loadedTypes[0] {
		return e.WxAgent, nil
	}
	return nil, &NotLoadedError{edge: "wx_agent"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*AgentBase) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case agentbase.FieldIndexes, agentbase.FieldCanWrite, agentbase.FieldIsOwner:
			values[i] = new([]byte)
		case agentbase.FieldChunkIndex:
			values[i] = new(sql.NullInt64)
		case agentbase.FieldID, agentbase.FieldQ, agentbase.FieldA, agentbase.FieldDatasetID, agentbase.FieldCollectionID, agentbase.FieldSourceName:
			values[i] = new(sql.NullString)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the AgentBase fields.
func (ab *AgentBase) 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 agentbase.FieldID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value.Valid {
				ab.ID = value.String
			}
		case agentbase.FieldQ:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field q", values[i])
			} else if value.Valid {
				ab.Q = value.String
			}
		case agentbase.FieldA:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field a", values[i])
			} else if value.Valid {
				ab.A = value.String
			}
		case agentbase.FieldChunkIndex:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field chunk_index", values[i])
			} else if value.Valid {
				ab.ChunkIndex = uint64(value.Int64)
			}
		case agentbase.FieldIndexes:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field indexes", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &ab.Indexes); err != nil {
					return fmt.Errorf("unmarshal field indexes: %w", err)
				}
			}
		case agentbase.FieldDatasetID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field dataset_id", values[i])
			} else if value.Valid {
				ab.DatasetID = value.String
			}
		case agentbase.FieldCollectionID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field collection_id", values[i])
			} else if value.Valid {
				ab.CollectionID = value.String
			}
		case agentbase.FieldSourceName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field source_name", values[i])
			} else if value.Valid {
				ab.SourceName = value.String
			}
		case agentbase.FieldCanWrite:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field can_write", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &ab.CanWrite); err != nil {
					return fmt.Errorf("unmarshal field can_write: %w", err)
				}
			}
		case agentbase.FieldIsOwner:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field is_owner", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &ab.IsOwner); err != nil {
					return fmt.Errorf("unmarshal field is_owner: %w", err)
				}
			}
		default:
			ab.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryWxAgent queries the "wx_agent" edge of the AgentBase entity.
func (ab *AgentBase) QueryWxAgent() *WxQuery {
	return NewAgentBaseClient(ab.config).QueryWxAgent(ab)
}

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

// Unwrap unwraps the AgentBase 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 (ab *AgentBase) Unwrap() *AgentBase {
	_tx, ok := ab.config.driver.(*txDriver)
	if !ok {
		panic("ent: AgentBase is not a transactional entity")
	}
	ab.config.driver = _tx.drv
	return ab
}

// String implements the fmt.Stringer.
func (ab *AgentBase) String() string {
	var builder strings.Builder
	builder.WriteString("AgentBase(")
	builder.WriteString(fmt.Sprintf("id=%v, ", ab.ID))
	builder.WriteString("q=")
	builder.WriteString(ab.Q)
	builder.WriteString(", ")
	builder.WriteString("a=")
	builder.WriteString(ab.A)
	builder.WriteString(", ")
	builder.WriteString("chunk_index=")
	builder.WriteString(fmt.Sprintf("%v", ab.ChunkIndex))
	builder.WriteString(", ")
	builder.WriteString("indexes=")
	builder.WriteString(fmt.Sprintf("%v", ab.Indexes))
	builder.WriteString(", ")
	builder.WriteString("dataset_id=")
	builder.WriteString(ab.DatasetID)
	builder.WriteString(", ")
	builder.WriteString("collection_id=")
	builder.WriteString(ab.CollectionID)
	builder.WriteString(", ")
	builder.WriteString("source_name=")
	builder.WriteString(ab.SourceName)
	builder.WriteString(", ")
	builder.WriteString("can_write=")
	builder.WriteString(fmt.Sprintf("%v", ab.CanWrite))
	builder.WriteString(", ")
	builder.WriteString("is_owner=")
	builder.WriteString(fmt.Sprintf("%v", ab.IsOwner))
	builder.WriteByte(')')
	return builder.String()
}

// AgentBases is a parsable slice of AgentBase.
type AgentBases []*AgentBase