// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/member-gentei/member-gentei/gentei/ent/user"
	"github.com/member-gentei/member-gentei/gentei/ent/usermembership"
	"github.com/member-gentei/member-gentei/gentei/ent/youtubetalent"
)

// UserMembership is the model entity for the UserMembership schema.
type UserMembership struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// FirstFailed holds the value of the "first_failed" field.
	FirstFailed time.Time `json:"first_failed,omitempty"`
	// LastVerified holds the value of the "last_verified" field.
	LastVerified time.Time `json:"last_verified,omitempty"`
	// FailCount holds the value of the "fail_count" field.
	FailCount int `json:"fail_count,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the UserMembershipQuery when eager-loading is set.
	Edges                          UserMembershipEdges `json:"edges"`
	user_memberships               *uint64
	user_membership_youtube_talent *string
	selectValues                   sql.SelectValues
}

// UserMembershipEdges holds the relations/edges for other nodes in the graph.
type UserMembershipEdges struct {
	// User holds the value of the user edge.
	User *User `json:"user,omitempty"`
	// YoutubeTalent holds the value of the youtube_talent edge.
	YoutubeTalent *YouTubeTalent `json:"youtube_talent,omitempty"`
	// Roles holds the value of the roles edge.
	Roles []*GuildRole `json:"roles,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

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

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

// RolesOrErr returns the Roles value or an error if the edge
// was not loaded in eager-loading.
func (e UserMembershipEdges) RolesOrErr() ([]*GuildRole, error) {
	if e.loadedTypes[2] {
		return e.Roles, nil
	}
	return nil, &NotLoadedError{edge: "roles"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*UserMembership) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case usermembership.FieldID, usermembership.FieldFailCount:
			values[i] = new(sql.NullInt64)
		case usermembership.FieldFirstFailed, usermembership.FieldLastVerified:
			values[i] = new(sql.NullTime)
		case usermembership.ForeignKeys[0]: // user_memberships
			values[i] = new(sql.NullInt64)
		case usermembership.ForeignKeys[1]: // user_membership_youtube_talent
			values[i] = new(sql.NullString)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the UserMembership fields.
func (um *UserMembership) 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 usermembership.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			um.ID = int(value.Int64)
		case usermembership.FieldFirstFailed:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field first_failed", values[i])
			} else if value.Valid {
				um.FirstFailed = value.Time
			}
		case usermembership.FieldLastVerified:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field last_verified", values[i])
			} else if value.Valid {
				um.LastVerified = value.Time
			}
		case usermembership.FieldFailCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field fail_count", values[i])
			} else if value.Valid {
				um.FailCount = int(value.Int64)
			}
		case usermembership.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field user_memberships", value)
			} else if value.Valid {
				um.user_memberships = new(uint64)
				*um.user_memberships = uint64(value.Int64)
			}
		case usermembership.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field user_membership_youtube_talent", values[i])
			} else if value.Valid {
				um.user_membership_youtube_talent = new(string)
				*um.user_membership_youtube_talent = value.String
			}
		default:
			um.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryUser queries the "user" edge of the UserMembership entity.
func (um *UserMembership) QueryUser() *UserQuery {
	return NewUserMembershipClient(um.config).QueryUser(um)
}

// QueryYoutubeTalent queries the "youtube_talent" edge of the UserMembership entity.
func (um *UserMembership) QueryYoutubeTalent() *YouTubeTalentQuery {
	return NewUserMembershipClient(um.config).QueryYoutubeTalent(um)
}

// QueryRoles queries the "roles" edge of the UserMembership entity.
func (um *UserMembership) QueryRoles() *GuildRoleQuery {
	return NewUserMembershipClient(um.config).QueryRoles(um)
}

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

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

// String implements the fmt.Stringer.
func (um *UserMembership) String() string {
	var builder strings.Builder
	builder.WriteString("UserMembership(")
	builder.WriteString(fmt.Sprintf("id=%v, ", um.ID))
	builder.WriteString("first_failed=")
	builder.WriteString(um.FirstFailed.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("last_verified=")
	builder.WriteString(um.LastVerified.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("fail_count=")
	builder.WriteString(fmt.Sprintf("%v", um.FailCount))
	builder.WriteByte(')')
	return builder.String()
}

// UserMemberships is a parsable slice of UserMembership.
type UserMemberships []*UserMembership
