// Code generated by ent, DO NOT EDIT.

package models

import (
	"context"
	"errors"
	"fmt"
	"juwan-backend/app/shop/rpc/internal/models/schema"
	"juwan-backend/app/shop/rpc/internal/models/shops"
	"time"

	"entgo.io/ent/dialect/sql/sqlgraph"
	"entgo.io/ent/schema/field"
	"github.com/shopspring/decimal"
)

// ShopsCreate is the builder for creating a Shops entity.
type ShopsCreate struct {
	config
	mutation *ShopsMutation
	hooks    []Hook
}

// SetOwnerID sets the "owner_id" field.
func (_c *ShopsCreate) SetOwnerID(v int64) *ShopsCreate {
	_c.mutation.SetOwnerID(v)
	return _c
}

// SetName sets the "name" field.
func (_c *ShopsCreate) SetName(v string) *ShopsCreate {
	_c.mutation.SetName(v)
	return _c
}

// SetBanner sets the "banner" field.
func (_c *ShopsCreate) SetBanner(v string) *ShopsCreate {
	_c.mutation.SetBanner(v)
	return _c
}

// SetNillableBanner sets the "banner" field if the given value is not nil.
func (_c *ShopsCreate) SetNillableBanner(v *string) *ShopsCreate {
	if v != nil {
		_c.SetBanner(*v)
	}
	return _c
}

// SetDescription sets the "description" field.
func (_c *ShopsCreate) SetDescription(v string) *ShopsCreate {
	_c.mutation.SetDescription(v)
	return _c
}

// SetNillableDescription sets the "description" field if the given value is not nil.
func (_c *ShopsCreate) SetNillableDescription(v *string) *ShopsCreate {
	if v != nil {
		_c.SetDescription(*v)
	}
	return _c
}

// SetRating sets the "rating" field.
func (_c *ShopsCreate) SetRating(v decimal.Decimal) *ShopsCreate {
	_c.mutation.SetRating(v)
	return _c
}

// SetNillableRating sets the "rating" field if the given value is not nil.
func (_c *ShopsCreate) SetNillableRating(v *decimal.Decimal) *ShopsCreate {
	if v != nil {
		_c.SetRating(*v)
	}
	return _c
}

// SetTotalOrders sets the "total_orders" field.
func (_c *ShopsCreate) SetTotalOrders(v int) *ShopsCreate {
	_c.mutation.SetTotalOrders(v)
	return _c
}

// SetNillableTotalOrders sets the "total_orders" field if the given value is not nil.
func (_c *ShopsCreate) SetNillableTotalOrders(v *int) *ShopsCreate {
	if v != nil {
		_c.SetTotalOrders(*v)
	}
	return _c
}

// SetPlayerCount sets the "player_count" field.
func (_c *ShopsCreate) SetPlayerCount(v int) *ShopsCreate {
	_c.mutation.SetPlayerCount(v)
	return _c
}

// SetNillablePlayerCount sets the "player_count" field if the given value is not nil.
func (_c *ShopsCreate) SetNillablePlayerCount(v *int) *ShopsCreate {
	if v != nil {
		_c.SetPlayerCount(*v)
	}
	return _c
}

// SetCommissionType sets the "commission_type" field.
func (_c *ShopsCreate) SetCommissionType(v string) *ShopsCreate {
	_c.mutation.SetCommissionType(v)
	return _c
}

// SetNillableCommissionType sets the "commission_type" field if the given value is not nil.
func (_c *ShopsCreate) SetNillableCommissionType(v *string) *ShopsCreate {
	if v != nil {
		_c.SetCommissionType(*v)
	}
	return _c
}

// SetCommissionValue sets the "commission_value" field.
func (_c *ShopsCreate) SetCommissionValue(v decimal.Decimal) *ShopsCreate {
	_c.mutation.SetCommissionValue(v)
	return _c
}

// SetAllowMultiShop sets the "allow_multi_shop" field.
func (_c *ShopsCreate) SetAllowMultiShop(v bool) *ShopsCreate {
	_c.mutation.SetAllowMultiShop(v)
	return _c
}

// SetNillableAllowMultiShop sets the "allow_multi_shop" field if the given value is not nil.
func (_c *ShopsCreate) SetNillableAllowMultiShop(v *bool) *ShopsCreate {
	if v != nil {
		_c.SetAllowMultiShop(*v)
	}
	return _c
}

