// 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/cloudreve/Cloudreve/v4/ent/oauthclient"
	"github.com/cloudreve/Cloudreve/v4/ent/oauthgrant"
	"github.com/cloudreve/Cloudreve/v4/ent/user"
)

// OAuthGrant is the model entity for the OAuthGrant schema.
type OAuthGrant struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,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"`
	// UserID holds the value of the "user_id" field.
	UserID int `json:"user_id,omitempty"`
	// ClientID holds the value of the "client_id" field.
	ClientID int `json:"client_id,omitempty"`
	// Scopes holds the value of the "scopes" field.
	Scopes []string `json:"scopes,omitempty"`
	// LastUsedAt holds the value of the "last_used_at" field.
	LastUsedAt *time.Time `json:"last_used_at,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the OAuthGrantQuery when eager-loading is set.
	Edges        OAuthGrantEdges `json:"edges"`
	selectValues sql.SelectValues
}

// OAuthGrantEdges holds the relations/edges for other nodes in the graph.
type OAuthGrantEdges struct {
	// User holds the value of the user edge.
	User *User `json:"user,omitempty"`
	// Client holds the value of the client edge.
	Client *OAuthClient `json:"client,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 OAuthGrantEdges) UserOrErr() (*User, error) {
	if e.loadedTypes[0] {
		if e.User == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: user.Label}
		}
		return e.User, nil
	}
	return nil, &NotLoadedError{edge: "user"}
}

// ClientOrErr returns the Client value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e OAuthGrantEdges) ClientOrErr() (*OAuthClient, error) {
	if e.loadedTypes[1] {
		if e.Client == nil {
			// Edge was loaded but was not found.
			return nil, &NotFoundError{label: oauthclient.Label}
		}
		return e.Client, nil
	}
	return nil, &NotLoadedError{edge: "client"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*OAuthGrant) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case oauthgrant.FieldScopes:
			values[i] = new([]byte)
		case oauthgrant.FieldID, oauthgrant.FieldUserID, oauthgrant.FieldClientID:
			values[i] = new(sql.NullInt64)
		case oauthgrant.FieldCreatedAt, oauthgrant.FieldUpdatedAt, oauthgrant.FieldDeletedAt, oauthgrant.FieldLastUsedAt:
			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 OAuthGrant fields.
func (og *OAuthGrant) 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 oauthgrant.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			og.ID = int(value.Int64)
		case oauthgrant.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 {
				og.CreatedAt = value.Time
			}
		case oauthgrant.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 {
				og.UpdatedAt = value.Time
			}
		case oauthgrant.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 {
				og.DeletedAt = new(time.Time)
				*og.DeletedAt = value.Time
			}
		case oauthgrant.FieldUserID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field user_id", values[i])
			} else if value.Valid {
				og.UserID = int(value.Int64)
			}
		case oauthgrant.FieldClientID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field client_id", values[i])
			} else if value.Valid {
				og.ClientID = int(value.Int64)
			}
		case oauthgrant.FieldScopes:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field scopes", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &og.Scopes); err != nil {
					return fmt.Errorf("unmarshal field scopes: %w", err)
				}
			}
		case oauthgrant.FieldLastUsedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field last_used_at", values[i])
			} else if value.Valid {
				og.LastUsedAt = new(time.Time)
				*og.LastUsedAt = value.Time
			}
		default:
			og.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryUser queries the "user" edge of the OAuthGrant entity.
func (og *OAuthGrant) QueryUser() *UserQuery {
	return NewOAuthGrantClient(og.config).QueryUser(og)
}

// QueryClient queries the "client" edge of the OAuthGrant entity.
func (og *OAuthGrant) QueryClient() *OAuthClientQuery {
	return NewOAuthGrantClient(og.config).QueryClient(og)
}

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

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

// String implements the fmt.Stringer.
func (og *OAuthGrant) String() string {
	var builder strings.Builder
	builder.WriteString("OAuthGrant(")
	builder.WriteString(fmt.Sprintf("id=%v, ", og.ID))
	builder.WriteString("created_at=")
	builder.WriteString(og.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(og.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := og.DeletedAt; v != nil {
		builder.WriteString("deleted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("user_id=")
	builder.WriteString(fmt.Sprintf("%v", og.UserID))
	builder.WriteString(", ")
	builder.WriteString("client_id=")
	builder.WriteString(fmt.Sprintf("%v", og.ClientID))
	builder.WriteString(", ")
	builder.WriteString("scopes=")
	builder.WriteString(fmt.Sprintf("%v", og.Scopes))
	builder.WriteString(", ")
	if v := og.LastUsedAt; v != nil {
		builder.WriteString("last_used_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteByte(')')
	return builder.String()
}

// SetUser manually set the edge as loaded state.
func (e *OAuthGrant) SetUser(v *User) {
	e.Edges.User = v
	e.Edges.loadedTypes[0] = true
}

// SetClient manually set the edge as loaded state.
func (e *OAuthGrant) SetClient(v *OAuthClient) {
	e.Edges.Client = v
	e.Edges.loadedTypes[1] = true
}

// OAuthGrants is a parsable slice of OAuthGrant.
type OAuthGrants []*OAuthGrant
