// Code generated by ent, DO NOT EDIT.

package ent

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

	"entgo.io/ent"
	"entgo.io/ent/dialect/sql"
	"git.dcentral.systems/toolz/goplt/internal/ent/auditlog"
)

// AuditLog is the model entity for the AuditLog schema.
type AuditLog struct {
	config `json:"-"`
	// ID of the ent.
	ID string `json:"id,omitempty"`
	// ID of the user/actor performing the action
	UserID string `json:"user_id,omitempty"`
	// Action performed (e.g., user.create, user.update)
	Action string `json:"action,omitempty"`
	// Resource type (e.g., user, role)
	Resource string `json:"resource,omitempty"`
	// ID of the target resource
	ResourceID string `json:"resource_id,omitempty"`
	// IP address of the client
	IPAddress string `json:"ip_address,omitempty"`
	// User agent of the client
	UserAgent string `json:"user_agent,omitempty"`
	// Additional metadata as JSON
	Metadata map[string]interface{} `json:"metadata,omitempty"`
	// Timestamp holds the value of the "timestamp" field.
	Timestamp    time.Time `json:"timestamp,omitempty"`
	selectValues sql.SelectValues
}

// scanValues returns the types for scanning values from sql.Rows.
func (*AuditLog) scanValues(columns []string) ([]any, error) {
	values := make([]any, len(columns))
	for i := range columns {
		switch columns[i] {
		case auditlog.FieldMetadata:
			values[i] = new([]byte)
		case auditlog.FieldID, auditlog.FieldUserID, auditlog.FieldAction, auditlog.FieldResource, auditlog.FieldResourceID, auditlog.FieldIPAddress, auditlog.FieldUserAgent:
			values[i] = new(sql.NullString)
		case auditlog.FieldTimestamp:
			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 AuditLog fields.
func (_m *AuditLog) 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 auditlog.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 auditlog.FieldUserID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field user_id", values[i])
			} else if value.Valid {
				_m.UserID = value.String
			}
		case auditlog.FieldAction:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field action", values[i])
			} else if value.Valid {
				_m.Action = value.String
			}
		case auditlog.FieldResource:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field resource", values[i])
			} else if value.Valid {
				_m.Resource = value.String
			}
		case auditlog.FieldResourceID:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field resource_id", values[i])
			} else if value.Valid {
				_m.ResourceID = value.String
			}
		case auditlog.FieldIPAddress:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field ip_address", values[i])
			} else if value.Valid {
				_m.IPAddress = value.String
			}
		case auditlog.FieldUserAgent:
			if value, ok := values[i].(*sql.NullString); !ok {
				return fmt.Errorf("unexpected type %T for field user_agent", values[i])
			} else if value.Valid {
				_m.UserAgent = value.String
			}
		case auditlog.FieldMetadata:
			if value, ok := values[i].(*[]byte); !ok {
				return fmt.Errorf("unexpected type %T for field metadata", values[i])
			} else if value != nil && len(*value) > 0 {
				if err := json.Unmarshal(*value, &_m.Metadata); err != nil {
					return fmt.Errorf("unmarshal field metadata: %w", err)
				}
			}
		case auditlog.FieldTimestamp:
			if value, ok := values[i].(*sql.NullTime); !ok {
				return fmt.Errorf("unexpected type %T for field timestamp", values[i])
			} else if value.Valid {
				_m.Timestamp = value.Time
			}
		default:
			_m.selectValues.Set(columns[i], values[i])
		}
	}
	return nil
}

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

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

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

// String implements the fmt.Stringer.
func (_m *AuditLog) String() string {
	var builder strings.Builder
	builder.WriteString("AuditLog(")
	builder.WriteString(fmt.Sprintf("id=%v, ", _m.ID))
	builder.WriteString("user_id=")
	builder.WriteString(_m.UserID)
	builder.WriteString(", ")
	builder.WriteString("action=")
	builder.WriteString(_m.Action)
	builder.WriteString(", ")
	builder.WriteString("resource=")
	builder.WriteString(_m.Resource)
	builder.WriteString(", ")
	builder.WriteString("resource_id=")
	builder.WriteString(_m.ResourceID)
	builder.WriteString(", ")
	builder.WriteString("ip_address=")
	builder.WriteString(_m.IPAddress)
	builder.WriteString(", ")
	builder.WriteString("user_agent=")
	builder.WriteString(_m.UserAgent)
	builder.WriteString(", ")
	builder.WriteString("metadata=")
	builder.WriteString(fmt.Sprintf("%v", _m.Metadata))
	builder.WriteString(", ")
	builder.WriteString("timestamp=")
	builder.WriteString(_m.Timestamp.Format(time.ANSIC))
	builder.WriteByte(')')
	return builder.String()
}

// AuditLogs is a parsable slice of AuditLog.
type AuditLogs []*AuditLog
