// 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/data/ent/casbackend"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/casmapping"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/organization"
	"github.com/chainloop-dev/chainloop/app/controlplane/pkg/data/ent/project"
	"github.com/google/uuid"
)

// CASMapping is the model entity for the CASMapping schema.
type CASMapping struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// Digest holds the value of the "digest" field.
	Digest string `json:"digest,omitempty"`
	// CreatedAt holds the value of the "created_at" field.
	CreatedAt time.Time `json:"created_at,omitempty"`
	// WorkflowRunID holds the value of the "workflow_run_id" field.
	WorkflowRunID uuid.UUID `json:"workflow_run_id,omitempty"`
	// OrganizationID holds the value of the "organization_id" field.
	OrganizationID uuid.UUID `json:"organization_id,omitempty"`
	// ProjectID holds the value of the "project_id" field.
	ProjectID uuid.UUID `json:"project_id,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the CASMappingQuery when eager-loading is set.
	Edges                   CASMappingEdges `json:"edges"`
	cas_mapping_cas_backend *uuid.UUID
	selectValues            sql.SelectValues
}

// CASMappingEdges holds the relations/edges for other nodes in the graph.
type CASMappingEdges struct {
	// CasBackend holds the value of the cas_backend edge.
	CasBackend *CASBackend `json:"cas_backend,omitempty"`
	// Organization holds the value of the organization edge.
	Organization *Organization `json:"organization,omitempty"`
	// Project holds the value of the project edge.
	Project *Project `json:"project,omitempty"`
	// loadedTypes holds the information for reporting if a
	// type was loaded (or requested) in eager-loading or not.
	loadedTypes [3]bool
}

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

// 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 CASMappingEdges) OrganizationOrErr() (*Organization, error) {
	if e.Organization != nil {
		return e.Organization, nil
	} else if e.loadedTypes[1] {
		return nil, &NotFoundError{label: organization.Label}
	}
	return nil, &NotLoadedError{edge: "organization"}
}

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

// scanValues returns the types for scanning values from sql.Rows.
func (*CASMapping) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case casmapping.FieldDigest:
			values[i] = new(sql.NullString)
		case casmapping.FieldCreatedAt:
			values[i] = new(sql.NullTime)
		case casmapping.FieldID, casmapping.FieldWorkflowRunID, casmapping.FieldOrganizationID, casmapping.FieldProjectID:
			values[i] = new(uuid.UUID)
		case casmapping.ForeignKeys[0]: // cas_mapping_cas_backend
			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 CASMapping fields.
func (cm *CASMapping) 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 casmapping.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				cm.ID = *value
			}
		case casmapping.FieldDigest:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field digest", values[i])
			} else if value.Valid {
				cm.Digest = value.String
			}
		case casmapping.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 {
				cm.CreatedAt = value.Time
			}
		case casmapping.FieldWorkflowRunID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field workflow_run_id", values[i])
			} else if value != nil {
				cm.WorkflowRunID = *value
			}
		case casmapping.FieldOrganizationID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field organization_id", values[i])
			} else if value != nil {
				cm.OrganizationID = *value
			}
		case casmapping.FieldProjectID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field project_id", values[i])
			} else if value != nil {
				cm.ProjectID = *value
			}
		case casmapping.ForeignKeys[0]:
			if value, ok := values[i].(*sql.NullScanner); !ok {
				return fmt.Errorf("unexpected type %T for field cas_mapping_cas_backend", values[i])
			} else if value.Valid {
				cm.cas_mapping_cas_backend = new(uuid.UUID)
				*cm.cas_mapping_cas_backend = *value.S.(*uuid.UUID)
			}
		default:
			cm.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryCasBackend queries the "cas_backend" edge of the CASMapping entity.
func (cm *CASMapping) QueryCasBackend() *CASBackendQuery {
	return NewCASMappingClient(cm.config).QueryCasBackend(cm)
}

// QueryOrganization queries the "organization" edge of the CASMapping entity.
func (cm *CASMapping) QueryOrganization() *OrganizationQuery {
	return NewCASMappingClient(cm.config).QueryOrganization(cm)
}

// QueryProject queries the "project" edge of the CASMapping entity.
func (cm *CASMapping) QueryProject() *ProjectQuery {
	return NewCASMappingClient(cm.config).QueryProject(cm)
}

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

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

// String implements the fmt.Stringer.
func (cm *CASMapping) String() string {
	var builder strings.Builder
	builder.WriteString("CASMapping(")
	builder.WriteString(fmt.Sprintf("id=%v, ", cm.ID))
	builder.WriteString("digest=")
	builder.WriteString(cm.Digest)
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(cm.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("workflow_run_id=")
	builder.WriteString(fmt.Sprintf("%v", cm.WorkflowRunID))
	builder.WriteString(", ")
	builder.WriteString("organization_id=")
	builder.WriteString(fmt.Sprintf("%v", cm.OrganizationID))
	builder.WriteString(", ")
	builder.WriteString("project_id=")
	builder.WriteString(fmt.Sprintf("%v", cm.ProjectID))
	builder.WriteByte(')')
	return builder.String()
}

// CASMappings is a parsable slice of CASMapping.
type CASMappings []*CASMapping
