// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"
	"wechat-api/ent/agent"
	"wechat-api/ent/whatsapp"

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

// Whatsapp is the model entity for the Whatsapp schema.
type Whatsapp 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"`
	// Status 1: normal 2: ban | 状态 1 正常 2 禁用
	Status uint8 `json:"status,omitempty"`
	// Delete Time | 删除日期
	DeletedAt time.Time `json:"deleted_at,omitempty"`
	// 通道ID
	WaID string `json:"wa_id,omitempty"`
	// 通道名
	WaName string `json:"wa_name,omitempty"`
	// 回调地址
	Callback string `json:"callback,omitempty"`
	// AI角色ID
	AgentID uint64 `json:"agent_id,omitempty"`
	// 账号
	Account string `json:"account,omitempty"`
	// 国家区号
	Cc string `json:"cc,omitempty"`
	// 手机号
	Phone string `json:"phone,omitempty"`
	// 国家区号 + 手机号
	CcPhone string `json:"cc_phone,omitempty"`
	// 号码名称
	PhoneName string `json:"phone_name,omitempty"`
	// 号码状态
	PhoneStatus int8 `json:"phone_status,omitempty"`
	// 机构 ID
	OrganizationID uint64 `json:"organization_id,omitempty"`
	// 大模型服务账号
	APIBase string `json:"api_base,omitempty"`
	// 大模型服务密钥
	APIKey string `json:"api_key,omitempty"`
	// 白名单
	AllowList []string `json:"allow_list,omitempty"`
	// 群白名单
	GroupAllowList []string `json:"group_allow_list,omitempty"`
	// 黑名单
	BlockList []string `json:"block_list,omitempty"`
	// 群黑名单
	GroupBlockList []string `json:"group_block_list,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the WhatsappQuery when eager-loading is set.
	Edges        WhatsappEdges `json:"edges"`
	selectValues sql.SelectValues
}

// WhatsappEdges holds the relations/edges for other nodes in the graph.
type WhatsappEdges 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 WhatsappEdges) 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 (*Whatsapp) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case whatsapp.FieldAllowList, whatsapp.FieldGroupAllowList, whatsapp.FieldBlockList, whatsapp.FieldGroupBlockList:
			values[i] = new([]byte)
		case whatsapp.FieldID, whatsapp.FieldStatus, whatsapp.FieldAgentID, whatsapp.FieldPhoneStatus, whatsapp.FieldOrganizationID:
			values[i] = new(sql.NullInt64)
		case whatsapp.FieldWaID, whatsapp.FieldWaName, whatsapp.FieldCallback, whatsapp.FieldAccount, whatsapp.FieldCc, whatsapp.FieldPhone, whatsapp.FieldCcPhone, whatsapp.FieldPhoneName, whatsapp.FieldAPIBase, whatsapp.FieldAPIKey:
			values[i] = new(sql.NullString)
		case whatsapp.FieldCreatedAt, whatsapp.FieldUpdatedAt, whatsapp.FieldDeletedAt:
			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 Whatsapp fields.
func (w *Whatsapp) 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 whatsapp.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			w.ID = uint64(value.Int64)
		case whatsapp.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 {
				w.CreatedAt = value.Time
			}
		case whatsapp.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 {
				w.UpdatedAt = value.Time
			}
		case whatsapp.FieldStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				w.Status = uint8(value.Int64)
			}
		case whatsapp.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 {
				w.DeletedAt = value.Time
			}
		case whatsapp.FieldWaID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field wa_id", values[i])
			} else if value.Valid {
				w.WaID = value.String
			}
		case whatsapp.FieldWaName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field wa_name", values[i])
			} else if value.Valid {
				w.WaName = value.String
			}
		case whatsapp.FieldCallback:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field callback", values[i])
			} else if value.Valid {
				w.Callback = value.String
			}
		case whatsapp.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 {
				w.AgentID = uint64(value.Int64)
			}
		case whatsapp.FieldAccount:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field account", values[i])
			} else if value.Valid {
				w.Account = value.String
			}
		case whatsapp.FieldCc:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field cc", values[i])
			} else if value.Valid {
				w.Cc = value.String
			}
		case whatsapp.FieldPhone:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field phone", values[i])
			} else if value.Valid {
				w.Phone = value.String
			}
		case whatsapp.FieldCcPhone:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field cc_phone", values[i])
			} else if value.Valid {
				w.CcPhone = value.String
			}
		case whatsapp.FieldPhoneName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field phone_name", values[i])
			} else if value.Valid {
				w.PhoneName = value.String
			}
		case whatsapp.FieldPhoneStatus:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field phone_status", values[i])
			} else if value.Valid {
				w.PhoneStatus = int8(value.Int64)
			}
		case whatsapp.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 {
				w.OrganizationID = uint64(value.Int64)
			}
		case whatsapp.FieldAPIBase:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field api_base", values[i])
			} else if value.Valid {
				w.APIBase = value.String
			}
		case whatsapp.FieldAPIKey:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field api_key", values[i])
			} else if value.Valid {
				w.APIKey = value.String
			}
		case whatsapp.FieldAllowList:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field allow_list", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &w.AllowList); err != nil {
					return fmt.Errorf("unmarshal field allow_list: %w", err)
				}
			}
		case whatsapp.FieldGroupAllowList:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field group_allow_list", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &w.GroupAllowList); err != nil {
					return fmt.Errorf("unmarshal field group_allow_list: %w", err)
				}
			}
		case whatsapp.FieldBlockList:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field block_list", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &w.BlockList); err != nil {
					return fmt.Errorf("unmarshal field block_list: %w", err)
				}
			}
		case whatsapp.FieldGroupBlockList:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field group_block_list", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &w.GroupBlockList); err != nil {
					return fmt.Errorf("unmarshal field group_block_list: %w", err)
				}
			}
		default:
			w.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryAgent queries the "agent" edge of the Whatsapp entity.
func (w *Whatsapp) QueryAgent() *AgentQuery {
	return NewWhatsappClient(w.config).QueryAgent(w)
}

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

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

// String implements the fmt.Stringer.
func (w *Whatsapp) String() string {
	var builder strings.Builder
	builder.WriteString("Whatsapp(")
	builder.WriteString(fmt.Sprintf("id=%v, ", w.ID))
	builder.WriteString("created_at=")
	builder.WriteString(w.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(w.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", w.Status))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(w.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("wa_id=")
	builder.WriteString(w.WaID)
	builder.WriteString(", ")
	builder.WriteString("wa_name=")
	builder.WriteString(w.WaName)
	builder.WriteString(", ")
	builder.WriteString("callback=")
	builder.WriteString(w.Callback)
	builder.WriteString(", ")
	builder.WriteString("agent_id=")
	builder.WriteString(fmt.Sprintf("%v", w.AgentID))
	builder.WriteString(", ")
	builder.WriteString("account=")
	builder.WriteString(w.Account)
	builder.WriteString(", ")
	builder.WriteString("cc=")
	builder.WriteString(w.Cc)
	builder.WriteString(", ")
	builder.WriteString("phone=")
	builder.WriteString(w.Phone)
	builder.WriteString(", ")
	builder.WriteString("cc_phone=")
	builder.WriteString(w.CcPhone)
	builder.WriteString(", ")
	builder.WriteString("phone_name=")
	builder.WriteString(w.PhoneName)
	builder.WriteString(", ")
	builder.WriteString("phone_status=")
	builder.WriteString(fmt.Sprintf("%v", w.PhoneStatus))
	builder.WriteString(", ")
	builder.WriteString("organization_id=")
	builder.WriteString(fmt.Sprintf("%v", w.OrganizationID))
	builder.WriteString(", ")
	builder.WriteString("api_base=")
	builder.WriteString(w.APIBase)
	builder.WriteString(", ")
	builder.WriteString("api_key=")
	builder.WriteString(w.APIKey)
	builder.WriteString(", ")
	builder.WriteString("allow_list=")
	builder.WriteString(fmt.Sprintf("%v", w.AllowList))
	builder.WriteString(", ")
	builder.WriteString("group_allow_list=")
	builder.WriteString(fmt.Sprintf("%v", w.GroupAllowList))
	builder.WriteString(", ")
	builder.WriteString("block_list=")
	builder.WriteString(fmt.Sprintf("%v", w.BlockList))
	builder.WriteString(", ")
	builder.WriteString("group_block_list=")
	builder.WriteString(fmt.Sprintf("%v", w.GroupBlockList))
	builder.WriteByte(')')
	return builder.String()
}

// Whatsapps is a parsable slice of Whatsapp.
type Whatsapps []*Whatsapp
