// 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/channelmonitor"
	"github.com/Wei-Shaw/sub2api/ent/channelmonitorhistory"
)

// ChannelMonitorHistory is the model entity for the ChannelMonitorHistory schema.
type ChannelMonitorHistory struct {
	config `json:"-"`
	// ID of the ent.
	ID int64 `json:"id,omitempty"`
	// MonitorID holds the value of the "monitor_id" field.
	MonitorID int64 `json:"monitor_id,omitempty"`
	// Model holds the value of the "model" field.
	Model string `json:"model,omitempty"`
	// Status holds the value of the "status" field.
	Status channelmonitorhistory.Status `json:"status,omitempty"`
	// LatencyMs holds the value of the "latency_ms" field.
	LatencyMs *int `json:"latency_ms,omitempty"`
	// PingLatencyMs holds the value of the "ping_latency_ms" field.
	PingLatencyMs *int `json:"ping_latency_ms,omitempty"`
	// Message holds the value of the "message" field.
	Message string `json:"message,omitempty"`
	// CheckedAt holds the value of the "checked_at" field.
	CheckedAt time.Time `json:"checked_at,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the ChannelMonitorHistoryQuery when eager-loading is set.
	Edges        ChannelMonitorHistoryEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

// MonitorOrErr returns the Monitor value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e ChannelMonitorHistoryEdges) MonitorOrErr() (*ChannelMonitor, error) {
	if e.Monitor != nil {
		return e.Monitor, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: channelmonitor.Label}
	}
	return nil, &NotLoadedError{edge: "monitor"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*ChannelMonitorHistory) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case channelmonitorhistory.FieldID, channelmonitorhistory.FieldMonitorID, channelmonitorhistory.FieldLatencyMs, channelmonitorhistory.FieldPingLatencyMs:
			values[i] = new(sql.NullInt64)
		case channelmonitorhistory.FieldModel, channelmonitorhistory.FieldStatus, channelmonitorhistory.FieldMessage:
			values[i] = new(sql.NullString)
		case channelmonitorhistory.FieldCheckedAt:
			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 ChannelMonitorHistory fields.
func (_m *ChannelMonitorHistory) 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 channelmonitorhistory.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 channelmonitorhistory.FieldMonitorID:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field monitor_id", values[i])
			} else if value.Valid {
				_m.MonitorID = value.Int64
			}
		case channelmonitorhistory.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 channelmonitorhistory.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 = channelmonitorhistory.Status(value.String)
			}
		case channelmonitorhistory.FieldLatencyMs:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field latency_ms", values[i])
			} else if value.Valid {
				_m.LatencyMs = new(int)
				*_m.LatencyMs = int(value.Int64)
			}
		case channelmonitorhistory.FieldPingLatencyMs:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field ping_latency_ms", values[i])
			} else if value.Valid {
				_m.PingLatencyMs = new(int)
				*_m.PingLatencyMs = int(value.Int64)
			}
		case channelmonitorhistory.FieldMessage:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field message", values[i])
			} else if value.Valid {
				_m.Message = value.String
			}
		case channelmonitorhistory.FieldCheckedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field checked_at", values[i])
			} else if value.Valid {
				_m.CheckedAt = 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 ChannelMonitorHistory.
// This includes values selected through modifiers, order, etc.
func (_m *ChannelMonitorHistory) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryMonitor queries the "monitor" edge of the ChannelMonitorHistory entity.
func (_m *ChannelMonitorHistory) QueryMonitor() *ChannelMonitorQuery {
	return NewChannelMonitorHistoryClient(_m.config).QueryMonitor(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *ChannelMonitorHistory) String() string {
	var builder strings.Builder
	builder.WriteString("ChannelMonitorHistory(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("monitor_id=")
	builder.WriteString(fmt.Sprintf("%v", _m.MonitorID))
	builder.WriteString(", ")
	builder.WriteString("model=")
	builder.WriteString(_m.Model)
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", _m.Status))
	builder.WriteString(", ")
	if v := _m.LatencyMs; v != nil {
		builder.WriteString("latency_ms=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	if v := _m.PingLatencyMs; v != nil {
		builder.WriteString("ping_latency_ms=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteString(", ")
	builder.WriteString("message=")
	builder.WriteString(_m.Message)
	builder.WriteString(", ")
	builder.WriteString("checked_at=")
	builder.WriteString(_m.CheckedAt.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// ChannelMonitorHistories is a parsable slice of ChannelMonitorHistory.
type ChannelMonitorHistories []*ChannelMonitorHistory
