// 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/AvengeMedia/dankcalendar/core/ent/account"
	"github.com/AvengeMedia/dankcalendar/core/ent/calendar"
	"github.com/AvengeMedia/dankcalendar/core/internal/settings"
)

// Calendar is the model entity for the Calendar schema.
type Calendar struct {
	config `json:"-"`
	// ID of the ent.
	ID string `json:"id,omitempty"`
	// RemoteID holds the value of the "remote_id" field.
	RemoteID string `json:"remote_id,omitempty"`
	// Name holds the value of the "name" field.
	Name string `json:"name,omitempty"`
	// NameOverride holds the value of the "name_override" field.
	NameOverride string `json:"name_override,omitempty"`
	// Description holds the value of the "description" field.
	Description string `json:"description,omitempty"`
	// Color holds the value of the "color" field.
	Color string `json:"color,omitempty"`
	// TimeZone holds the value of the "time_zone" field.
	TimeZone string `json:"time_zone,omitempty"`
	// ReadOnly holds the value of the "read_only" field.
	ReadOnly bool `json:"read_only,omitempty"`
	// Hidden holds the value of the "hidden" field.
	Hidden bool `json:"hidden,omitempty"`
	// ReminderOverrides holds the value of the "reminder_overrides" field.
	ReminderOverrides *settings.ReminderOverride `json:"reminder_overrides,omitempty"`
	// SyncToken holds the value of the "sync_token" field.
	SyncToken string `json:"sync_token,omitempty"`
	// SupportedComponents holds the value of the "supported_components" field.
	SupportedComponents []string `json:"supported_components,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"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the CalendarQuery when eager-loading is set.
	Edges             CalendarEdges `json:"edges"`
	account_calendars *string
	selectValues      sql.SelectValues
}

// CalendarEdges holds the relations/edges for other nodes in the graph.
type CalendarEdges struct {
	// Account holds the value of the account edge.
	Account *Account `json:"account,omitempty"`
	// Events holds the value of the events edge.
	Events []*Event `json:"events,omitempty"`
	// Tasks holds the value of the tasks edge.
	Tasks []*Task `json:"tasks,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

// 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 CalendarEdges) AccountOrErr() (*Account, error) {
	if e.Account != nil {
		return e.Account, nil
	} else if e.loadedTypes[0] {
		return nil, &NotFoundError{label: account.Label}
	}
	return nil, &NotLoadedError{edge: "account"}
}

// EventsOrErr returns the Events value or an error if the edge
// was not loaded in eager-loading.
func (e CalendarEdges) EventsOrErr() ([]*Event, error) {
	if e.loadedTypes[1] {
		return e.Events, nil
	}
	return nil, &NotLoadedError{edge: "events"}
}

// TasksOrErr returns the Tasks value or an error if the edge
// was not loaded in eager-loading.
func (e CalendarEdges) TasksOrErr() ([]*Task, error) {
	if e.loadedTypes[2] {
		return e.Tasks, nil
	}
	return nil, &NotLoadedError{edge: "tasks"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Calendar) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case calendar.FieldReminderOverrides, calendar.FieldSupportedComponents:
			values[i] = new([]byte)
		case calendar.FieldReadOnly, calendar.FieldHidden:
			values[i] = new(sql.NullBool)
		case calendar.FieldID, calendar.FieldRemoteID, calendar.FieldName, calendar.FieldNameOverride, calendar.FieldDescription, calendar.FieldColor, calendar.FieldTimeZone, calendar.FieldSyncToken:
			values[i] = new(sql.NullString)
		case calendar.FieldCreatedAt, calendar.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		case calendar.ForeignKeys[0]: // account_calendars
			values[i] = new(sql.NullString)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Calendar fields.
func (_m *Calendar) 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 calendar.FieldID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value.Valid {
				_m.ID = value.String
			}
		case calendar.FieldRemoteID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field remote_id", values[i])
			} else if value.Valid {
				_m.RemoteID = value.String
			}
		case calendar.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 calendar.FieldNameOverride:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field name_override", values[i])
			} else if value.Valid {
				_m.NameOverride = value.String
			}
		case calendar.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 calendar.FieldColor:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field color", values[i])
			} else if value.Valid {
				_m.Color = value.String
			}
		case calendar.FieldTimeZone:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field time_zone", values[i])
			} else if value.Valid {
				_m.TimeZone = value.String
			}
		case calendar.FieldReadOnly:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field read_only", values[i])
			} else if value.Valid {
				_m.ReadOnly = value.Bool
			}
		case calendar.FieldHidden:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field hidden", values[i])
			} else if value.Valid {
				_m.Hidden = value.Bool
			}
		case calendar.FieldReminderOverrides:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field reminder_overrides", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.ReminderOverrides); err != nil {
					return fmt.Errorf("unmarshal field reminder_overrides: %w", err)
				}
			}
		case calendar.FieldSyncToken:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field sync_token", values[i])
			} else if value.Valid {
				_m.SyncToken = value.String
			}
		case calendar.FieldSupportedComponents:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field supported_components", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.SupportedComponents); err != nil {
					return fmt.Errorf("unmarshal field supported_components: %w", err)
				}
			}
		case calendar.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
			}
		case calendar.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 {
				_m.UpdatedAt = value.Time
			}
		case calendar.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field account_calendars", values[i])
			} else if value.Valid {
				_m.account_calendars = new(string)
				*_m.account_calendars = value.String
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

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

// QueryEvents queries the "events" edge of the Calendar entity.
func (_m *Calendar) QueryEvents() *EventQuery {
	return NewCalendarClient(_m.config).QueryEvents(_m)
}

// QueryTasks queries the "tasks" edge of the Calendar entity.
func (_m *Calendar) QueryTasks() *TaskQuery {
	return NewCalendarClient(_m.config).QueryTasks(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *Calendar) String() string {
	var builder strings.Builder
	builder.WriteString("Calendar(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("remote_id=")
	builder.WriteString(_m.RemoteID)
	builder.WriteString(", ")
	builder.WriteString("name=")
	builder.WriteString(_m.Name)
	builder.WriteString(", ")
	builder.WriteString("name_override=")
	builder.WriteString(_m.NameOverride)
	builder.WriteString(", ")
	builder.WriteString("description=")
	builder.WriteString(_m.Description)
	builder.WriteString(", ")
	builder.WriteString("color=")
	builder.WriteString(_m.Color)
	builder.WriteString(", ")
	builder.WriteString("time_zone=")
	builder.WriteString(_m.TimeZone)
	builder.WriteString(", ")
	builder.WriteString("read_only=")
	builder.WriteString(fmt.Sprintf("%v", _m.ReadOnly))
	builder.WriteString(", ")
	builder.WriteString("hidden=")
	builder.WriteString(fmt.Sprintf("%v", _m.Hidden))
	builder.WriteString(", ")
	builder.WriteString("reminder_overrides=")
	builder.WriteString(fmt.Sprintf("%v", _m.ReminderOverrides))
	builder.WriteString(", ")
	builder.WriteString("sync_token=")
	builder.WriteString(_m.SyncToken)
	builder.WriteString(", ")
	builder.WriteString("supported_components=")
	builder.WriteString(fmt.Sprintf("%v", _m.SupportedComponents))
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(_m.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(_m.UpdatedAt.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// Calendars is a parsable slice of Calendar.
type Calendars []*Calendar
