// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/anzhiyu-c/anheyu-app/ent/link"
	"github.com/anzhiyu-c/anheyu-app/ent/linkcategory"
)

// Link is the model entity for the Link schema.
type Link struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// 网站名称
	Name string `json:"name,omitempty"`
	// 网站链接
	URL string `json:"url,omitempty"`
	// 网站头像/Logo
	Logo string `json:"logo,omitempty"`
	// 网站介绍
	Description string `json:"description,omitempty"`
	// 友链状态
	Status link.Status `json:"status,omitempty"`
	// 网站快照的 URL
	Siteshot string `json:"siteshot,omitempty"`
	// 排序权重，数字越小越靠前
	SortOrder int `json:"sort_order,omitempty"`
	// 是否跳过健康检查
	SkipHealthCheck bool `json:"skip_health_check,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the LinkQuery when eager-loading is set.
	Edges               LinkEdges `json:"edges"`
	link_category_links *int
	selectValues        sql.SelectValues
}

// LinkEdges holds the relations/edges for other nodes in the graph.
type LinkEdges struct {
	// Category holds the value of the category edge.
	Category *LinkCategory `json:"category,omitempty"`
	// Tags holds the value of the tags edge.
	Tags []*LinkTag `json:"tags,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [2]bool
}

// CategoryOrErr returns the Category value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e LinkEdges) CategoryOrErr() (*LinkCategory, error) {
	if e.Category != nil {
		return e.Category, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: linkcategory.Label}
	}
	return nil, &NotLoadedError{edge: "category"}
}

// TagsOrErr returns the Tags value or an error if the edge
// was not loaded in eager-loading.
func (e LinkEdges) TagsOrErr() ([]*LinkTag, error) {
	if e.loadedTypes[1] {
		return e.Tags, nil
	}
	return nil, &NotLoadedError{edge: "tags"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Link) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case link.FieldSkipHealthCheck:
			values[i] = new(sql.NullBool)
		case link.FieldID, link.FieldSortOrder:
			values[i] = new(sql.NullInt64)
		case link.FieldName, link.FieldURL, link.FieldLogo, link.FieldDescription, link.FieldStatus, link.FieldSiteshot:
			values[i] = new(sql.NullString)
		case link.ForeignKeys[0]: // link_category_links
			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 Link fields.
func (_m *Link) 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 link.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			_m.ID = int(value.Int64)
		case link.FieldName:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name", values[i])
			} else if value.Valid {
				_m.Name = value.String
			}
		case link.FieldURL:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field url", values[i])
			} else if value.Valid {
				_m.URL = value.String
			}
		case link.FieldLogo:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field logo", values[i])
			} else if value.Valid {
				_m.Logo = value.String
			}
		case link.FieldDescription:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field description", values[i])
			} else if value.Valid {
				_m.Description = value.String
			}
		case link.FieldStatus:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field status", values[i])
			} else if value.Valid {
				_m.Status = link.Status(value.String)
			}
		case link.FieldSiteshot:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field siteshot", values[i])
			} else if value.Valid {
				_m.Siteshot = value.String
			}
		case link.FieldSortOrder:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field sort_order", values[i])
			} else if value.Valid {
				_m.SortOrder = int(value.Int64)
			}
		case link.FieldSkipHealthCheck:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field skip_health_check", values[i])
			} else if value.Valid {
				_m.SkipHealthCheck = value.Bool
			}
		case link.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field link_category_links", value)
			} else if value.Valid {
				_m.link_category_links = new(int)
				*_m.link_category_links = int(value.Int64)
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryCategory queries the "category" edge of the Link entity.
func (_m *Link) QueryCategory() *LinkCategoryQuery {
	return NewLinkClient(_m.config).QueryCategory(_m)
}

// QueryTags queries the "tags" edge of the Link entity.
func (_m *Link) QueryTags() *LinkTagQuery {
	return NewLinkClient(_m.config).QueryTags(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *Link) String() string {
	var builder strings.Builder
	builder.WriteString("Link(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("name=")
	builder.WriteString(_m.Name)
	builder.WriteString(", ")
	builder.WriteString("url=")
	builder.WriteString(_m.URL)
	builder.WriteString(", ")
	builder.WriteString("logo=")
	builder.WriteString(_m.Logo)
	builder.WriteString(", ")
	builder.WriteString("description=")
	builder.WriteString(_m.Description)
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", _m.Status))
	builder.WriteString(", ")
	builder.WriteString("siteshot=")
	builder.WriteString(_m.Siteshot)
	builder.WriteString(", ")
	builder.WriteString("sort_order=")
	builder.WriteString(fmt.Sprintf("%v", _m.SortOrder))
	builder.WriteString(", ")
	builder.WriteString("skip_health_check=")
	builder.WriteString(fmt.Sprintf("%v", _m.SkipHealthCheck))
	builder.WriteByte(')')
	return builder.String()
}

// Links is a parsable slice of Link.
type Links []*Link
