// Code generated by ent, DO NOT EDIT.

package ent

import (
	"demo/ent/course"
	"demo/ent/enrollment"
	"demo/ent/student"
	"fmt"
	"strings"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
)

// Enrollment is the model entity for the Enrollment schema.
type Enrollment struct {
	config `json:"-"`
	// ID of the ent.
	ID int `json:"id,omitempty"`
	// Semester holds the value of the "semester" field.
	Semester string `json:"semester,omitempty"`
	// Year holds the value of the "year" field.
	Year int `json:"year,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the EnrollmentQuery when eager-loading is set.
	Edges               EnrollmentEdges `json:"edges"`
	course_enrollments  *int
	student_enrollments *int
	selectValues        sql.SelectValues
}

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

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

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

// scanValues returns the types for scanning values from sql.Rows.
func (*Enrollment) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case enrollment.FieldID, enrollment.FieldYear:
			values[i] = new(sql.NullInt64)
		case enrollment.FieldSemester:
			values[i] = new(sql.NullString)
		case enrollment.ForeignKeys[0]: // course_enrollments
			values[i] = new(sql.NullInt64)
		case enrollment.ForeignKeys[1]: // student_enrollments
			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 Enrollment fields.
func (e *Enrollment) 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 enrollment.FieldID:
			value, ok := values[i].(*sql.NullInt64)
			if !ok {
				return fmt.Errorf("unexpected type %T for field id", value)
			}
			e.ID = int(value.Int64)
		case enrollment.FieldSemester:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field semester", values[i])
			} else if value.Valid {
				e.Semester = value.String
			}
		case enrollment.FieldYear:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field year", values[i])
			} else if value.Valid {
				e.Year = int(value.Int64)
			}
		case enrollment.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field course_enrollments", value)
			} else if value.Valid {
				e.course_enrollments = new(int)
				*e.course_enrollments = int(value.Int64)
			}
		case enrollment.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for edge-field student_enrollments", value)
			} else if value.Valid {
				e.student_enrollments = new(int)
				*e.student_enrollments = int(value.Int64)
			}
		default:
			e.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryStudent queries the "student" edge of the Enrollment entity.
func (e *Enrollment) QueryStudent() *StudentQuery {
	return NewEnrollmentClient(e.config).QueryStudent(e)
}

// QueryCourse queries the "course" edge of the Enrollment entity.
func (e *Enrollment) QueryCourse() *CourseQuery {
	return NewEnrollmentClient(e.config).QueryCourse(e)
}

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

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

// String implements the fmt.Stringer.
func (e *Enrollment) String() string {
	var builder strings.Builder
	builder.WriteString("Enrollment(")
	builder.WriteString(fmt.Sprintf("id=%v, ", e.ID))
	builder.WriteString("semester=")
	builder.WriteString(e.Semester)
	builder.WriteString(", ")
	builder.WriteString("year=")
	builder.WriteString(fmt.Sprintf("%v", e.Year))
	builder.WriteByte(')')
	return builder.String()
}

// Enrollments is a parsable slice of Enrollment.
type Enrollments []*Enrollment
