// Copyright 2021-present The Atlas Authors. All rights reserved.
// This source code is licensed under the Apache 2.0 license found
// in the LICENSE file in the root directory of this source tree.

// Code generated by entc, DO NOT EDIT.

package ent

import (
	"encoding/json"
	"fmt"
	"strings"
	"time"

	"ariga.io/atlas/cmd/atlas/internal/migrate/ent/revision"
	"ariga.io/atlas/sql/migrate"
	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
)

// Revision is the model entity for the Revision schema.
type Revision struct {
	config `json:"-"`
	// ID of the ent.
	ID string `json:"id,omitempty"`
	// Description holds the value of the "description" field.
	Description string `json:"description,omitempty"`
	// Type holds the value of the "type" field.
	Type migrate.RevisionType `json:"type,omitempty"`
	// Applied holds the value of the "applied" field.
	Applied int `json:"applied,omitempty"`
	// Total holds the value of the "total" field.
	Total int `json:"total,omitempty"`
	// ExecutedAt holds the value of the "executed_at" field.
	ExecutedAt time.Time `json:"executed_at,omitempty"`
	// ExecutionTime holds the value of the "execution_time" field.
	ExecutionTime time.Duration `json:"execution_time,omitempty"`
	// Error holds the value of the "error" field.
	Error string `json:"error,omitempty"`
	// ErrorStmt holds the value of the "error_stmt" field.
	ErrorStmt string `json:"error_stmt,omitempty"`
	// Hash holds the value of the "hash" field.
	Hash string `json:"hash,omitempty"`
	// PartialHashes holds the value of the "partial_hashes" field.
	PartialHashes []string `json:"partial_hashes,omitempty"`
	// OperatorVersion holds the value of the "operator_version" field.
	OperatorVersion string `json:"operator_version,omitempty"`
	selectValues    sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*Revision) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case revision.FieldPartialHashes:
			values[i] = new([]byte)
		case revision.FieldType, revision.FieldApplied, revision.FieldTotal, revision.FieldExecutionTime:
			values[i] = new(sql.NullInt64)
		case revision.FieldID, revision.FieldDescription, revision.FieldError, revision.FieldErrorStmt, revision.FieldHash, revision.FieldOperatorVersion:
			values[i] = new(sql.NullString)
		case revision.FieldExecutedAt:
			values[i] = new(sql.NullTime)
		default:
			values[i] = new(sql.UnknownType)
		}
	}
	return values, nil
}

// assignValues assigns the values that were returned from sql.Rows (after scanning)
// to the Revision fields.
func (_m *Revision) 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 revision.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 revision.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 revision.FieldType:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field type", values[i])
			} else if value.Valid {
				_m.Type = migrate.RevisionType(value.Int64)
			}
		case revision.FieldApplied:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field applied", values[i])
			} else if value.Valid {
				_m.Applied = int(value.Int64)
			}
		case revision.FieldTotal:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field total", values[i])
			} else if value.Valid {
				_m.Total = int(value.Int64)
			}
		case revision.FieldExecutedAt:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field executed_at", values[i])
			} else if value.Valid {
				_m.ExecutedAt = value.Time
			}
		case revision.FieldExecutionTime:
			if value, ok := values[i].(*sql.NullInt64); !ok {
				return fmt.Errorf("unexpected type %T for field execution_time", values[i])
			} else if value.Valid {
				_m.ExecutionTime = time.Duration(value.Int64)
			}
		case revision.FieldError:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field error", values[i])
			} else if value.Valid {
				_m.Error = value.String
			}
		case revision.FieldErrorStmt:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field error_stmt", values[i])
			} else if value.Valid {
				_m.ErrorStmt = value.String
			}
		case revision.FieldHash:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field hash", values[i])
			} else if value.Valid {
				_m.Hash = value.String
			}
		case revision.FieldPartialHashes:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field partial_hashes", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.PartialHashes); err != nil {
					return fmt.Errorf("unmarshal field partial_hashes: %w", err)
				}
			}
		case revision.FieldOperatorVersion:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field operator_version", values[i])
			} else if value.Valid {
				_m.OperatorVersion = 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 Revision.
// This includes values selected through modifiers, order, etc.
func (_m *Revision) Value(name string) (ent.Value, error) {
	return _m.selectValues.Get(name)
}

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

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

// String implements the fmt.Stringer.
func (_m *Revision) String() string {
	var builder strings.Builder
	builder.WriteString("Revision(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("description=")
	builder.WriteString(_m.Description)
	builder.WriteString(", ")
	builder.WriteString("type=")
	builder.WriteString(fmt.Sprintf("%v", _m.Type))
	builder.WriteString(", ")
	builder.WriteString("applied=")
	builder.WriteString(fmt.Sprintf("%v", _m.Applied))
	builder.WriteString(", ")
	builder.WriteString("total=")
	builder.WriteString(fmt.Sprintf("%v", _m.Total))
	builder.WriteString(", ")
	builder.WriteString("executed_at=")
	builder.WriteString(_m.ExecutedAt.Format(time.ANSIC))
	builder.WriteString(", ")
	builder.WriteString("execution_time=")
	builder.WriteString(fmt.Sprintf("%v", _m.ExecutionTime))
	builder.WriteString(", ")
	builder.WriteString("error=")
	builder.WriteString(_m.Error)
	builder.WriteString(", ")
	builder.WriteString("error_stmt=")
	builder.WriteString(_m.ErrorStmt)
	builder.WriteString(", ")
	builder.WriteString("hash=")
	builder.WriteString(_m.Hash)
	builder.WriteString(", ")
	builder.WriteString("partial_hashes=")
	builder.WriteString(fmt.Sprintf("%v", _m.PartialHashes))
	builder.WriteString(", ")
	builder.WriteString("operator_version=")
	builder.WriteString(_m.OperatorVersion)
	builder.WriteByte(')')
	return builder.String()
}

// Revisions is a parsable slice of Revision.
type Revisions []*Revision
