// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/dispute/rpc/internal/models/disputes"
	"juwan-backend/pkg/types"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
)

// DisputesCreate is the builder for creating a Disputes entity.
type DisputesCreate struct {
	config
	mutation *DisputesMutation
	hooks    []Hook
}

// SetOrderID sets the "order_id" field.
func (_c *DisputesCreate) SetOrderID(v int64) *DisputesCreate {
	_c.mutation.SetOrderID(v)
	return _c
}

// SetInitiatorID sets the "initiator_id" field.
func (_c *DisputesCreate) SetInitiatorID(v int64) *DisputesCreate {
	_c.mutation.SetInitiatorID(v)
	return _c
}

// SetInitiatorName sets the "initiator_name" field.
func (_c *DisputesCreate) SetInitiatorName(v string) *DisputesCreate {
	_c.mutation.SetInitiatorName(v)
	return _c
}

// SetRespondentID sets the "respondent_id" field.
func (_c *DisputesCreate) SetRespondentID(v int64) *DisputesCreate {
	_c.mutation.SetRespondentID(v)
	return _c
}

// SetReason sets the "reason" field.
func (_c *DisputesCreate) SetReason(v string) *DisputesCreate {
	_c.mutation.SetReason(v)
	return _c
}

// SetEvidence sets the "evidence" field.
func (_c *DisputesCreate) SetEvidence(v types.TextArray) *DisputesCreate {
	_c.mutation.SetEvidence(v)
	return _c
}

// SetNillableEvidence sets the "evidence" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableEvidence(v *types.TextArray) *DisputesCreate {
	if v != nil {
		_c.SetEvidence(*v)
	}
	return _c
}

// SetStatus sets the "status" field.
func (_c *DisputesCreate) SetStatus(v string) *DisputesCreate {
	_c.mutation.SetStatus(v)
	return _c
}

// SetNillableStatus sets the "status" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableStatus(v *string) *DisputesCreate {
	if v != nil {
		_c.SetStatus(*v)
	}
	return _c
}

// SetResult sets the "result" field.
func (_c *DisputesCreate) SetResult(v string) *DisputesCreate {
	_c.mutation.SetResult(v)
	return _c
}

// SetNillableResult sets the "result" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableResult(v *string) *DisputesCreate {
	if v != nil {
		_c.SetResult(*v)
	}
	return _c
}

// SetRespondentReason sets the "respondent_reason" field.
func (_c *DisputesCreate) SetRespondentReason(v string) *DisputesCreate {
	_c.mutation.SetRespondentReason(v)
	return _c
}

// SetNillableRespondentReason sets the "respondent_reason" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableRespondentReason(v *string) *DisputesCreate {
	if v != nil {
		_c.SetRespondentReason(*v)
	}
	return _c
}

// SetRespondentEvidence sets the "respondent_evidence" field.
func (_c *DisputesCreate) SetRespondentEvidence(v types.TextArray) *DisputesCreate {
	_c.mutation.SetRespondentEvidence(v)
	return _c
}

// SetNillableRespondentEvidence sets the "respondent_evidence" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableRespondentEvidence(v *types.TextArray) *DisputesCreate {
	if v != nil {
		_c.SetRespondentEvidence(*v)
	}
	return _c
}

// SetAppealReason sets the "appeal_reason" field.
func (_c *DisputesCreate) SetAppealReason(v string) *DisputesCreate {
	_c.mutation.SetAppealReason(v)
	return _c
}

// SetNillableAppealReason sets the "appeal_reason" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableAppealReason(v *string) *DisputesCreate {
	if v != nil {
		_c.SetAppealReason(*v)
	}
	return _c
}

// SetAppealedAt sets the "appealed_at" field.
func (_c *DisputesCreate) SetAppealedAt(v time.Time) *DisputesCreate {
	_c.mutation.SetAppealedAt(v)
	return _c
}

// SetNillableAppealedAt sets the "appealed_at" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableAppealedAt(v *time.Time) *DisputesCreate {
	if v != nil {
		_c.SetAppealedAt(*v)
	}
	return _c
}

// SetResolvedBy sets the "resolved_by" field.
func (_c *DisputesCreate) SetResolvedBy(v int64) *DisputesCreate {
	_c.mutation.SetResolvedBy(v)
	return _c
}

// SetNillableResolvedBy sets the "resolved_by" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableResolvedBy(v *int64) *DisputesCreate {
	if v != nil {
		_c.SetResolvedBy(*v)
	}
	return _c
}

// SetResolvedAt sets the "resolved_at" field.
func (_c *DisputesCreate) SetResolvedAt(v time.Time) *DisputesCreate {
	_c.mutation.SetResolvedAt(v)
	return _c
}

// SetNillableResolvedAt sets the "resolved_at" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableResolvedAt(v *time.Time) *DisputesCreate {
	if v != nil {
		_c.SetResolvedAt(*v)
	}
	return _c
}

// SetCreatedAt sets the "created_at" field.
func (_c *DisputesCreate) SetCreatedAt(v time.Time) *DisputesCreate {
	_c.mutation.SetCreatedAt(v)
	return _c
}

