// 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/account"
	"github.com/Wei-Shaw/sub2api/ent/apikey"
	"github.com/Wei-Shaw/sub2api/ent/group"
	"github.com/Wei-Shaw/sub2api/ent/usagelog"
	"github.com/Wei-Shaw/sub2api/ent/user"
	"github.com/Wei-Shaw/sub2api/ent/usersubscription"
)

// UsageLog is the model entity for the UsageLog schema.
type UsageLog struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// UserID holds the value of the "user_id" field.
	UserID int64 `json:"user_id,omitempty"`
	// APIKeyID holds the value of the "api_key_id" field.
	APIKeyID int64 `json:"api_key_id,omitempty"`
	// AccountID holds the value of the "account_id" field.
	AccountID int64 `json:"account_id,omitempty"`
	// RequestID holds the value of the "request_id" field.
	RequestID string `json:"request_id,omitempty"`
	// Model holds the value of the "model" field.
	Model string `json:"model,omitempty"`
	// GroupID holds the value of the "group_id" field.
	GroupID *int64 `json:"group_id,omitempty"`
	// SubscriptionID holds the value of the "subscription_id" field.
	SubscriptionID *int64 `json:"subscription_id,omitempty"`
	// InputTokens holds the value of the "input_tokens" field.
	InputTokens int `json:"input_tokens,omitempty"`
	// OutputTokens holds the value of the "output_tokens" field.
	OutputTokens int `json:"output_tokens,omitempty"`
	// CacheCreationTokens holds the value of the "cache_creation_tokens" field.
	CacheCreationTokens int `json:"cache_creation_tokens,omitempty"`
	// CacheReadTokens holds the value of the "cache_read_tokens" field.
	CacheReadTokens int `json:"cache_read_tokens,omitempty"`
	// CacheCreation5mTokens holds the value of the "cache_creation_5m_tokens" field.
	CacheCreation5mTokens int `json:"cache_creation_5m_tokens,omitempty"`
	// CacheCreation1hTokens holds the value of the "cache_creation_1h_tokens" field.
	CacheCreation1hTokens int `json:"cache_creation_1h_tokens,omitempty"`
	// InputCost holds the value of the "input_cost" field.
	InputCost float64 `json:"input_cost,omitempty"`
	// OutputCost holds the value of the "output_cost" field.
	OutputCost float64 `json:"output_cost,omitempty"`
	// CacheCreationCost holds the value of the "cache_creation_cost" field.
	CacheCreationCost float64 `json:"cache_creation_cost,omitempty"`
	// CacheReadCost holds the value of the "cache_read_cost" field.
	CacheReadCost float64 `json:"cache_read_cost,omitempty"`
	// TotalCost holds the value of the "total_cost" field.
	TotalCost float64 `json:"total_cost,omitempty"`
	// ActualCost holds the value of the "actual_cost" field.
	ActualCost float64 `json:"actual_cost,omitempty"`
	// RateMultiplier holds the value of the "rate_multiplier" field.
	RateMultiplier float64 `json:"rate_multiplier,omitempty"`
	// BillingType holds the value of the "billing_type" field.
	BillingType int8 `json:"billing_type,omitempty"`
	// Stream holds the value of the "stream" field.
	Stream bool `json:"stream,omitempty"`
	// DurationMs holds the value of the "duration_ms" field.
	DurationMs *int `json:"duration_ms,omitempty"`
	// FirstTokenMs holds the value of the "first_token_ms" field.
	FirstTokenMs *int `json:"first_token_ms,omitempty"`
	// ImageCount holds the value of the "image_count" field.
	ImageCount int `json:"image_count,omitempty"`
	// ImageSize holds the value of the "image_size" field.
	ImageSize *string `json:"image_size,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the UsageLogQuery when eager-loading is set.
	Edges        UsageLogEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

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

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

// 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 UsageLogEdges) GroupOrErr() (*Group, error) {
	if e.Group != nil {
		return e.Group, nil
	} else if e.loadedTypes[3] {
		return nil, &NotFoundError{label: group.Label}
	}
	return nil, &NotLoadedError{edge: "group"}
}

// SubscriptionOrErr returns the Subscription value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e UsageLogEdges) SubscriptionOrErr() (*UserSubscription, error) {
	if e.Subscription != nil {
		return e.Subscription, nil
	} else if e.loadedTypes[4] {
		return nil, &NotFoundError{label: usersubscription.Label}
	}
	return nil, &NotLoadedError{edge: "subscription"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*UsageLog) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case usagelog.FieldStream:
			values[i] = new(sql.NullBool)
		case usagelog.FieldInputCost, usagelog.FieldOutputCost, usagelog.FieldCacheCreationCost, usagelog.FieldCacheReadCost, usagelog.FieldTotalCost, usagelog.FieldActualCost, usagelog.FieldRateMultiplier:
			values[i] = new(sql.NullFloat64)
		case usagelog.FieldID, usagelog.FieldUserID, usagelog.FieldAPIKeyID, usagelog.FieldAccountID, usagelog.FieldGroupID, usagelog.FieldSubscriptionID, usagelog.FieldInputTokens, usagelog.FieldOutputTokens, usagelog.FieldCacheCreationTokens, usagelog.FieldCacheReadTokens, usagelog.FieldCacheCreation5mTokens, usagelog.FieldCacheCreation1hTokens, usagelog.FieldBillingType, usagelog.FieldDurationMs, usagelog.FieldFirstTokenMs, usagelog.FieldImageCount:
			values[i] = new(sql.NullInt64)
		case usagelog.FieldRequestID, usagelog.FieldModel, usagelog.FieldImageSize:
			values[i] = new(sql.NullString)
		case usagelog.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 UsageLog fields.
func (_m *UsageLog) 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 usagelog.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 usagelog.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 {
				_m.UserID = value.Int64
			}
		case usagelog.FieldAPIKeyID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field api_key_id", values[i])
			} else if value.Valid {
				_m.APIKeyID = value.Int64
			}
		case usagelog.FieldAccountID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field account_id", values[i])
			} else if value.Valid {
				_m.AccountID = value.Int64
			}
		case usagelog.FieldRequestID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field request_id", values[i])
			} else if value.Valid {
				_m.RequestID = value.String
			}
		case usagelog.FieldModel:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field model", values[i])
			} else if value.Valid {
				_m.Model = value.String
			}
		case usagelog.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 usagelog.FieldSubscriptionID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field subscription_id", values[i])
			} else if value.Valid {
				_m.SubscriptionID = new(int64)
				*_m.SubscriptionID = value.Int64
			}
		case usagelog.FieldInputTokens:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field input_tokens", values[i])
			} else if value.Valid {
				_m.InputTokens = int(value.Int64)
			}
		case usagelog.FieldOutputTokens:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field output_tokens", values[i])
			} else if value.Valid {
				_m.OutputTokens = int(value.Int64)
			}
		case usagelog.FieldCacheCreationTokens:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field cache_creation_tokens", values[i])
			} else if value.Valid {
				_m.CacheCreationTokens = int(value.Int64)
			}
		case usagelog.FieldCacheReadTokens:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field cache_read_tokens", values[i])
			} else if value.Valid {
				_m.CacheReadTokens = int(value.Int64)
			}
		case usagelog.FieldCacheCreation5mTokens:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field cache_creation_5m_tokens", values[i])
			} else if value.Valid {
				_m.CacheCreation5mTokens = int(value.Int64)
			}
		case usagelog.FieldCacheCreation1hTokens:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field cache_creation_1h_tokens", values[i])
			} else if value.Valid {
				_m.CacheCreation1hTokens = int(value.Int64)
			}
		case usagelog.FieldInputCost:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field input_cost", values[i])
			} else if value.Valid {
				_m.InputCost = value.Float64
			}
		case usagelog.FieldOutputCost:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field output_cost", values[i])
			} else if value.Valid {
				_m.OutputCost = value.Float64
			}
		case usagelog.FieldCacheCreationCost:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field cache_creation_cost", values[i])
			} else if value.Valid {
				_m.CacheCreationCost = value.Float64
			}
		case usagelog.FieldCacheReadCost:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field cache_read_cost", values[i])
			} else if value.Valid {
				_m.CacheReadCost = value.Float64
			}
		case usagelog.FieldTotalCost:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field total_cost", values[i])
			} else if value.Valid {
				_m.TotalCost = value.Float64
			}
		case usagelog.FieldActualCost:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field actual_cost", values[i])
			} else if value.Valid {
				_m.ActualCost = value.Float64
			}
		case usagelog.FieldRateMultiplier:
			if value, ok := values[i].(*sql.NullFloat64); !ok {
				return fmt.Errorf("unexpected type %T for field rate_multiplier", values[i])
			} else if value.Valid {
				_m.RateMultiplier = value.Float64
			}
		case usagelog.FieldBillingType:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field billing_type", values[i])
			} else if value.Valid {
				_m.BillingType = int8(value.Int64)
			}
		case usagelog.FieldStream:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field stream", values[i])
			} else if value.Valid {
				_m.Stream = value.Bool
			}
		case usagelog.FieldDurationMs:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field duration_ms", values[i])
			} else if value.Valid {
				_m.DurationMs = new(int)
				*_m.DurationMs = int(value.Int64)
			}
		case usagelog.FieldFirstTokenMs:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field first_token_ms", values[i])
			} else if value.Valid {
				_m.FirstTokenMs = new(int)
				*_m.FirstTokenMs = int(value.Int64)
			}
		case usagelog.FieldImageCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field image_count", values[i])
			} else if value.Valid {
				_m.ImageCount = int(value.Int64)
			}
		case usagelog.FieldImageSize:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field image_size", values[i])
			} else if value.Valid {
				_m.ImageSize = new(string)
				*_m.ImageSize = value.String
			}
		case usagelog.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
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

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

// QueryAPIKey queries the "api_key" edge of the UsageLog entity.
func (_m *UsageLog) QueryAPIKey() *APIKeyQuery {
	return NewUsageLogClient(_m.config).QueryAPIKey(_m)
}

// QueryAccount queries the "account" edge of the UsageLog entity.
func (_m *UsageLog) QueryAccount() *AccountQuery {
	return NewUsageLogClient(_m.config).QueryAccount(_m)
}

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

// QuerySubscription queries the "subscription" edge of the UsageLog entity.
func (_m *UsageLog) QuerySubscription() *UserSubscriptionQuery {
	return NewUsageLogClient(_m.config).QuerySubscription(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *UsageLog) String() string {
	var builder strings.Builder
	builder.WriteString("UsageLog(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("user_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.UserID))
	builder.WriteString(", ")
	builder.WriteString("api_key_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.APIKeyID))
	builder.WriteString(", ")
	builder.WriteString("account_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.AccountID))
	builder.WriteString(", ")
	builder.WriteString("request_id=")
	builder.WriteString(_m.RequestID)
	builder.WriteString(", ")
	builder.WriteString("model=")
	builder.WriteString(_m.Model)
	builder.WriteString(", ")
	if v := _m.GroupID; v != nil {
		builder.WriteString("group_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	if v := _m.SubscriptionID; v != nil {
		builder.WriteString("subscription_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("input_tokens=")
	builder.WriteString(fmt.Sprintf("%v", _m.InputTokens))
	builder.WriteString(", ")
	builder.WriteString("output_tokens=")
	builder.WriteString(fmt.Sprintf("%v", _m.OutputTokens))
	builder.WriteString(", ")
	builder.WriteString("cache_creation_tokens=")
	builder.WriteString(fmt.Sprintf("%v", _m.CacheCreationTokens))
	builder.WriteString(", ")
	builder.WriteString("cache_read_tokens=")
	builder.WriteString(fmt.Sprintf("%v", _m.CacheReadTokens))
	builder.WriteString(", ")
	builder.WriteString("cache_creation_5m_tokens=")
	builder.WriteString(fmt.Sprintf("%v", _m.CacheCreation5mTokens))
	builder.WriteString(", ")
	builder.WriteString("cache_creation_1h_tokens=")
	builder.WriteString(fmt.Sprintf("%v", _m.CacheCreation1hTokens))
	builder.WriteString(", ")
	builder.WriteString("input_cost=")
	builder.WriteString(fmt.Sprintf("%v", _m.InputCost))
	builder.WriteString(", ")
	builder.WriteString("output_cost=")
	builder.WriteString(fmt.Sprintf("%v", _m.OutputCost))
	builder.WriteString(", ")
	builder.WriteString("cache_creation_cost=")
	builder.WriteString(fmt.Sprintf("%v", _m.CacheCreationCost))
	builder.WriteString(", ")
	builder.WriteString("cache_read_cost=")
	builder.WriteString(fmt.Sprintf("%v", _m.CacheReadCost))
	builder.WriteString(", ")
	builder.WriteString("total_cost=")
	builder.WriteString(fmt.Sprintf("%v", _m.TotalCost))
	builder.WriteString(", ")
	builder.WriteString("actual_cost=")
	builder.WriteString(fmt.Sprintf("%v", _m.ActualCost))
	builder.WriteString(", ")
	builder.WriteString("rate_multiplier=")
	builder.WriteString(fmt.Sprintf("%v", _m.RateMultiplier))
	builder.WriteString(", ")
	builder.WriteString("billing_type=")
	builder.WriteString(fmt.Sprintf("%v", _m.BillingType))
	builder.WriteString(", ")
	builder.WriteString("stream=")
	builder.WriteString(fmt.Sprintf("%v", _m.Stream))
	builder.WriteString(", ")
	if v := _m.DurationMs; v != nil {
		builder.WriteString("duration_ms=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	if v := _m.FirstTokenMs; v != nil {
		builder.WriteString("first_token_ms=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("image_count=")
	builder.WriteString(fmt.Sprintf("%v", _m.ImageCount))
	builder.WriteString(", ")
	if v := _m.ImageSize; v != nil {
		builder.WriteString("image_size=")
		builder.WriteString(*v)
	}
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// UsageLogs is a parsable slice of UsageLog.
type UsageLogs []*UsageLog
