// 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/inventory/types"
)

// OAuthClient is the model entity for the OAuthClient schema.
type OAuthClient 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"`
	// GUID holds the value of the "guid" field.
	GUID string `json:"guid,omitempty"`
	// Secret holds the value of the "secret" field.
	Secret string `json:"-"`
	// Name holds the value of the "name" field.
	Name string `json:"name,omitempty"`
	// HomepageURL holds the value of the "homepage_url" field.
	HomepageURL string `json:"homepage_url,omitempty"`
	// RedirectUris holds the value of the "redirect_uris" field.
	RedirectUris []string `json:"redirect_uris,omitempty"`
	// Scopes holds the value of the "scopes" field.
	Scopes []string `json:"scopes,omitempty"`
	// Props holds the value of the "props" field.
	Props *types.OAuthClientProps `json:"props,omitempty"`
	// IsEnabled holds the value of the "is_enabled" field.
	IsEnabled bool `json:"is_enabled,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the OAuthClientQuery when eager-loading is set.
	Edges        OAuthClientEdges `json:"edges"`
	selectValues sql.SelectValues
}

// OAuthClientEdges holds the relations/edges for other nodes in the graph.
type OAuthClientEdges struct {
	// Grants holds the value of the grants edge.
	Grants []*OAuthGrant `json:"grants,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [1]bool
}

// GrantsOrErr returns the Grants value or an error if the edge
// was not loaded in eager-loading.
func (e OAuthClientEdges) GrantsOrErr() ([]*OAuthGrant, error) {
	if e.loadedTypes[0] {
		return e.Grants, nil
	}
	return nil, &NotLoadedError{edge: "grants"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*OAuthClient) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case oauthclient.FieldRedirectUris, oauthclient.FieldScopes, oauthclient.FieldProps:
			values[i] = new([]byte)
		case oauthclient.FieldIsEnabled:
			values[i] = new(sql.NullBool)
		case oauthclient.FieldID:
			values[i] = new(sql.NullInt64)
		case oauthclient.FieldGUID, oauthclient.FieldSecret, oauthclient.FieldName, oauthclient.FieldHomepageURL:
			values[i] = new(sql.NullString)
		case oauthclient.FieldCreatedAt, oauthclient.FieldUpdatedAt, oauthclient.FieldDeletedAt:
			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 OAuthClient fields.
func (oc *OAuthClient) 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 oauthclient.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			oc.ID = int(value.Int64)
		case oauthclient.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 {
				oc.CreatedAt = value.Time
			}
		case oauthclient.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 {
				oc.UpdatedAt = value.Time
			}
		case oauthclient.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 {
				oc.DeletedAt = new(time.Time)
				*oc.DeletedAt = value.Time
			}
		case oauthclient.FieldGUID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field guid", values[i])
			} else if value.Valid {
				oc.GUID = value.String
			}
		case oauthclient.FieldSecret:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field secret", values[i])
			} else if value.Valid {
				oc.Secret = value.String
			}
		case oauthclient.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				oc.Name = value.String
			}
		case oauthclient.FieldHomepageURL:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field homepage_url", values[i])
			} else if value.Valid {
				oc.HomepageURL = value.String
			}
		case oauthclient.FieldRedirectUris:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field redirect_uris", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &oc.RedirectUris); err != nil {
					return fmt.Errorf("unmarshal field redirect_uris: %w", err)
				}
			}
		case oauthclient.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, &oc.Scopes); err != nil {
					return fmt.Errorf("unmarshal field scopes: %w", err)
				}
			}
		case oauthclient.FieldProps:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field props", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &oc.Props); err != nil {
					return fmt.Errorf("unmarshal field props: %w", err)
				}
			}
		case oauthclient.FieldIsEnabled:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field is_enabled", values[i])
			} else if value.Valid {
				oc.IsEnabled = value.Bool
			}
		default:
			oc.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryGrants queries the "grants" edge of the OAuthClient entity.
func (oc *OAuthClient) QueryGrants() *OAuthGrantQuery {
	return NewOAuthClientClient(oc.config).QueryGrants(oc)
}

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

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

// String implements the fmt.Stringer.
func (oc *OAuthClient) String() string {
	var builder strings.Builder
	builder.WriteString("OAuthClient(")
	builder.WriteString(fmt.Sprintf("id=%v, ", oc.ID))
	builder.WriteString("created_at=")
	builder.WriteString(oc.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(oc.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := oc.DeletedAt; v != nil {
		builder.WriteString("deleted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("guid=")
	builder.WriteString(oc.GUID)
	builder.WriteString(", ")
	builder.WriteString("secret=<sensitive>")
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(oc.Name)
	builder.WriteString(", ")
	builder.WriteString("homepage_url=")
	builder.WriteString(oc.HomepageURL)
	builder.WriteString(", ")
	builder.WriteString("redirect_uris=")
	builder.WriteString(fmt.Sprintf("%v", oc.RedirectUris))
	builder.WriteString(", ")
	builder.WriteString("scopes=")
	builder.WriteString(fmt.Sprintf("%v", oc.Scopes))
	builder.WriteString(", ")
	builder.WriteString("props=")
	builder.WriteString(fmt.Sprintf("%v", oc.Props))
	builder.WriteString(", ")
	builder.WriteString("is_enabled=")
	builder.WriteString(fmt.Sprintf("%v", oc.IsEnabled))
	builder.WriteByte(')')
	return builder.String()
}

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

// OAuthClients is a parsable slice of OAuthClient.
type OAuthClients []*OAuthClient
