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

// ProjectVersion is the model entity for the ProjectVersion schema.
type ProjectVersion struct {
	config `json:"-"`
	// ID of the ent.
	ID uuid.UUID `json:"id,omitempty"`
	// Version holds the value of the "version" field.
	Version string `json:"version,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"`
	// DeletedAt holds the value of the "deleted_at" field.
	DeletedAt time.Time `json:"deleted_at,omitempty"`
	// ProjectID holds the value of the "project_id" field.
	ProjectID uuid.UUID `json:"project_id,omitempty"`
	// Prerelease holds the value of the "prerelease" field.
	Prerelease bool `json:"prerelease,omitempty"`
	// WorkflowRunCount holds the value of the "workflow_run_count" field.
	WorkflowRunCount int `json:"workflow_run_count,omitempty"`
	// ReleasedAt holds the value of the "released_at" field.
	ReleasedAt time.Time `json:"released_at,omitempty"`
	// LastRunAt holds the value of the "last_run_at" field.
	LastRunAt time.Time `json:"last_run_at,omitempty"`
	// Whether this is the latest version of the project
	Latest bool `json:"latest,omitempty"`
	// Edges holds the relations/edges for other nodes in the graph.
	// The values are being populated by the ProjectVersionQuery when eager-loading is set.
	Edges        ProjectVersionEdges `json:"edges"`
	selectValues sql.SelectValues
}

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

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

// RunsOrErr returns the Runs value or an error if the edge
// was not loaded in eager-loading.
func (e ProjectVersionEdges) RunsOrErr() ([]*WorkflowRun, error) {
	if e.loadedTypes[1] {
		return e.Runs, nil
	}
	return nil, &NotLoadedError{edge: "runs"}
}

// scanValues returns the types for scanning values from sql.Rows.
func (*ProjectVersion) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case projectversion.FieldPrerelease, projectversion.FieldLatest:
			values[i] = new(sql.NullBool)
		case projectversion.FieldWorkflowRunCount:
			values[i] = new(sql.NullInt64)
		case projectversion.FieldVersion:
			values[i] = new(sql.NullString)
		case projectversion.FieldCreatedAt, projectversion.FieldUpdatedAt, projectversion.FieldDeletedAt, projectversion.FieldReleasedAt, projectversion.FieldLastRunAt:
			values[i] = new(sql.NullTime)
		case projectversion.FieldID, projectversion.FieldProjectID:
			values[i] = 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 ProjectVersion fields.
func (pv *ProjectVersion) 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 projectversion.FieldID:
			if value, ok := values[i].(*uuid.UUID); !ok {
				return fmt.Errorf("unexpected type %T for field id", values[i])
			} else if value != nil {
				pv.ID = *value
			}
		case projectversion.FieldVersion:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field version", values[i])
			} else if value.Valid {
				pv.Version = value.String
			}
		case projectversion.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 {
				pv.CreatedAt = value.Time
			}
		case projectversion.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 {
				pv.UpdatedAt = value.Time
			}
		case projectversion.FieldDeletedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field deleted_at", values[i])
			} else if value.Valid {
				pv.DeletedAt = value.Time
			}
		case projectversion.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 {
				pv.ProjectID = *value
			}
		case projectversion.FieldPrerelease:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field prerelease", values[i])
			} else if value.Valid {
				pv.Prerelease = value.Bool
			}
		case projectversion.FieldWorkflowRunCount:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field workflow_run_count", values[i])
			} else if value.Valid {
				pv.WorkflowRunCount = int(value.Int64)
			}
		case projectversion.FieldReleasedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field released_at", values[i])
			} else if value.Valid {
				pv.ReleasedAt = value.Time
			}
		case projectversion.FieldLastRunAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field last_run_at", values[i])
			} else if value.Valid {
				pv.LastRunAt = value.Time
			}
		case projectversion.FieldLatest:
			if value, ok := values[i].(*sql.NullBool); !ok {
				return fmt.Errorf("unexpected type %T for field latest", values[i])
			} else if value.Valid {
				pv.Latest = value.Bool
			}
		default:
			pv.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

// QueryProject queries the "project" edge of the ProjectVersion entity.
func (pv *ProjectVersion) QueryProject() *ProjectQuery {
	return NewProjectVersionClient(pv.config).QueryProject(pv)
}

// QueryRuns queries the "runs" edge of the ProjectVersion entity.
func (pv *ProjectVersion) QueryRuns() *WorkflowRunQuery {
	return NewProjectVersionClient(pv.config).QueryRuns(pv)
}

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

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

// String implements the fmt.Stringer.
func (pv *ProjectVersion) String() string {
	var builder strings.Builder
	builder.WriteString("ProjectVersion(")
	builder.WriteString(fmt.Sprintf("id=%v, ", pv.ID))
	builder.WriteString("version=")
	builder.WriteString(pv.Version)
	builder.WriteString(", ")
	builder.WriteString("created_at=")
	builder.WriteString(pv.CreatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("updated_at=")
	builder.WriteString(pv.UpdatedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("deleted_at=")
	builder.WriteString(pv.DeletedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("project_id=")
	builder.WriteString(fmt.Sprintf("%v", pv.ProjectID))
	builder.WriteString(", ")
	builder.WriteString("prerelease=")
	builder.WriteString(fmt.Sprintf("%v", pv.Prerelease))
	builder.WriteString(", ")
	builder.WriteString("workflow_run_count=")
	builder.WriteString(fmt.Sprintf("%v", pv.WorkflowRunCount))
	builder.WriteString(", ")
	builder.WriteString("released_at=")
	builder.WriteString(pv.ReleasedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("last_run_at=")
	builder.WriteString(pv.LastRunAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("latest=")
	builder.WriteString(fmt.Sprintf("%v", pv.Latest))
	builder.WriteByte(')')
	return builder.String()
}

// ProjectVersions is a parsable slice of ProjectVersion.
type ProjectVersions []*ProjectVersion
