// Code generated by entc, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"code.icod.de/postfix/manager/ent/account"
	"entgo.io/ent/dialect/sql"
)

// Account is the model entity for the Account schema.
type Account struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// Created holds the value of the "created" field.
	Created time.Time `json:"created,omitempty"`
	// Modified holds the value of the "modified" field.
	Modified time.Time `json:"modified,omitempty"`
	// Username holds the value of the "username" field.
	Username string `json:"username,omitempty"`
	// Password holds the value of the "password" field.
	Password []byte `json:"password,omitempty"`
	// Super holds the value of the "super" field.
	Super bool `json:"super,omitempty"`
	// Active holds the value of the "active" field.
	Active bool `json:"active,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"`
}

// AccountEdges holds the relations/edges for other nodes in the graph.
type AccountEdges struct {
	// Domains holds the value of the domains edge.
	Domains []*Domain `json:"domains,omitempty"`
	// Logs holds the value of the logs edge.
	Logs []*Logentry `json:"logs,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [2]bool
}

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

// LogsOrErr returns the Logs value or an error if the edge
// was not loaded in eager-loading.
func (e AccountEdges) LogsOrErr() ([]*Logentry, error) {
	if e.loadedTypes[1] {
		return e.Logs, nil
	}
	return nil, &NotLoadedError{edge: "logs"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Account) scanValues(columns []string) ([]interface{}, error) {
	values := make([]interface{}, len(columns))
	for i := range columns {
		switch columns[i] {
		case account.FieldPassword:
			values[i] = new([]byte)
		case account.FieldSuper, account.FieldActive:
			values[i] = new(sql.NullBool)
		case account.FieldID:
			values[i] = new(sql.NullInt64)
		case account.FieldUsername:
			values[i] = new(sql.NullString)
		case account.FieldCreated, account.FieldModified:
			values[i] = new(sql.NullTime)
		default:
			return nil, fmt.Errorf("unexpected column %q for type Account", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Account fields.
func (a *Account) assignValues(columns []string, values []interface{}) 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)
			}
			a.ID = int64(value.Int64)
		case account.FieldCreated:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created", values[i])
			} else if value.Valid {
				a.Created = value.Time
			}
		case account.FieldModified:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field modified", values[i])
			} else if value.Valid {
				a.Modified = value.Time
			}
		case account.FieldUsername:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field username", values[i])
			} else if value.Valid {
				a.Username = value.String
			}
		case account.FieldPassword:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field password", values[i])
			} else if value != nil {
				a.Password = *value
			}
		case account.FieldSuper:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field super", values[i])
			} else if value.Valid {
				a.Super = value.Bool
			}
		case account.FieldActive:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field active", values[i])
			} else if value.Valid {
				a.Active = value.Bool
			}
		}
	}
	return nil
}

// QueryDomains queries the "domains" edge of the Account entity.
func (a *Account) QueryDomains() *DomainQuery {
	return (&AccountClient{config: a.config}).QueryDomains(a)
}

// QueryLogs queries the "logs" edge of the Account entity.
func (a *Account) QueryLogs() *LogentryQuery {
	return (&AccountClient{config: a.config}).QueryLogs(a)
}

// 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 (a *Account) Update() *AccountUpdateOne {
	return (&AccountClient{config: a.config}).UpdateOne(a)
}

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

// String implements the fmt.Stringer.
func (a *Account) String() string {
	var builder strings.Builder
	builder.WriteString("Account(")
	builder.WriteString(fmt.Sprintf("id=%v", a.ID))
	builder.WriteString(", created=")
	builder.WriteString(a.Created.Format(time.ANSIC))
	builder.WriteString(", modified=")
	builder.WriteString(a.Modified.Format(time.ANSIC))
	builder.WriteString(", username=")
	builder.WriteString(a.Username)
	builder.WriteString(", password=")
	builder.WriteString(fmt.Sprintf("%v", a.Password))
	builder.WriteString(", super=")
	builder.WriteString(fmt.Sprintf("%v", a.Super))
	builder.WriteString(", active=")
	builder.WriteString(fmt.Sprintf("%v", a.Active))
	builder.WriteByte(')')
	return builder.String()
}

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

func (a Accounts) config(cfg config) {
	for _i := range a {
		a[_i].config = cfg
	}
}