// SetAllowIndependentOrders sets the "allow_independent_orders" field.
func (_c *ShopsCreate) SetAllowIndependentOrders(v bool) *ShopsCreate {
	_c.mutation.SetAllowIndependentOrders(v)
	return _c
}

// SetNillableAllowIndependentOrders sets the "allow_independent_orders" field if the given value is not nil.
func (_c *ShopsCreate) SetNillableAllowIndependentOrders(v *bool) *ShopsCreate {
	if v != nil {
		_c.SetAllowIndependentOrders(*v)
	}
	return _c
}

// SetDispatchMode sets the "dispatch_mode" field.
func (_c *ShopsCreate) SetDispatchMode(v string) *ShopsCreate {
	_c.mutation.SetDispatchMode(v)
	return _c
}

// SetNillableDispatchMode sets the "dispatch_mode" field if the given value is not nil.
func (_c *ShopsCreate) SetNillableDispatchMode(v *string) *ShopsCreate {
	if v != nil {
		_c.SetDispatchMode(*v)
	}
	return _c
}

// SetAnnouncements sets the "announcements" field.
func (_c *ShopsCreate) SetAnnouncements(v schema.TextArray) *ShopsCreate {
	_c.mutation.SetAnnouncements(v)
	return _c
}

// SetNillableAnnouncements sets the "announcements" field if the given value is not nil.
func (_c *ShopsCreate) SetNillableAnnouncements(v *schema.TextArray) *ShopsCreate {
	if v != nil {
		_c.SetAnnouncements(*v)
	}
	return _c
}

