// Code generated by ent, DO NOT EDIT.

package models

import (
	"encoding/json"
	"fmt"
	"juwan-backend/app/users/rpc/internal/models/schema"
	"juwan-backend/app/users/rpc/internal/models/users"
	"juwan-backend/pkg/types"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
)

// Users is the model entity for the Users schema.
type Users struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// Username holds the value of the "username" field.
	Username string `json:"username,omitempty"`
	// PasswordHash holds the value of the "password_hash" field.
	PasswordHash string `json:"password_hash,omitempty"`
	// Email holds the value of the "email" field.
	Email string `json:"email,omitempty"`
	// Phone holds the value of the "phone" field.
	Phone string `json:"phone,omitempty"`
	// Nickname holds the value of the "nickname" field.
	Nickname string `json:"nickname,omitempty"`
	// Avatar holds the value of the "avatar" field.
	Avatar string `json:"avatar,omitempty"`
	// Bio holds the value of the "bio" field.
	Bio string `json:"bio,omitempty"`
	// CurrentRole holds the value of the "current_role" field.
	CurrentRole string `json:"current_role,omitempty"`
	// VerificationStatus holds the value of the "verificationStatus" field.
	VerificationStatus schema.VerificationStatusStruct `json:"verificationStatus,omitempty"`
	// IsAdmin holds the value of the "is_admin" field.
	IsAdmin bool `json:"is_admin,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// UpdatedAt holds the value of the "updated_at" field.
	UpdatedAt time.Time `json:"updated_at,omitempty"`
	// DeletedAt holds the value of the "deleted_at" field.
	DeletedAt time.Time `json:"deleted_at,omitempty"`
	// VerifiedRoles holds the value of the "verified_roles" field.
	VerifiedRoles types.TextArray `json:"verified_roles,omitempty"`
	selectValues  sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Users) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case users.FieldVerificationStatus:
			values[i] = new([]byte)
		case users.FieldIsAdmin:
			values[i] = new(sql.NullBool)
		case users.FieldID:
			values[i] = new(sql.NullInt64)
		case users.FieldUsername, users.FieldPasswordHash, users.FieldEmail, users.FieldPhone, users.FieldNickname, users.FieldAvatar, users.FieldBio, users.FieldCurrentRole:
			values[i] = new(sql.NullString)
		case users.FieldCreatedAt, users.FieldUpdatedAt, users.FieldDeletedAt:
			values[i] = new(sql.NullTime)
		case users.FieldVerifiedRoles:
			values[i] = new(types.TextArray)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Users fields.
func (_m *Users) 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 users.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 users.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 users.FieldPasswordHash:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field password_hash", values[i])
			} else if value.Valid {
				_m.PasswordHash = value.String
			}
		case users.FieldEmail:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field email", values[i])
			} else if value.Valid {
				_m.Email = value.String
			}
		case users.FieldPhone:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field phone", values[i])
			} else if value.Valid {
				_m.Phone = value.String
			}
		case users.FieldNickname:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field nickname", values[i])
			} else if value.Valid {
				_m.Nickname = value.String
			}
		case users.FieldAvatar:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field avatar", values[i])
			} else if value.Valid {
				_m.Avatar = value.String
			}
		case users.FieldBio:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field bio", values[i])
			} else if value.Valid {
				_m.Bio = value.String
			}
		case users.FieldCurrentRole:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field current_role", values[i])
			} else if value.Valid {
				_m.CurrentRole = value.String
			}
		case users.FieldVerificationStatus:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field verificationStatus", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.VerificationStatus); err != nil {
					return fmt.Errorf("unmarshal field verificationStatus: %w", err)
				}
			}
		case users.FieldIsAdmin:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_admin", values[i])
			} else if value.Valid {
				_m.IsAdmin = value.Bool
			}
		case users.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 users.FieldUpdatedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updated_at", values[i])
			} else if value.Valid {
				_m.UpdatedAt = value.Time
			}
		case users.FieldDeletedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field deleted_at", values[i])
			} else if value.Valid {
				_m.DeletedAt = value.Time
			}
		case users.FieldVerifiedRoles:
			if value, ok := values[i].(*types.TextArray); !ok {
				return fmt.Errorf("unexpected type %T for field verified_roles", values[i])
			} else if value != nil {
				_m.VerifiedRoles = *value
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

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

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

// String implements the fmt.Stringer.
func (_m *Users) String() string {
	var builder strings.Builder
	builder.WriteString("Users(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("username=")
	builder.WriteString(_m.Username)
	builder.WriteString(", ")
	builder.WriteString("password_hash=")
	builder.WriteString(_m.PasswordHash)
	builder.WriteString(", ")
	builder.WriteString("email=")
	builder.WriteString(_m.Email)
	builder.WriteString(", ")
	builder.WriteString("phone=")
	builder.WriteString(_m.Phone)
	builder.WriteString(", ")
	builder.WriteString("nickname=")
	builder.WriteString(_m.Nickname)
	builder.WriteString(", ")
	builder.WriteString("avatar=")
	builder.WriteString(_m.Avatar)
	builder.WriteString(", ")
	builder.WriteString("bio=")
	builder.WriteString(_m.Bio)
	builder.WriteString(", ")
	builder.WriteString("current_role=")
	builder.WriteString(_m.CurrentRole)
	builder.WriteString(", ")
	builder.WriteString("verificationStatus=")
	builder.WriteString(fmt.Sprintf("%v", _m.VerificationStatus))
	builder.WriteString(", ")
	builder.WriteString("is_admin=")
	builder.WriteString(fmt.Sprintf("%v", _m.IsAdmin))
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(_m.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("verified_roles=")
	builder.WriteString(fmt.Sprintf("%v", _m.VerifiedRoles))
	builder.WriteByte(')')
	return builder.String()
}

// UsersSlice is a parsable slice of Users.
type UsersSlice []*Users