// SetNillableCreatedAt sets the "created_at" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableCreatedAt(v *time.Time) *DisputesCreate {
	if v != nil {
		_c.SetCreatedAt(*v)
	}
	return _c
}

// SetUpdatedAt sets the "updated_at" field.
func (_c *DisputesCreate) SetUpdatedAt(v time.Time) *DisputesCreate {
	_c.mutation.SetUpdatedAt(v)
	return _c
}

// SetNillableUpdatedAt sets the "updated_at" field if the given value is not nil.
func (_c *DisputesCreate) SetNillableUpdatedAt(v *time.Time) *DisputesCreate {
	if v != nil {
		_c.SetUpdatedAt(*v)
	}
	return _c
}

// SetID sets the "id" field.
func (_c *DisputesCreate) SetID(v int64) *DisputesCreate {
	_c.mutation.SetID(v)
	return _c
}

// Mutation returns the DisputesMutation object of the builder.
func (_c *DisputesCreate) Mutation() *DisputesMutation {
	return _c.mutation
}

// Save creates the Disputes in the database.
func (_c *DisputesCreate) Save(ctx context.Context) (*Disputes, error) {
	_c.defaults()
	return withHooks(ctx, _c.sqlSave, _c.mutation, _c.hooks)
}

// SaveX calls Save and panics if Save returns an error.
func (_c *DisputesCreate) SaveX(ctx context.Context) *Disputes {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (_c *DisputesCreate) Exec(ctx context.Context) error {
	_, err := _c.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_c *DisputesCreate) ExecX(ctx context.Context) {
	if err := _c.Exec(ctx); err != nil {
		panic(err)
	}
}

// defaults sets the default values of the builder before save.
func (_c *DisputesCreate) defaults() {
	if _, ok := _c.mutation.Status(); !ok {
		v := disputes.DefaultStatus
		_c.mutation.SetStatus(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := disputes.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := disputes.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *DisputesCreate) check() error {
	if _, ok := _c.mutation.OrderID(); !ok {
		return &ValidationError{Name: "order_id", err: errors.New(`models: missing required field "Disputes.order_id"`)}
	}
	if _, ok := _c.mutation.InitiatorID(); !ok {
		return &ValidationError{Name: "initiator_id", err: errors.New(`models: missing required field "Disputes.initiator_id"`)}
	}
	if _, ok := _c.mutation.InitiatorName(); !ok {
		return &ValidationError{Name: "initiator_name", err: errors.New(`models: missing required field "Disputes.initiator_name"`)}
	}
	if v, ok := _c.mutation.InitiatorName(); ok {
		if err := disputes.InitiatorNameValidator(v); err != nil {
			return &ValidationError{Name: "initiator_name", err: fmt.Errorf(`models: validator failed for field "Disputes.initiator_name": %w`, err)}
		}
	}
	if _, ok := _c.mutation.RespondentID(); !ok {
		return &ValidationError{Name: "respondent_id", err: errors.New(`models: missing required field "Disputes.respondent_id"`)}
	}
	if _, ok := _c.mutation.Reason(); !ok {
		return &ValidationError{Name: "reason", err: errors.New(`models: missing required field "Disputes.reason"`)}
	}
	if _, ok := _c.mutation.Status(); !ok {
		return &ValidationError{Name: "status", err: errors.New(`models: missing required field "Disputes.status"`)}
	}
	if v, ok := _c.mutation.Status(); ok {
		if err := disputes.StatusValidator(v); err != nil {
			return &ValidationError{Name: "status", err: fmt.Errorf(`models: validator failed for field "Disputes.status": %w`, err)}
		}
	}
	if v, ok := _c.mutation.Result(); ok {
		if err := disputes.ResultValidator(v); err != nil {
			return &ValidationError{Name: "result", err: fmt.Errorf(`models: validator failed for field "Disputes.result": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "Disputes.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "Disputes.updated_at"`)}
	}
	return nil
}

func (_c *DisputesCreate) sqlSave(ctx context.Context) (*Disputes, error) {
	if err := _c.check(); err != nil {
		return nil, err
	}
	_node, _spec := _c.createSpec()
	if err := sqlgraph.CreateNode(ctx, _c.driver, _spec); err != nil {
		if sqlgraph.IsConstraintError(err) {
			err = &ConstraintError{msg: err.Error(), wrap: err}
		}
		return nil, err
	}
	if _spec.ID.Value != _node.ID {
		id := _spec.ID.Value.(int64)
		_node.ID = int64(id)
	}
	_c.mutation.id = &_node.ID
	_c.mutation.done = true
	return _node, nil
}

func (_c *DisputesCreate) createSpec() (*Disputes, *sqlgraph.CreateSpec) {
	var (
		_node = &Disputes{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(disputes.Table, sqlgraph.NewFieldSpec(disputes.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.OrderID(); ok {
		_spec.SetField(disputes.FieldOrderID, field.TypeInt64, value)
		_node.OrderID = value
	}
	if value, ok := _c.mutation.InitiatorID(); ok {
		_spec.SetField(disputes.FieldInitiatorID, field.TypeInt64, value)
		_node.InitiatorID = value
	}
	if value, ok := _c.mutation.InitiatorName(); ok {
		_spec.SetField(disputes.FieldInitiatorName, field.TypeString, value)
		_node.InitiatorName = value
	}
	if value, ok := _c.mutation.RespondentID(); ok {
		_spec.SetField(disputes.FieldRespondentID, field.TypeInt64, value)
		_node.RespondentID = value
	}
	if value, ok := _c.mutation.Reason(); ok {
		_spec.SetField(disputes.FieldReason, field.TypeString, value)
		_node.Reason = value
	}
	if value, ok := _c.mutation.Evidence(); ok {
		_spec.SetField(disputes.FieldEvidence, field.TypeOther, value)
		_node.Evidence = value
	}
	if value, ok := _c.mutation.Status(); ok {
		_spec.SetField(disputes.FieldStatus, field.TypeString, value)
		_node.Status = value
	}
	if value, ok := _c.mutation.Result(); ok {
		_spec.SetField(disputes.FieldResult, field.TypeString, value)
		_node.Result = &value
	}
	if value, ok := _c.mutation.RespondentReason(); ok {
		_spec.SetField(disputes.FieldRespondentReason, field.TypeString, value)
		_node.RespondentReason = &value
	}
	if value, ok := _c.mutation.RespondentEvidence(); ok {
		_spec.SetField(disputes.FieldRespondentEvidence, field.TypeOther, value)
		_node.RespondentEvidence = value
	}
	if value, ok := _c.mutation.AppealReason(); ok {
		_spec.SetField(disputes.FieldAppealReason, field.TypeString, value)
		_node.AppealReason = &value
	}
	if value, ok := _c.mutation.AppealedAt(); ok {
		_spec.SetField(disputes.FieldAppealedAt, field.TypeTime, value)
		_node.AppealedAt = &value
	}
	if value, ok := _c.mutation.ResolvedBy(); ok {
		_spec.SetField(disputes.FieldResolvedBy, field.TypeInt64, value)
		_node.ResolvedBy = &value
	}
	if value, ok := _c.mutation.ResolvedAt(); ok {
		_spec.SetField(disputes.FieldResolvedAt, field.TypeTime, value)
		_node.ResolvedAt = &value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(disputes.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(disputes.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// DisputesCreateBulk is the builder for creating many Disputes entities in bulk.
type DisputesCreateBulk struct {
	config
	err      error
	builders []*DisputesCreate
}

// Save creates the Disputes entities in the database.
func (_c *DisputesCreateBulk) Save(ctx context.Context) ([]*Disputes, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*Disputes, len(_c.builders))
	mutators := make([]Mutator, len(_c.builders))
	for i := range _c.builders {
		func(i int, root context.Context) {
			builder := _c.builders[i]
			builder.defaults()
			var mut Mutator = MutateFunc(func(ctx context.Context, m Mutation) (Value, error) {
				mutation, ok := m.(*DisputesMutation)
				if !ok {
					return nil, fmt.Errorf("unexpected mutation type %T", m)
				}
				if err := builder.check(); err != nil {
					return nil, err
				}
				builder.mutation = mutation
				var err error
				nodes[i], specs[i] = builder.createSpec()
				if i < len(mutators)-1 {
					_, err = mutators[i+1].Mutate(root, _c.builders[i+1].mutation)
				} else {
					spec := &sqlgraph.BatchCreateSpec{Nodes: specs}
					// Invoke the actual operation on the latest mutation in the chain.
					if err = sqlgraph.BatchCreate(ctx, _c.driver, spec); err != nil {
						if sqlgraph.IsConstraintError(err) {
							err = &ConstraintError{msg: err.Error(), wrap: err}
						}
					}
				}
				if err != nil {
					return nil, err
				}
				mutation.id = &nodes[i].ID
				if specs[i].ID.Value != nil && nodes[i].ID == 0 {
					id := specs[i].ID.Value.(int64)
					nodes[i].ID = int64(id)
				}
				mutation.done = true
				return nodes[i], nil
			})
			for i := len(builder.hooks) - 1; i >= 0; i-- {
				mut = builder.hooks[i](mut)
			}
			mutators[i] = mut
		}(i, ctx)
	}
	if len(mutators) > 0 {
		if _, err := mutators[0].Mutate(ctx, _c.builders[0].mutation); err != nil {
			return nil, err
		}
	}
	return nodes, nil
}

// SaveX is like Save, but panics if an error occurs.
func (_c *DisputesCreateBulk) SaveX(ctx context.Context) []*Disputes {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

// Exec executes the query.
func (_c *DisputesCreateBulk) Exec(ctx context.Context) error {
	_, err := _c.Save(ctx)
	return err
}

// ExecX is like Exec, but panics if an error occurs.
func (_c *DisputesCreateBulk) ExecX(ctx context.Context) {
	if err := _c.Exec(ctx); err != nil {
		panic(err)
	}
}
