// 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/file"
	"github.com/cloudreve/Cloudreve/v4/ent/share"
	"github.com/cloudreve/Cloudreve/v4/ent/user"
	"github.com/cloudreve/Cloudreve/v4/inventory/types"
)

// Share is the model entity for the Share schema.
type Share 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"`
	// Password holds the value of the "password" field.
	Password string `json:"password,omitempty"`
	// Views holds the value of the "views" field.
	Views int `json:"views,omitempty"`
	// Downloads holds the value of the "downloads" field.
	Downloads int `json:"downloads,omitempty"`
	// Expires holds the value of the "expires" field.
	Expires *time.Time `json:"expires,omitempty"`
	// RemainDownloads holds the value of the "remain_downloads" field.
	RemainDownloads *int `json:"remain_downloads,omitempty"`
	// Props holds the value of the "props" field.
	Props *types.ShareProps `json:"props,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the ShareQuery when eager-loading is set.
	Edges        ShareEdges `json:"edges"`
	file_shares  *int
	user_shares  *int
	selectValues sql.SelectValues
}

// ShareEdges holds the relations/edges for other nodes in the graph.
type ShareEdges struct {
	// User holds the value of the user edge.
	User *User `json:"user,omitempty"`
	// File holds the value of the file edge.
	File *File `json:"file,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 ShareEdges) 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"}
}

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

// scanValues returns the types for scanning values from sql.Rows.
func (*Share) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case share.FieldProps:
			values[i] = new([]byte)
		case share.FieldID, share.FieldViews, share.FieldDownloads, share.FieldRemainDownloads:
			values[i] = new(sql.NullInt64)
		case share.FieldPassword:
			values[i] = new(sql.NullString)
		case share.FieldCreatedAt, share.FieldUpdatedAt, share.FieldDeletedAt, share.FieldExpires:
			values[i] = new(sql.NullTime)
		case share.ForeignKeys[0]: // file_shares
			values[i] = new(sql.NullInt64)
		case share.ForeignKeys[1]: // user_shares
			values[i] = new(sql.NullInt64)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Share fields.
func (s *Share) 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 share.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			s.ID = int(value.Int64)
		case share.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 {
				s.CreatedAt = value.Time
			}
		case share.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 {
				s.UpdatedAt = value.Time
			}
		case share.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 {
				s.DeletedAt = new(time.Time)
				*s.DeletedAt = value.Time
			}
		case share.FieldPassword:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field password", values[i])
			} else if value.Valid {
				s.Password = value.String
			}
		case share.FieldViews:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field views", values[i])
			} else if value.Valid {
				s.Views = int(value.Int64)
			}
		case share.FieldDownloads:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field downloads", values[i])
			} else if value.Valid {
				s.Downloads = int(value.Int64)
			}
		case share.FieldExpires:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field expires", values[i])
			} else if value.Valid {
				s.Expires = new(time.Time)
				*s.Expires = value.Time
			}
		case share.FieldRemainDownloads:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field remain_downloads", values[i])
			} else if value.Valid {
				s.RemainDownloads = new(int)
				*s.RemainDownloads = int(value.Int64)
			}
		case share.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, &s.Props); err != nil {
					return fmt.Errorf("unmarshal field props: %w", err)
				}
			}
		case share.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field file_shares", value)
			} else if value.Valid {
				s.file_shares = new(int)
				*s.file_shares = int(value.Int64)
			}
		case share.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field user_shares", value)
			} else if value.Valid {
				s.user_shares = new(int)
				*s.user_shares = int(value.Int64)
			}
		default:
			s.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryUser queries the "user" edge of the Share entity.
func (s *Share) QueryUser() *UserQuery {
	return NewShareClient(s.config).QueryUser(s)
}

// QueryFile queries the "file" edge of the Share entity.
func (s *Share) QueryFile() *FileQuery {
	return NewShareClient(s.config).QueryFile(s)
}

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

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

// String implements the fmt.Stringer.
func (s *Share) String() string {
	var builder strings.Builder
	builder.WriteString("Share(")
	builder.WriteString(fmt.Sprintf("id=%v, ", s.ID))
	builder.WriteString("created_at=")
	builder.WriteString(s.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(s.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	if v := s.DeletedAt; v != nil {
		builder.WriteString("deleted_at=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("password=")
	builder.WriteString(s.Password)
	builder.WriteString(", ")
	builder.WriteString("views=")
	builder.WriteString(fmt.Sprintf("%v", s.Views))
	builder.WriteString(", ")
	builder.WriteString("downloads=")
	builder.WriteString(fmt.Sprintf("%v", s.Downloads))
	builder.WriteString(", ")
	if v := s.Expires; v != nil {
		builder.WriteString("expires=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := s.RemainDownloads; v != nil {
		builder.WriteString("remain_downloads=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("props=")
	builder.WriteString(fmt.Sprintf("%v", s.Props))
	builder.WriteByte(')')
	return builder.String()
}

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

// SetFile manually set the edge as loaded state.
func (e *Share) SetFile(v *File) {
	e.Edges.File = v
	e.Edges.loadedTypes[1] = true
}

// Shares is a parsable slice of Share.
type Shares []*Share
