// Code generated by ent, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/Wei-Shaw/sub2api/ent/account"
	"github.com/Wei-Shaw/sub2api/ent/proxy"
)

// Account is the model entity for the Account schema.
type Account struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// UpdatedAt holds the value of the "updated_at" field.
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// DeletedAt holds the value of the "deleted_at" field.
	DeletedAt *time.Time `json:"deleted_at,omitempty"`
	// Name holds the value of the "name" field.
	Name string `json:"name,omitempty"`
	// Notes holds the value of the "notes" field.
	Notes *string `json:"notes,omitempty"`
	// Platform holds the value of the "platform" field.
	Platform string `json:"platform,omitempty"`
	// Type holds the value of the "type" field.
	Type string `json:"type,omitempty"`
	// Credentials holds the value of the "credentials" field.
	Credentials map[string]interface{} `json:"credentials,omitempty"`
	// Extra holds the value of the "extra" field.
	Extra map[string]interface{} `json:"extra,omitempty"`
	// ProxyID holds the value of the "proxy_id" field.
	ProxyID *int64 `json:"proxy_id,omitempty"`
	// Concurrency holds the value of the "concurrency" field.
	Concurrency int `json:"concurrency,omitempty"`
	// Priority holds the value of the "priority" field.
	Priority int `json:"priority,omitempty"`
	// Status holds the value of the "status" field.
	Status string `json:"status,omitempty"`
	// ErrorMessage holds the value of the "error_message" field.
	ErrorMessage *string `json:"error_message,omitempty"`
	// LastUsedAt holds the value of the "last_used_at" field.
	LastUsedAt *time.Time `json:"last_used_at,omitempty"`
	// Account expiration time (NULL means no expiration).
	ExpiresAt *time.Time `json:"expires_at,omitempty"`
	// Auto pause scheduling when account expires.
	AutoPauseOnExpired bool `json:"auto_pause_on_expired,omitempty"`
	// Schedulable holds the value of the "schedulable" field.
	Schedulable bool `json:"schedulable,omitempty"`
	// RateLimitedAt holds the value of the "rate_limited_at" field.
	RateLimitedAt *time.Time `json:"rate_limited_at,omitempty"`
	// RateLimitResetAt holds the value of the "rate_limit_reset_at" field.
	RateLimitResetAt *time.Time `json:"rate_limit_reset_at,omitempty"`
	// OverloadUntil holds the value of the "overload_until" field.
	OverloadUntil *time.Time `json:"overload_until,omitempty"`
	// SessionWindowStart holds the value of the "session_window_start" field.
	SessionWindowStart *time.Time `json:"session_window_start,omitempty"`
	// SessionWindowEnd holds the value of the "session_window_end" field.
	SessionWindowEnd *time.Time `json:"session_window_end,omitempty"`
	// SessionWindowStatus holds the value of the "session_window_status" field.
	SessionWindowStatus *string `json:"session_window_status,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the AccountQuery when eager-loading is set.
	Edges        AccountEdges `json:"edges"`
	selectValues sql.SelectValues
}

// AccountEdges holds the relations/edges for other nodes in the graph.
type AccountEdges struct {
	// Groups holds the value of the groups edge.
	Groups []*Group `json:"groups,omitempty"`
	// Proxy holds the value of the proxy edge.
	Proxy *Proxy `json:"proxy,omitempty"`
	// UsageLogs holds the value of the usage_logs edge.
	UsageLogs []*UsageLog `json:"usage_logs,omitempty"`
	// AccountGroups holds the value of the account_groups edge.
	AccountGroups []*AccountGroup `json:"account_groups,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [4]bool
}

// GroupsOrErr returns the Groups value or an error if the edge
// was not loaded in eager-loading.
func (e AccountEdges) GroupsOrErr() ([]*Group, error) {
	if e.loadedTypes[0] {
		return e.Groups, nil
	}
	return nil, &NotLoadedError{edge: "groups"}
}

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

// UsageLogsOrErr returns the UsageLogs value or an error if the edge
// was not loaded in eager-loading.
func (e AccountEdges) UsageLogsOrErr() ([]*UsageLog, error) {
	if e.loadedTypes[2] {
		return e.UsageLogs, nil
	}
	return nil, &NotLoadedError{edge: "usage_logs"}
}

