// Code generated by ent, DO NOT EDIT.

package ent

import (
	"fmt"
	"strings"
	"time"

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/authz"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/membership"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/organization"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/user"
	"github.com/google/uuid"
)

// Membership is the model entity for the Membership schema.
type Membership struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// Current holds the value of the "current" field.
	Current bool `json:"current,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"`
	// Role holds the value of the "role" field.
	Role authz.Role `json:"role,omitempty"`
	// MembershipType holds the value of the "membership_type" field.
	MembershipType authz.MembershipType `json:"membership_type,omitempty"`
	// MemberID holds the value of the "member_id" field.
	MemberID uuid.UUID `json:"member_id,omitempty"`
	// ResourceType holds the value of the "resource_type" field.
	ResourceType authz.ResourceType `json:"resource_type,omitempty"`
	// ResourceID holds the value of the "resource_id" field.
	ResourceID uuid.UUID `json:"resource_id,omitempty"`
	// ParentID holds the value of the "parent_id" field.
	ParentID *uuid.UUID `json:"parent_id,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the MembershipQuery when eager-loading is set.
	Edges                    MembershipEdges `json:"edges"`
	organization_memberships *uuid.UUID
	user_memberships         *uuid.UUID
	selectValues             sql.SelectValues
}

// MembershipEdges holds the relations/edges for other nodes in the graph.
type MembershipEdges struct {
	// Organization holds the value of the organization edge.
	Organization *Organization `json:"organization,omitempty"`
	// User holds the value of the user edge.
	User *User `json:"user,omitempty"`
	// Parent holds the value of the parent edge.
	Parent *Membership `json:"parent,omitempty"`
	// Children holds the value of the children edge.
	Children []*Membership `json:"children,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [4]bool
}

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

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

// ParentOrErr returns the Parent value or an error if the edge
// was not loaded in eager-loading, or loaded but was not found.
func (e MembershipEdges) ParentOrErr() (*Membership, error) {
	if e.Parent != nil {
		return e.Parent, nil
	} else if e.loadedTypes[2] {
		return nil, &NotFoundError{label: membership.Label}
	}
	return nil, &NotLoadedError{edge: "parent"}
}

// ChildrenOrErr returns the Children value or an error if the edge
// was not loaded in eager-loading.
func (e MembershipEdges) ChildrenOrErr() ([]*Membership, error) {
	if e.loadedTypes[3] {
		return e.Children, nil
	}
	return nil, &NotLoadedError{edge: "children"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Membership) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case membership.FieldParentID:
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case membership.FieldCurrent:
			values[i] = new(sql.NullBool)
		case membership.FieldRole, membership.FieldMembershipType, membership.FieldResourceType:
			values[i] = new(sql.NullString)
		case membership.FieldCreatedAt, membership.FieldUpdatedAt:
			values[i] = new(sql.NullTime)
		case membership.FieldID, membership.FieldMemberID, membership.FieldResourceID:
			values[i] = new(uuid.UUID)
		case membership.ForeignKeys[0]: // organization_memberships
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		case membership.ForeignKeys[1]: // user_memberships
			values[i] = &sql.NullScanner{S: new(uuid.UUID)}
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Membership fields.
func (m *Membership) 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 membership.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				m.ID = *value
			}
		case membership.FieldCurrent:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field current", values[i])
			} else if value.Valid {
				m.Current = value.Bool
			}
		case membership.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 membership.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 membership.FieldRole:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field role", values[i])
			} else if value.Valid {
				m.Role = authz.Role(value.String)
			}
		case membership.FieldMembershipType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field membership_type", values[i])
			} else if value.Valid {
				m.MembershipType = authz.MembershipType(value.String)
			}
		case membership.FieldMemberID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field member_id", values[i])
			} else if value != nil {
				m.MemberID = *value
			}
		case membership.FieldResourceType:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field resource_type", values[i])
			} else if value.Valid {
				m.ResourceType = authz.ResourceType(value.String)
			}
		case membership.FieldResourceID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field resource_id", values[i])
			} else if value != nil {
				m.ResourceID = *value
			}
		case membership.FieldParentID:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field parent_id", values[i])
			} else if value.Valid {
				m.ParentID = new(uuid.UUID)
				*m.ParentID = *value.S.(*uuid.UUID)
			}
		case membership.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field organization_memberships", values[i])
			} else if value.Valid {
				m.organization_memberships = new(uuid.UUID)
				*m.organization_memberships = *value.S.(*uuid.UUID)
			}
		case membership.ForeignKeys[1]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field user_memberships", values[i])
			} else if value.Valid {
				m.user_memberships = new(uuid.UUID)
				*m.user_memberships = *value.S.(*uuid.UUID)
			}
		default:
			m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryOrganization queries the "organization" edge of the Membership entity.
func (m *Membership) QueryOrganization() *OrganizationQuery {
	return NewMembershipClient(m.config).QueryOrganization(m)
}

// QueryUser queries the "user" edge of the Membership entity.
func (m *Membership) QueryUser() *UserQuery {
	return NewMembershipClient(m.config).QueryUser(m)
}

// QueryParent queries the "parent" edge of the Membership entity.
func (m *Membership) QueryParent() *MembershipQuery {
	return NewMembershipClient(m.config).QueryParent(m)
}

// QueryChildren queries the "children" edge of the Membership entity.
func (m *Membership) QueryChildren() *MembershipQuery {
	return NewMembershipClient(m.config).QueryChildren(m)
}

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

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

// String implements the fmt.Stringer.
func (m *Membership) String() string {
	var builder strings.Builder
	builder.WriteString("Membership(")
	builder.WriteString(fmt.Sprintf("id=%v, ", m.ID))
	builder.WriteString("current=")
	builder.WriteString(fmt.Sprintf("%v", m.Current))
	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.WriteString(", ")
	builder.WriteString("role=")
	builder.WriteString(fmt.Sprintf("%v", m.Role))
	builder.WriteString(", ")
	builder.WriteString("membership_type=")
	builder.WriteString(fmt.Sprintf("%v", m.MembershipType))
	builder.WriteString(", ")
	builder.WriteString("member_id=")
	builder.WriteString(fmt.Sprintf("%v", m.MemberID))
	builder.WriteString(", ")
	builder.WriteString("resource_type=")
	builder.WriteString(fmt.Sprintf("%v", m.ResourceType))
	builder.WriteString(", ")
	builder.WriteString("resource_id=")
	builder.WriteString(fmt.Sprintf("%v", m.ResourceID))
	builder.WriteString(", ")
	if v := m.ParentID; v != nil {
		builder.WriteString("parent_id=")
		builder.WriteString(fmt.Sprintf("%v", *v))
	}
	builder.WriteByte(')')
	return builder.String()
}

// Memberships is a parsable slice of Membership.
type Memberships []*Membership
