// 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/google/uuid"
	"github.com/lightsparkdev/spark/common/keys"
	"github.com/lightsparkdev/spark/so/ent/schema/schematype"
	"github.com/lightsparkdev/spark/so/ent/signingkeyshare"
)

// SigningKeyshare is the model entity for the SigningKeyshare schema.
type SigningKeyshare struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// CreateTime holds the value of the "create_time" field.
	CreateTime time.Time `json:"create_time,omitempty"`
	// UpdateTime holds the value of the "update_time" field.
	UpdateTime time.Time `json:"update_time,omitempty"`
	// The status of the signing keyshare (i.e. whether it is in use or not).
	Status schematype.SigningKeyshareStatus `json:"status,omitempty"`
	// The secret share of the signing keyshare held by this SO.
	SecretShare keys.Private `json:"secret_share,omitempty"`
	// A map from SO identifier to the public key of the secret share held by that SO.
	PublicShares map[string]keys.Public `json:"public_shares,omitempty"`
	// The public key of the combined secret represented by this signing keyshare.
	PublicKey keys.Public `json:"public_key,omitempty"`
	// The minimum number of signers required to produce a valid signature using this signing keyshare.
	MinSigners int32 `json:"min_signers,omitempty"`
	// The SO index of the coordinator that initiated the DKG round that produced this signing keyshare. An SO can only claim a signing keyshare to mark it in-use for which it is the coordinator.
	CoordinatorIndex uint64 `json:"coordinator_index,omitempty"`
	selectValues     sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*SigningKeyshare) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case signingkeyshare.FieldPublicShares:
			values[i] = new([]byte)
		case signingkeyshare.FieldSecretShare:
			values[i] = new(keys.Private)
		case signingkeyshare.FieldPublicKey:
			values[i] = new(keys.Public)
		case signingkeyshare.FieldMinSigners, signingkeyshare.FieldCoordinatorIndex:
			values[i] = new(sql.NullInt64)
		case signingkeyshare.FieldStatus:
			values[i] = new(sql.NullString)
		case signingkeyshare.FieldCreateTime, signingkeyshare.FieldUpdateTime:
			values[i] = new(sql.NullTime)
		case signingkeyshare.FieldID:
			values[i] = new(uuid.UUID)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the SigningKeyshare fields.
func (sk *SigningKeyshare) 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 signingkeyshare.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				sk.ID = *value
			}
		case signingkeyshare.FieldCreateTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field create_time", values[i])
			} else if value.Valid {
				sk.CreateTime = value.Time
			}
		case signingkeyshare.FieldUpdateTime:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field update_time", values[i])
			} else if value.Valid {
				sk.UpdateTime = value.Time
			}
		case signingkeyshare.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				sk.Status = schematype.SigningKeyshareStatus(value.String)
			}
		case signingkeyshare.FieldSecretShare:
			if value, ok := values[i].(*keys.Private); !ok {
				return fmt.Errorf("unexpected type %T for field secret_share", values[i])
			} else if value != nil {
				sk.SecretShare = *value
			}
		case signingkeyshare.FieldPublicShares:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field public_shares", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &sk.PublicShares); err != nil {
					return fmt.Errorf("unmarshal field public_shares: %w", err)
				}
			}
		case signingkeyshare.FieldPublicKey:
			if value, ok := values[i].(*keys.Public); !ok {
				return fmt.Errorf("unexpected type %T for field public_key", values[i])
			} else if value != nil {
				sk.PublicKey = *value
			}
		case signingkeyshare.FieldMinSigners:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field min_signers", values[i])
			} else if value.Valid {
				sk.MinSigners = int32(value.Int64)
			}
		case signingkeyshare.FieldCoordinatorIndex:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field coordinator_index", values[i])
			} else if value.Valid {
				sk.CoordinatorIndex = uint64(value.Int64)
			}
		default:
			sk.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

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

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

// String implements the fmt.Stringer.
func (sk *SigningKeyshare) String() string {
	var builder strings.Builder
	builder.WriteString("SigningKeyshare(")
	builder.WriteString(fmt.Sprintf("id=%v, ", sk.ID))
	builder.WriteString("create_time=")
	builder.WriteString(sk.CreateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("update_time=")
	builder.WriteString(sk.UpdateTime.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", sk.Status))
	builder.WriteString(", ")
	builder.WriteString("secret_share=")
	builder.WriteString(fmt.Sprintf("%v", sk.SecretShare))
	builder.WriteString(", ")
	builder.WriteString("public_shares=")
	builder.WriteString(fmt.Sprintf("%v", sk.PublicShares))
	builder.WriteString(", ")
	builder.WriteString("public_key=")
	builder.WriteString(fmt.Sprintf("%v", sk.PublicKey))
	builder.WriteString(", ")
	builder.WriteString("min_signers=")
	builder.WriteString(fmt.Sprintf("%v", sk.MinSigners))
	builder.WriteString(", ")
	builder.WriteString("coordinator_index=")
	builder.WriteString(fmt.Sprintf("%v", sk.CoordinatorIndex))
	builder.WriteByte(')')
	return builder.String()
}

// SigningKeyshares is a parsable slice of SigningKeyshare.
type SigningKeyshares []*SigningKeyshare