// SetTemplateConfig sets the "template_config" field.
func (_c *ShopsCreate) SetTemplateConfig(v map[string]interface{}) *ShopsCreate {
	_c.mutation.SetTemplateConfig(v)
	return _c
}

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

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

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

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

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *ShopsCreate) 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 *ShopsCreate) defaults() {
	if _, ok := _c.mutation.Rating(); !ok {
		v := shops.DefaultRating
		_c.mutation.SetRating(v)
	}
	if _, ok := _c.mutation.TotalOrders(); !ok {
		v := shops.DefaultTotalOrders
		_c.mutation.SetTotalOrders(v)
	}
	if _, ok := _c.mutation.PlayerCount(); !ok {
		v := shops.DefaultPlayerCount
		_c.mutation.SetPlayerCount(v)
	}
	if _, ok := _c.mutation.CommissionType(); !ok {
		v := shops.DefaultCommissionType
		_c.mutation.SetCommissionType(v)
	}
	if _, ok := _c.mutation.AllowMultiShop(); !ok {
		v := shops.DefaultAllowMultiShop
		_c.mutation.SetAllowMultiShop(v)
	}
	if _, ok := _c.mutation.AllowIndependentOrders(); !ok {
		v := shops.DefaultAllowIndependentOrders
		_c.mutation.SetAllowIndependentOrders(v)
	}
	if _, ok := _c.mutation.DispatchMode(); !ok {
		v := shops.DefaultDispatchMode
		_c.mutation.SetDispatchMode(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := shops.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := shops.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *ShopsCreate) check() error {
	if _, ok := _c.mutation.OwnerID(); !ok {
		return &ValidationError{Name: "owner_id", err: errors.New(`models: missing required field "Shops.owner_id"`)}
	}
	if _, ok := _c.mutation.Name(); !ok {
		return &ValidationError{Name: "name", err: errors.New(`models: missing required field "Shops.name"`)}
	}
	if v, ok := _c.mutation.Name(); ok {
		if err := shops.NameValidator(v); err != nil {
			return &ValidationError{Name: "name", err: fmt.Errorf(`models: validator failed for field "Shops.name": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CommissionType(); !ok {
		return &ValidationError{Name: "commission_type", err: errors.New(`models: missing required field "Shops.commission_type"`)}
	}
	if v, ok := _c.mutation.CommissionType(); ok {
		if err := shops.CommissionTypeValidator(v); err != nil {
			return &ValidationError{Name: "commission_type", err: fmt.Errorf(`models: validator failed for field "Shops.commission_type": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CommissionValue(); !ok {
		return &ValidationError{Name: "commission_value", err: errors.New(`models: missing required field "Shops.commission_value"`)}
	}
	if _, ok := _c.mutation.DispatchMode(); !ok {
		return &ValidationError{Name: "dispatch_mode", err: errors.New(`models: missing required field "Shops.dispatch_mode"`)}
	}
	if v, ok := _c.mutation.DispatchMode(); ok {
		if err := shops.DispatchModeValidator(v); err != nil {
			return &ValidationError{Name: "dispatch_mode", err: fmt.Errorf(`models: validator failed for field "Shops.dispatch_mode": %w`, err)}
		}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "Shops.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "Shops.updated_at"`)}
	}
	return nil
}

func (_c *ShopsCreate) sqlSave(ctx context.Context) (*Shops, 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 *ShopsCreate) createSpec() (*Shops, *sqlgraph.CreateSpec) {
	var (
		_node = &Shops{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(shops.Table, sqlgraph.NewFieldSpec(shops.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.OwnerID(); ok {
		_spec.SetField(shops.FieldOwnerID, field.TypeInt64, value)
		_node.OwnerID = value
	}
	if value, ok := _c.mutation.Name(); ok {
		_spec.SetField(shops.FieldName, field.TypeString, value)
		_node.Name = value
	}
	if value, ok := _c.mutation.Banner(); ok {
		_spec.SetField(shops.FieldBanner, field.TypeString, value)
		_node.Banner = &value
	}
	if value, ok := _c.mutation.Description(); ok {
		_spec.SetField(shops.FieldDescription, field.TypeString, value)
		_node.Description = &value
	}
	if value, ok := _c.mutation.Rating(); ok {
		_spec.SetField(shops.FieldRating, field.TypeOther, value)
		_node.Rating = value
	}
	if value, ok := _c.mutation.TotalOrders(); ok {
		_spec.SetField(shops.FieldTotalOrders, field.TypeInt, value)
		_node.TotalOrders = value
	}
	if value, ok := _c.mutation.PlayerCount(); ok {
		_spec.SetField(shops.FieldPlayerCount, field.TypeInt, value)
		_node.PlayerCount = value
	}
	if value, ok := _c.mutation.CommissionType(); ok {
		_spec.SetField(shops.FieldCommissionType, field.TypeString, value)
		_node.CommissionType = value
	}
	if value, ok := _c.mutation.CommissionValue(); ok {
		_spec.SetField(shops.FieldCommissionValue, field.TypeOther, value)
		_node.CommissionValue = value
	}
	if value, ok := _c.mutation.AllowMultiShop(); ok {
		_spec.SetField(shops.FieldAllowMultiShop, field.TypeBool, value)
		_node.AllowMultiShop = value
	}
	if value, ok := _c.mutation.AllowIndependentOrders(); ok {
		_spec.SetField(shops.FieldAllowIndependentOrders, field.TypeBool, value)
		_node.AllowIndependentOrders = value
	}
	if value, ok := _c.mutation.DispatchMode(); ok {
		_spec.SetField(shops.FieldDispatchMode, field.TypeString, value)
		_node.DispatchMode = value
	}
	if value, ok := _c.mutation.Announcements(); ok {
		_spec.SetField(shops.FieldAnnouncements, field.TypeOther, value)
		_node.Announcements = value
	}
	if value, ok := _c.mutation.TemplateConfig(); ok {
		_spec.SetField(shops.FieldTemplateConfig, field.TypeJSON, value)
		_node.TemplateConfig = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(shops.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(shops.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	return _node, _spec
}

// ShopsCreateBulk is the builder for creating many Shops entities in bulk.
type ShopsCreateBulk struct {
	config
	err      error
	builders []*ShopsCreate
}

// Save creates the Shops entities in the database.
func (_c *ShopsCreateBulk) Save(ctx context.Context) ([]*Shops, error) {
	if _c.err != nil {
		return nil, _c.err
	}
	specs := make([]*sqlgraph.CreateSpec, len(_c.builders))
	nodes := make([]*Shops, 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.(*ShopsMutation)
				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 *ShopsCreateBulk) SaveX(ctx context.Context) []*Shops {
	v, err := _c.Save(ctx)
	if err != nil {
		panic(err)
	}
	return v
}

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

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