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

// Task is the model entity for the Task schema.
type Task struct {
	config `json:"-"`
	// ID of the ent.
	ID string `json:"id,omitempty"`
	// UID holds the value of the "uid" field.
	UID string `json:"uid,omitempty"`
	// RemoteID holds the value of the "remote_id" field.
	RemoteID string `json:"remote_id,omitempty"`
	// Etag holds the value of the "etag" field.
	Etag string `json:"etag,omitempty"`
	// Summary holds the value of the "summary" field.
	Summary string `json:"summary,omitempty"`
	// Description holds the value of the "description" field.
	Description string `json:"description,omitempty"`
	// Location holds the value of the "location" field.
	Location string `json:"location,omitempty"`
	// Status holds the value of the "status" field.
	Status task.Status `json:"status,omitempty"`
	// Priority holds the value of the "priority" field.
	Priority int `json:"priority,omitempty"`
	// PercentComplete holds the value of the "percent_complete" field.
	PercentComplete int `json:"percent_complete,omitempty"`
	// Due holds the value of the "due" field.
	Due *time.Time `json:"due,omitempty"`
	// Start holds the value of the "start" field.
	Start *time.Time `json:"start,omitempty"`
	// Completed holds the value of the "completed" field.
	Completed *time.Time `json:"completed,omitempty"`
	// AllDay holds the value of the "all_day" field.
	AllDay bool `json:"all_day,omitempty"`
	// DueTz holds the value of the "due_tz" field.
	DueTz string `json:"due_tz,omitempty"`
	// StartTz holds the value of the "start_tz" field.
	StartTz string `json:"start_tz,omitempty"`
	// ParentUID holds the value of the "parent_uid" field.
	ParentUID string `json:"parent_uid,omitempty"`
	// Recurrence holds the value of the "recurrence" field.
	Recurrence map[string]interface{} `json:"recurrence,omitempty"`
	// Reminders holds the value of the "reminders" field.
	Reminders []map[string]interface{} `json:"reminders,omitempty"`
	// Categories holds the value of the "categories" field.
	Categories []string `json:"categories,omitempty"`
	// RawIcs holds the value of the "raw_ics" field.
	RawIcs string `json:"raw_ics,omitempty"`
	// Created holds the value of the "created" field.
	Created time.Time `json:"created,omitempty"`
	// Updated holds the value of the "updated" field.
	Updated time.Time `json:"updated,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the TaskQuery when eager-loading is set.
	Edges          TaskEdges `json:"edges"`
	calendar_tasks *string
	selectValues   sql.SelectValues
}

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*Task) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case task.FieldRecurrence, task.FieldReminders, task.FieldCategories:
			values[i] = new([]byte)
		case task.FieldAllDay:
			values[i] = new(sql.NullBool)
		case task.FieldPriority, task.FieldPercentComplete:
			values[i] = new(sql.NullInt64)
		case task.FieldID, task.FieldUID, task.FieldRemoteID, task.FieldEtag, task.FieldSummary, task.FieldDescription, task.FieldLocation, task.FieldStatus, task.FieldDueTz, task.FieldStartTz, task.FieldParentUID, task.FieldRawIcs:
			values[i] = new(sql.NullString)
		case task.FieldDue, task.FieldStart, task.FieldCompleted, task.FieldCreated, task.FieldUpdated:
			values[i] = new(sql.NullTime)
		case task.ForeignKeys[0]: // calendar_tasks
			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 Task fields.
func (_m *Task) 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 task.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 task.FieldUID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field uid", values[i])
			} else if value.Valid {
				_m.UID = value.String
			}
		case task.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 task.FieldEtag:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field etag", values[i])
			} else if value.Valid {
				_m.Etag = value.String
			}
		case task.FieldSummary:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field summary", values[i])
			} else if value.Valid {
				_m.Summary = value.String
			}
		case task.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 task.FieldLocation:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field location", values[i])
			} else if value.Valid {
				_m.Location = value.String
			}
		case task.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 = task.Status(value.String)
			}
		case task.FieldPriority:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field priority", values[i])
			} else if value.Valid {
				_m.Priority = int(value.Int64)
			}
		case task.FieldPercentComplete:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field percent_complete", values[i])
			} else if value.Valid {
				_m.PercentComplete = int(value.Int64)
			}
		case task.FieldDue:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field due", values[i])
			} else if value.Valid {
				_m.Due = new(time.Time)
				*_m.Due = value.Time
			}
		case task.FieldStart:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field start", values[i])
			} else if value.Valid {
				_m.Start = new(time.Time)
				*_m.Start = value.Time
			}
		case task.FieldCompleted:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field completed", values[i])
			} else if value.Valid {
				_m.Completed = new(time.Time)
				*_m.Completed = value.Time
			}
		case task.FieldAllDay:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field all_day", values[i])
			} else if value.Valid {
				_m.AllDay = value.Bool
			}
		case task.FieldDueTz:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field due_tz", values[i])
			} else if value.Valid {
				_m.DueTz = value.String
			}
		case task.FieldStartTz:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field start_tz", values[i])
			} else if value.Valid {
				_m.StartTz = value.String
			}
		case task.FieldParentUID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field parent_uid", values[i])
			} else if value.Valid {
				_m.ParentUID = value.String
			}
		case task.FieldRecurrence:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field recurrence", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Recurrence); err != nil {
					return fmt.Errorf("unmarshal field recurrence: %w", err)
				}
			}
		case task.FieldReminders:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field reminders", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Reminders); err != nil {
					return fmt.Errorf("unmarshal field reminders: %w", err)
				}
			}
		case task.FieldCategories:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field categories", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Categories); err != nil {
					return fmt.Errorf("unmarshal field categories: %w", err)
				}
			}
		case task.FieldRawIcs:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field raw_ics", values[i])
			} else if value.Valid {
				_m.RawIcs = value.String
			}
		case task.FieldCreated:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field created", values[i])
			} else if value.Valid {
				_m.Created = value.Time
			}
		case task.FieldUpdated:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field updated", values[i])
			} else if value.Valid {
				_m.Updated = value.Time
			}
		case task.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field calendar_tasks", values[i])
			} else if value.Valid {
				_m.calendar_tasks = new(string)
				*_m.calendar_tasks = 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 Task.
// This includes values selected through modifiers, order, etc.
func (_m *Task) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

// QueryCalendar queries the "calendar" edge of the Task entity.
func (_m *Task) QueryCalendar() *CalendarQuery {
	return NewTaskClient(_m.config).QueryCalendar(_m)
}

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

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

// String implements the fmt.Stringer.
func (_m *Task) String() string {
	var builder strings.Builder
	builder.WriteString("Task(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("uid=")
	builder.WriteString(_m.UID)
	builder.WriteString(", ")
	builder.WriteString("remote_id=")
	builder.WriteString(_m.RemoteID)
	builder.WriteString(", ")
	builder.WriteString("etag=")
	builder.WriteString(_m.Etag)
	builder.WriteString(", ")
	builder.WriteString("summary=")
	builder.WriteString(_m.Summary)
	builder.WriteString(", ")
	builder.WriteString("description=")
	builder.WriteString(_m.Description)
	builder.WriteString(", ")
	builder.WriteString("location=")
	builder.WriteString(_m.Location)
	builder.WriteString(", ")
	builder.WriteString("status=")
	builder.WriteString(fmt.Sprintf("%v", _m.Status))
	builder.WriteString(", ")
	builder.WriteString("priority=")
	builder.WriteString(fmt.Sprintf("%v", _m.Priority))
	builder.WriteString(", ")
	builder.WriteString("percent_complete=")
	builder.WriteString(fmt.Sprintf("%v", _m.PercentComplete))
	builder.WriteString(", ")
	if v := _m.Due; v != nil {
		builder.WriteString("due=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.Start; v != nil {
		builder.WriteString("start=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	if v := _m.Completed; v != nil {
		builder.WriteString("completed=")
		builder.WriteString(v.Format(time.ANSIC))
	}
	builder.WriteString(", ")
	builder.WriteString("all_day=")
	builder.WriteString(fmt.Sprintf("%v", _m.AllDay))
	builder.WriteString(", ")
	builder.WriteString("due_tz=")
	builder.WriteString(_m.DueTz)
	builder.WriteString(", ")
	builder.WriteString("start_tz=")
	builder.WriteString(_m.StartTz)
	builder.WriteString(", ")
	builder.WriteString("parent_uid=")
	builder.WriteString(_m.ParentUID)
	builder.WriteString(", ")
	builder.WriteString("recurrence=")
	builder.WriteString(fmt.Sprintf("%v", _m.Recurrence))
	builder.WriteString(", ")
	builder.WriteString("reminders=")
	builder.WriteString(fmt.Sprintf("%v", _m.Reminders))
	builder.WriteString(", ")
	builder.WriteString("categories=")
	builder.WriteString(fmt.Sprintf("%v", _m.Categories))
	builder.WriteString(", ")
	builder.WriteString("raw_ics=")
	builder.WriteString(_m.RawIcs)
	builder.WriteString(", ")
	builder.WriteString("created=")
	builder.WriteString(_m.Created.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated=")
	builder.WriteString(_m.Updated.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// Tasks is a parsable slice of Task.
type Tasks []*Task
