// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent/dialect/sql"
	"github.com/appditto/pippin_nano_wallet/libs/database/ent/account"
	"github.com/appditto/pippin_nano_wallet/libs/database/ent/block"
	"github.com/google/uuid"
)

// Block is the model entity for the Block schema.
type Block struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// AccountID holds the value of the "account_id" field.
	AccountID *uuid.UUID `json:"account_id,omitempty"`
	// BlockHash holds the value of the "block_hash" field.
	BlockHash string `json:"block_hash,omitempty"`
	// Block holds the value of the "block" field.
	Block map[string]interface{} `json:"block,omitempty"`
	// SendID holds the value of the "send_id" field.
	SendID *string `json:"send_id,omitempty"`
	// Subtype holds the value of the "subtype" field.
	Subtype string `json:"subtype,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the BlockQuery when eager-loading is set.
	Edges BlockEdges `json:"edges"`
}

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*Block) scanValues(columns []string) ([]interface{}, error) {
	values := make([]interface{}, len(columns))
	for i := range columns {
		switch columns[i] {
		case block.FieldAccountID:
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case block.FieldBlock:
			values[i] = new([]byte)
		case block.FieldBlockHash, block.FieldSendID, block.FieldSubtype:
			values[i] = new(sql.NullString)
		case block.FieldCreatedAt:
			values[i] = new(sql.NullTime)
		case block.FieldID:
			values[i] = new(uuid.UUID)
		default:
			return nil, fmt.Errorf("unexpected column %q for type Block", columns[i])
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Block fields.
func (b *Block) 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 block.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				b.ID = *value
			}
		case block.FieldAccountID:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field account_id", values[i])
			} else if value.Valid {
				b.AccountID = new(uuid.UUID)
				*b.AccountID = *value.S.(*uuid.UUID)
			}
		case block.FieldBlockHash:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field block_hash", values[i])
			} else if value.Valid {
				b.BlockHash = value.String
			}
		case block.FieldBlock:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field block", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &b.Block); err != nil {
					return fmt.Errorf("unmarshal field block: %w", err)
				}
			}
		case block.FieldSendID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field send_id", values[i])
			} else if value.Valid {
				b.SendID = new(string)
				*b.SendID = value.String
			}
		case block.FieldSubtype:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field subtype", values[i])
			} else if value.Valid {
				b.Subtype = value.String
			}
		case block.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 {
				b.CreatedAt = value.Time
			}
		}
	}
	return nil
}

// QueryAccount queries the "account" edge of the Block entity.
func (b *Block) QueryAccount() *AccountQuery {
	return (&BlockClient{config: b.config}).QueryAccount(b)
}

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

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

// String implements the fmt.Stringer.
func (b *Block) String() string {
	var builder strings.Builder
	builder.WriteString("Block(")
	builder.WriteString(fmt.Sprintf("id=%v, ", b.ID))
	if v := b.AccountID; v != nil {
		builder.WriteString("account_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("block_hash=")
	builder.WriteString(b.BlockHash)
	builder.WriteString(", ")
	builder.WriteString("block=")
	builder.WriteString(fmt.Sprintf("%v", b.Block))
	builder.WriteString(", ")
	if v := b.SendID; v != nil {
		builder.WriteString("send_id=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("subtype=")
	builder.WriteString(b.Subtype)
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(b.CreatedAt.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// Blocks is a parsable slice of Block.
type Blocks []*Block

func (b Blocks) config(cfg config) {
	for _i := range b {
		b[_i].config = cfg
	}
}
