// Code generated by entc, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

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

// Mailbox is the model entity for the Mailbox schema.
type Mailbox struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// Active holds the value of the "active" field.
	Active bool `json:"active,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"`
	// DomainID holds the value of the "domain_id" field.
	DomainID int64 `json:"domain_id,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"`
	// Name holds the value of the "name" field.
	Name *string `json:"name,omitempty"`
	// Quota holds the value of the "quota" field.
	Quota int64 `json:"quota,omitempty"`
	// LocalPart holds the value of the "local_part" field.
	LocalPart string `json:"local_part,omitempty"`
	// Homedir holds the value of the "homedir" field.
	Homedir *string `json:"homedir,omitempty"`
	// Maildir holds the value of the "maildir" field.
	Maildir *string `json:"maildir,omitempty"`
	// UID holds the value of the "uid" field.
	UID *int32 `json:"uid,omitempty"`
	// Gid holds the value of the "gid" field.
	Gid *int32 `json:"gid,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the MailboxQuery when eager-loading is set.
	Edges MailboxEdges `json:"edges"`
}

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

// DomainOrErr returns the Domain value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e MailboxEdges) DomainOrErr() (*Domain, error) {
	if e.loadedTypes[0] {
		if e.Domain == nil {
			// The edge domain was loaded in eager-loading,
			// but was not found.
			return nil, &NotFoundError{label: domain.Label}
		}
		return e.Domain, nil
	}
	return nil, &NotLoadedError{edge: "domain"}
}

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

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Mailbox fields.
func (m *Mailbox) 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 mailbox.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 mailbox.FieldActive:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field active", values[i])
			} else if value.Valid {
				m.Active = value.Bool
			}
		case mailbox.FieldCreated:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created", values[i])
			} else if value.Valid {
				m.Created = value.Time
			}
		case mailbox.FieldModified:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field modified", values[i])
			} else if value.Valid {
				m.Modified = new(time.Time)
				*m.Modified = value.Time
			}
		case mailbox.FieldDomainID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field domain_id", values[i])
			} else if value.Valid {
				m.DomainID = value.Int64
			}
		case mailbox.FieldUsername:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field username", values[i])
			} else if value.Valid {
				m.Username = value.String
			}
		case mailbox.FieldPassword:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field password", values[i])
			} else if value != nil {
				m.Password = *value
			}
		case mailbox.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 = new(string)
				*m.Name = value.String
			}
		case mailbox.FieldQuota:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field quota", values[i])
			} else if value.Valid {
				m.Quota = value.Int64
			}
		case mailbox.FieldLocalPart:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field local_part", values[i])
			} else if value.Valid {
				m.LocalPart = value.String
			}
		case mailbox.FieldHomedir:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field homedir", values[i])
			} else if value.Valid {
				m.Homedir = new(string)
				*m.Homedir = value.String
			}
		case mailbox.FieldMaildir:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field maildir", values[i])
			} else if value.Valid {
				m.Maildir = new(string)
				*m.Maildir = value.String
			}
		case mailbox.FieldUID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field uid", values[i])
			} else if value.Valid {
				m.UID = new(int32)
				*m.UID = int32(value.Int64)
			}
		case mailbox.FieldGid:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field gid", values[i])
			} else if value.Valid {
				m.Gid = new(int32)
				*m.Gid = int32(value.Int64)
			}
		}
	}
	return nil
}

// QueryDomain queries the "domain" edge of the Mailbox entity.
func (m *Mailbox) QueryDomain() *DomainQuery {
	return (&MailboxClient{config: m.config}).QueryDomain(m)
}

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

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

// String implements the fmt.Stringer.
func (m *Mailbox) String() string {
	var builder strings.Builder
	builder.WriteString("Mailbox(")
	builder.WriteString(fmt.Sprintf("id=%v", m.ID))
	builder.WriteString(", active=")
	builder.WriteString(fmt.Sprintf("%v", m.Active))
	builder.WriteString(", created=")
	builder.WriteString(m.Created.Format(time.ANSIC))
	if v := m.Modified; v != nil {
		builder.WriteString(", modified=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", domain_id=")
	builder.WriteString(fmt.Sprintf("%v", m.DomainID))
	builder.WriteString(", username=")
	builder.WriteString(m.Username)
	builder.WriteString(", password=")
	builder.WriteString(fmt.Sprintf("%v", m.Password))
	if v := m.Name; v != nil {
		builder.WriteString(", name=")
		builder.WriteString(*v)
	}
	builder.WriteString(", quota=")
	builder.WriteString(fmt.Sprintf("%v", m.Quota))
	builder.WriteString(", local_part=")
	builder.WriteString(m.LocalPart)
	if v := m.Homedir; v != nil {
		builder.WriteString(", homedir=")
		builder.WriteString(*v)
	}
	if v := m.Maildir; v != nil {
		builder.WriteString(", maildir=")
		builder.WriteString(*v)
	}
	if v := m.UID; v != nil {
		builder.WriteString(", uid=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	if v := m.Gid; v != nil {
		builder.WriteString(", gid=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteByte(')')
	return builder.String()
}

// Mailboxes is a parsable slice of Mailbox.
type Mailboxes []*Mailbox

func (m Mailboxes) config(cfg config) {
	for _i := range m {
		m[_i].config = cfg
	}
}