// AccountGroupsOrErr returns the AccountGroups value or an error if the edge
// was not loaded in eager-loading.
func (e AccountEdges) AccountGroupsOrErr() ([]*AccountGroup, error) {
	if e.loadedTypes[3] {
		return e.AccountGroups, nil
	}
	return nil, &NotLoadedError{edge: "account_groups"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Account) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case account.FieldCredentials, account.FieldExtra:
			values[i] = new([]byte)
		case account.FieldAutoPauseOnExpired, account.FieldSchedulable:
			values[i] = new(sql.NullBool)
		case account.FieldID, account.FieldProxyID, account.FieldConcurrency, account.FieldPriority:
			values[i] = new(sql.NullInt64)
		case account.FieldName, account.FieldNotes, account.FieldPlatform, account.FieldType, account.FieldStatus, account.FieldErrorMessage, account.FieldSessionWindowStatus:
			values[i] = new(sql.NullString)
		case account.FieldCreatedAt, account.FieldUpdatedAt, account.FieldDeletedAt, account.FieldLastUsedAt, account.FieldExpiresAt, account.FieldRateLimitedAt, account.FieldRateLimitResetAt, account.FieldOverloadUntil, account.FieldSessionWindowStart, account.FieldSessionWindowEnd:
			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 Account fields.
func (_m *Account) 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 account.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			_m.ID = int64(value.Int64)
		case account.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 {
				_m.CreatedAt = value.Time
			}
		case account.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 {
				_m.UpdatedAt = value.Time
			}
		case account.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 {
				_m.DeletedAt = new(time.Time)
				*_m.DeletedAt = value.Time
			}
		case account.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				_m.Name = value.String
			}
		case account.FieldNotes:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field notes", values[i])
			} else if value.Valid {
				_m.Notes = new(string)
				*_m.Notes = value.String
			}
		case account.FieldPlatform:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field platform", values[i])
			} else if value.Valid {
				_m.Platform = value.String
			}
		case account.FieldType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				_m.Type = value.String
			}
		case account.FieldCredentials:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field credentials", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Credentials); err != nil {
					return fmt.Errorf("unmarshal field credentials: %w", err)
				}
			}
		case account.FieldExtra:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field extra", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Extra); err != nil {
					return fmt.Errorf("unmarshal field extra: %w", err)
				}
			}
		case account.FieldProxyID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field proxy_id", values[i])
			} else if value.Valid {
				_m.ProxyID = new(int64)
				*_m.ProxyID = value.Int64
			}
		case account.FieldConcurrency:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field concurrency", values[i])
			} else if value.Valid {
				_m.Concurrency = int(value.Int64)
			}
		case account.FieldPriority:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field priority", values[i])
			} else if value.Valid {
				_m.Priority = int(value.Int64)
			}
		case account.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				_m.Status = value.String
			}
		case account.FieldErrorMessage:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field error_message", values[i])
			} else if value.Valid {
				_m.ErrorMessage = new(string)
				*_m.ErrorMessage = value.String
			}
		case account.FieldLastUsedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field last_used_at", values[i])
			} else if value.Valid {
				_m.LastUsedAt = new(time.Time)
				*_m.LastUsedAt = value.Time
			}
		case account.FieldExpiresAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field expires_at", values[i])
			} else if value.Valid {
				_m.ExpiresAt = new(time.Time)
				*_m.ExpiresAt = value.Time
			}
		case account.FieldAutoPauseOnExpired:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field auto_pause_on_expired", values[i])
			} else if value.Valid {
				_m.AutoPauseOnExpired = value.Bool
			}
		case account.FieldSchedulable:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field schedulable", values[i])
			} else if value.Valid {
				_m.Schedulable = value.Bool
			}
		case account.FieldRateLimitedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field rate_limited_at", values[i])
			} else if value.Valid {
				_m.RateLimitedAt = new(time.Time)
				*_m.RateLimitedAt = value.Time
			}
		case account.FieldRateLimitResetAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field rate_limit_reset_at", values[i])
			} else if value.Valid {
				_m.RateLimitResetAt = new(time.Time)
				*_m.RateLimitResetAt = value.Time
			}
		case account.FieldOverloadUntil:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field overload_until", values[i])
			} else if value.Valid {
				_m.OverloadUntil = new(time.Time)
				*_m.OverloadUntil = value.Time
			}
		case account.FieldSessionWindowStart:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field session_window_start", values[i])
			} else if value.Valid {
				_m.SessionWindowStart = new(time.Time)
				*_m.SessionWindowStart = value.Time
			}
		case account.FieldSessionWindowEnd:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field session_window_end", values[i])
			} else if value.Valid {
				_m.SessionWindowEnd = new(time.Time)
				*_m.SessionWindowEnd = value.Time
			}
		case account.FieldSessionWindowStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field session_window_status", values[i])
			} else if value.Valid {
				_m.SessionWindowStatus = new(string)
				*_m.SessionWindowStatus = value.String
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryGroups queries the "groups" edge of the Account entity.
func (_m *Account) QueryGroups() *GroupQuery {
	return NewAccountClient(_m.config).QueryGroups(_m)
}

// QueryProxy queries the "proxy" edge of the Account entity.
func (_m *Account) QueryProxy() *ProxyQuery {
	return NewAccountClient(_m.config).QueryProxy(_m)
}

// QueryUsageLogs queries the "usage_logs" edge of the Account entity.
func (_m *Account) QueryUsageLogs() *UsageLogQuery {
	return NewAccountClient(_m.config).QueryUsageLogs(_m)
}

// QueryAccountGroups queries the "account_groups" edge of the Account entity.
func (_m *Account) QueryAccountGroups() *AccountGroupQuery {
	return NewAccountClient(_m.config).QueryAccountGroups(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *Account) String() string {
	var builder strings.Builder
	builder.WriteString("Account(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := _m.DeletedAt; v != nil {
		builder.WriteString("deleted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(_m.Name)
	builder.WriteString(", ")
	if v := _m.Notes; v != nil {
		builder.WriteString("notes=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("platform=")
	builder.WriteString(_m.Platform)
	builder.WriteString(", ")
	builder.WriteString("type=")
	builder.WriteString(_m.Type)
	builder.WriteString(", ")
	builder.WriteString("credentials=")
	builder.WriteString(fmt.Sprintf("%v", _m.Credentials))
	builder.WriteString(", ")
	builder.WriteString("extra=")
	builder.WriteString(fmt.Sprintf("%v", _m.Extra))
	builder.WriteString(", ")
	if v := _m.ProxyID; v != nil {
		builder.WriteString("proxy_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("concurrency=")
	builder.WriteString(fmt.Sprintf("%v", _m.Concurrency))
	builder.WriteString(", ")
	builder.WriteString("priority=")
	builder.WriteString(fmt.Sprintf("%v", _m.Priority))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(_m.Status)
	builder.WriteString(", ")
	if v := _m.ErrorMessage; v != nil {
		builder.WriteString("error_message=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	if v := _m.LastUsedAt; v != nil {
		builder.WriteString("last_used_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.ExpiresAt; v != nil {
		builder.WriteString("expires_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("auto_pause_on_expired=")
	builder.WriteString(fmt.Sprintf("%v", _m.AutoPauseOnExpired))
	builder.WriteString(", ")
	builder.WriteString("schedulable=")
	builder.WriteString(fmt.Sprintf("%v", _m.Schedulable))
	builder.WriteString(", ")
	if v := _m.RateLimitedAt; v != nil {
		builder.WriteString("rate_limited_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.RateLimitResetAt; v != nil {
		builder.WriteString("rate_limit_reset_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.OverloadUntil; v != nil {
		builder.WriteString("overload_until=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.SessionWindowStart; v != nil {
		builder.WriteString("session_window_start=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.SessionWindowEnd; v != nil {
		builder.WriteString("session_window_end=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.SessionWindowStatus; v != nil {
		builder.WriteString("session_window_status=")
		builder.WriteString(*v)
	}
	builder.WriteByte(')')
	return builder.String()
}

// Accounts is a parsable slice of Account.
type Accounts []*Account
