// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/Wei-Shaw/sub2api/ent/group"
	"github.com/Wei-Shaw/sub2api/ent/redeemcode"
	"github.com/Wei-Shaw/sub2api/ent/user"
)

// RedeemCode is the model entity for the RedeemCode schema.
type RedeemCode struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// Code holds the value of the "code" field.
	Code string `json:"code,omitempty"`
	// Type holds the value of the "type" field.
	Type string `json:"type,omitempty"`
	// Value holds the value of the "value" field.
	Value float64 `json:"value,omitempty"`
	// Status holds the value of the "status" field.
	Status string `json:"status,omitempty"`
	// UsedBy holds the value of the "used_by" field.
	UsedBy *int64 `json:"used_by,omitempty"`
	// UsedAt holds the value of the "used_at" field.
	UsedAt *time.Time `json:"used_at,omitempty"`
	// Notes holds the value of the "notes" field.
	Notes *string `json:"notes,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// GroupID holds the value of the "group_id" field.
	GroupID *int64 `json:"group_id,omitempty"`
	// ValidityDays holds the value of the "validity_days" field.
	ValidityDays int `json:"validity_days,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the RedeemCodeQuery when eager-loading is set.
	Edges        RedeemCodeEdges `json:"edges"`
	selectValues sql.SelectValues
}

// RedeemCodeEdges holds the relations/edges for other nodes in the graph.
type RedeemCodeEdges struct {
	// User holds the value of the user edge.
	User *User `json:"user,omitempty"`
	// Group holds the value of the group edge.
	Group *Group `json:"group,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [2]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 RedeemCodeEdges) 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"}
}

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

// scanValues returns the types for scanning values from sql.Rows.
func (*RedeemCode) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case redeemcode.FieldValue:
			values[i] = new(sql.NullFloat64)
		case redeemcode.FieldID, redeemcode.FieldUsedBy, redeemcode.FieldGroupID, redeemcode.FieldValidityDays:
			values[i] = new(sql.NullInt64)
		case redeemcode.FieldCode, redeemcode.FieldType, redeemcode.FieldStatus, redeemcode.FieldNotes:
			values[i] = new(sql.NullString)
		case redeemcode.FieldUsedAt, redeemcode.FieldCreatedAt:
			values[i] = new(sql.NullTime)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the RedeemCode fields.
func (_m *RedeemCode) 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 redeemcode.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 redeemcode.FieldCode:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field code", values[i])
			} else if value.Valid {
				_m.Code = value.String
			}
		case redeemcode.FieldType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				_m.Type = value.String
			}
		case redeemcode.FieldValue:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field value", values[i])
			} else if value.Valid {
				_m.Value = value.Float64
			}
		case redeemcode.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				_m.Status = value.String
			}
		case redeemcode.FieldUsedBy:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field used_by", values[i])
			} else if value.Valid {
				_m.UsedBy = new(int64)
				*_m.UsedBy = value.Int64
			}
		case redeemcode.FieldUsedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field used_at", values[i])
			} else if value.Valid {
				_m.UsedAt = new(time.Time)
				*_m.UsedAt = value.Time
			}
		case redeemcode.FieldNotes:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field notes", values[i])
			} else if value.Valid {
				_m.Notes = new(string)
				*_m.Notes = value.String
			}
		case redeemcode.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 {
				_m.CreatedAt = value.Time
			}
		case redeemcode.FieldGroupID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field group_id", values[i])
			} else if value.Valid {
				_m.GroupID = new(int64)
				*_m.GroupID = value.Int64
			}
		case redeemcode.FieldValidityDays:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field validity_days", values[i])
			} else if value.Valid {
				_m.ValidityDays = int(value.Int64)
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryUser queries the "user" edge of the RedeemCode entity.
func (_m *RedeemCode) QueryUser() *UserQuery {
	return NewRedeemCodeClient(_m.config).QueryUser(_m)
}

// QueryGroup queries the "group" edge of the RedeemCode entity.
func (_m *RedeemCode) QueryGroup() *GroupQuery {
	return NewRedeemCodeClient(_m.config).QueryGroup(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *RedeemCode) String() string {
	var builder strings.Builder
	builder.WriteString("RedeemCode(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("code=")
	builder.WriteString(_m.Code)
	builder.WriteString(", ")
	builder.WriteString("type=")
	builder.WriteString(_m.Type)
	builder.WriteString(", ")
	builder.WriteString("value=")
	builder.WriteString(fmt.Sprintf("%v", _m.Value))
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(_m.Status)
	builder.WriteString(", ")
	if v := _m.UsedBy; v != nil {
		builder.WriteString("used_by=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	if v := _m.UsedAt; v != nil {
		builder.WriteString("used_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.Notes; v != nil {
		builder.WriteString("notes=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := _m.GroupID; v != nil {
		builder.WriteString("group_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("validity_days=")
	builder.WriteString(fmt.Sprintf("%v", _m.ValidityDays))
	builder.WriteByte(')')
	return builder.String()
}

// RedeemCodes is a parsable slice of RedeemCode.
type RedeemCodes []*RedeemCode
