// Code generated by ent, DO NOT EDIT.

package models

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

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

// PostsCreate is the builder for creating a Posts entity.
type PostsCreate struct {
	config
	mutation *PostsMutation
	hooks    []Hook
}

// SetAuthorID sets the "author_id" field.
func (_c *PostsCreate) SetAuthorID(v int64) *PostsCreate {
	_c.mutation.SetAuthorID(v)
	return _c
}

// SetAuthorRole sets the "author_role" field.
func (_c *PostsCreate) SetAuthorRole(v string) *PostsCreate {
	_c.mutation.SetAuthorRole(v)
	return _c
}

// SetNillableAuthorRole sets the "author_role" field if the given value is not nil.
func (_c *PostsCreate) SetNillableAuthorRole(v *string) *PostsCreate {
	if v != nil {
		_c.SetAuthorRole(*v)
	}
	return _c
}

// SetTitle sets the "title" field.
func (_c *PostsCreate) SetTitle(v string) *PostsCreate {
	_c.mutation.SetTitle(v)
	return _c
}

// SetContent sets the "content" field.
func (_c *PostsCreate) SetContent(v string) *PostsCreate {
	_c.mutation.SetContent(v)
	return _c
}

// SetImages sets the "images" field.
func (_c *PostsCreate) SetImages(v types.TextArray) *PostsCreate {
	_c.mutation.SetImages(v)
	return _c
}

// SetNillableImages sets the "images" field if the given value is not nil.
func (_c *PostsCreate) SetNillableImages(v *types.TextArray) *PostsCreate {
	if v != nil {
		_c.SetImages(*v)
	}
	return _c
}

// SetTags sets the "tags" field.
func (_c *PostsCreate) SetTags(v types.TextArray) *PostsCreate {
	_c.mutation.SetTags(v)
	return _c
}

// SetNillableTags sets the "tags" field if the given value is not nil.
func (_c *PostsCreate) SetNillableTags(v *types.TextArray) *PostsCreate {
	if v != nil {
		_c.SetTags(*v)
	}
	return _c
}

// SetLinkedOrderID sets the "linked_order_id" field.
func (_c *PostsCreate) SetLinkedOrderID(v int64) *PostsCreate {
	_c.mutation.SetLinkedOrderID(v)
	return _c
}

// SetNillableLinkedOrderID sets the "linked_order_id" field if the given value is not nil.
func (_c *PostsCreate) SetNillableLinkedOrderID(v *int64) *PostsCreate {
	if v != nil {
		_c.SetLinkedOrderID(*v)
	}
	return _c
}

// SetQuotedPostID sets the "quoted_post_id" field.
func (_c *PostsCreate) SetQuotedPostID(v int64) *PostsCreate {
	_c.mutation.SetQuotedPostID(v)
	return _c
}

// SetNillableQuotedPostID sets the "quoted_post_id" field if the given value is not nil.
func (_c *PostsCreate) SetNillableQuotedPostID(v *int64) *PostsCreate {
	if v != nil {
		_c.SetQuotedPostID(*v)
	}
	return _c
}

// SetLikeCount sets the "like_count" field.
func (_c *PostsCreate) SetLikeCount(v int) *PostsCreate {
	_c.mutation.SetLikeCount(v)
	return _c
}

// SetNillableLikeCount sets the "like_count" field if the given value is not nil.
func (_c *PostsCreate) SetNillableLikeCount(v *int) *PostsCreate {
	if v != nil {
		_c.SetLikeCount(*v)
	}
	return _c
}

// SetCommentCount sets the "comment_count" field.
func (_c *PostsCreate) SetCommentCount(v int) *PostsCreate {
	_c.mutation.SetCommentCount(v)
	return _c
}

// SetNillableCommentCount sets the "comment_count" field if the given value is not nil.
func (_c *PostsCreate) SetNillableCommentCount(v *int) *PostsCreate {
	if v != nil {
		_c.SetCommentCount(*v)
	}
	return _c
}

// SetPinned sets the "pinned" field.
func (_c *PostsCreate) SetPinned(v bool) *PostsCreate {
	_c.mutation.SetPinned(v)
	return _c
}

// SetNillablePinned sets the "pinned" field if the given value is not nil.
func (_c *PostsCreate) SetNillablePinned(v *bool) *PostsCreate {
	if v != nil {
		_c.SetPinned(*v)
	}
	return _c
}

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

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

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

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

// SetDeletedAt sets the "deleted_at" field.
func (_c *PostsCreate) SetDeletedAt(v time.Time) *PostsCreate {
	_c.mutation.SetDeletedAt(v)
	return _c
}

// SetNillableDeletedAt sets the "deleted_at" field if the given value is not nil.
func (_c *PostsCreate) SetNillableDeletedAt(v *time.Time) *PostsCreate {
	if v != nil {
		_c.SetDeletedAt(*v)
	}
	return _c
}

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

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

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

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

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

// ExecX is like Exec, but panics if an error occurs.
func (_c *PostsCreate) 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 *PostsCreate) defaults() {
	if _, ok := _c.mutation.AuthorRole(); !ok {
		v := posts.DefaultAuthorRole
		_c.mutation.SetAuthorRole(v)
	}
	if _, ok := _c.mutation.LikeCount(); !ok {
		v := posts.DefaultLikeCount
		_c.mutation.SetLikeCount(v)
	}
	if _, ok := _c.mutation.CommentCount(); !ok {
		v := posts.DefaultCommentCount
		_c.mutation.SetCommentCount(v)
	}
	if _, ok := _c.mutation.Pinned(); !ok {
		v := posts.DefaultPinned
		_c.mutation.SetPinned(v)
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		v := posts.DefaultCreatedAt()
		_c.mutation.SetCreatedAt(v)
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		v := posts.DefaultUpdatedAt()
		_c.mutation.SetUpdatedAt(v)
	}
}

// check runs all checks and user-defined validators on the builder.
func (_c *PostsCreate) check() error {
	if _, ok := _c.mutation.AuthorID(); !ok {
		return &ValidationError{Name: "author_id", err: errors.New(`models: missing required field "Posts.author_id"`)}
	}
	if _, ok := _c.mutation.AuthorRole(); !ok {
		return &ValidationError{Name: "author_role", err: errors.New(`models: missing required field "Posts.author_role"`)}
	}
	if v, ok := _c.mutation.AuthorRole(); ok {
		if err := posts.AuthorRoleValidator(v); err != nil {
			return &ValidationError{Name: "author_role", err: fmt.Errorf(`models: validator failed for field "Posts.author_role": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Title(); !ok {
		return &ValidationError{Name: "title", err: errors.New(`models: missing required field "Posts.title"`)}
	}
	if v, ok := _c.mutation.Title(); ok {
		if err := posts.TitleValidator(v); err != nil {
			return &ValidationError{Name: "title", err: fmt.Errorf(`models: validator failed for field "Posts.title": %w`, err)}
		}
	}
	if _, ok := _c.mutation.Content(); !ok {
		return &ValidationError{Name: "content", err: errors.New(`models: missing required field "Posts.content"`)}
	}
	if _, ok := _c.mutation.CreatedAt(); !ok {
		return &ValidationError{Name: "created_at", err: errors.New(`models: missing required field "Posts.created_at"`)}
	}
	if _, ok := _c.mutation.UpdatedAt(); !ok {
		return &ValidationError{Name: "updated_at", err: errors.New(`models: missing required field "Posts.updated_at"`)}
	}
	return nil
}

func (_c *PostsCreate) sqlSave(ctx context.Context) (*Posts, 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 *PostsCreate) createSpec() (*Posts, *sqlgraph.CreateSpec) {
	var (
		_node = &Posts{config: _c.config}
		_spec = sqlgraph.NewCreateSpec(posts.Table, sqlgraph.NewFieldSpec(posts.FieldID, field.TypeInt64))
	)
	if id, ok := _c.mutation.ID(); ok {
		_node.ID = id
		_spec.ID.Value = id
	}
	if value, ok := _c.mutation.AuthorID(); ok {
		_spec.SetField(posts.FieldAuthorID, field.TypeInt64, value)
		_node.AuthorID = value
	}
	if value, ok := _c.mutation.AuthorRole(); ok {
		_spec.SetField(posts.FieldAuthorRole, field.TypeString, value)
		_node.AuthorRole = value
	}
	if value, ok := _c.mutation.Title(); ok {
		_spec.SetField(posts.FieldTitle, field.TypeString, value)
		_node.Title = value
	}
	if value, ok := _c.mutation.Content(); ok {
		_spec.SetField(posts.FieldContent, field.TypeString, value)
		_node.Content = value
	}
	if value, ok := _c.mutation.Images(); ok {
		_spec.SetField(posts.FieldImages, field.TypeOther, value)
		_node.Images = value
	}
	if value, ok := _c.mutation.Tags(); ok {
		_spec.SetField(posts.FieldTags, field.TypeOther, value)
		_node.Tags = value
	}
	if value, ok := _c.mutation.LinkedOrderID(); ok {
		_spec.SetField(posts.FieldLinkedOrderID, field.TypeInt64, value)
		_node.LinkedOrderID = &value
	}
	if value, ok := _c.mutation.QuotedPostID(); ok {
		_spec.SetField(posts.FieldQuotedPostID, field.TypeInt64, value)
		_node.QuotedPostID = &value
	}
	if value, ok := _c.mutation.LikeCount(); ok {
		_spec.SetField(posts.FieldLikeCount, field.TypeInt, value)
		_node.LikeCount = value
	}
	if value, ok := _c.mutation.CommentCount(); ok {
		_spec.SetField(posts.FieldCommentCount, field.TypeInt, value)
		_node.CommentCount = value
	}
	if value, ok := _c.mutation.Pinned(); ok {
		_spec.SetField(posts.FieldPinned, field.TypeBool, value)
		_node.Pinned = value
	}
	if value, ok := _c.mutation.CreatedAt(); ok {
		_spec.SetField(posts.FieldCreatedAt, field.TypeTime, value)
		_node.CreatedAt = value
	}
	if value, ok := _c.mutation.UpdatedAt(); ok {
		_spec.SetField(posts.FieldUpdatedAt, field.TypeTime, value)
		_node.UpdatedAt = value
	}
	if value, ok := _c.mutation.DeletedAt(); ok {
		_spec.SetField(posts.FieldDeletedAt, field.TypeTime, value)
		_node.DeletedAt = &value
	}
	return _node, _spec
}

// PostsCreateBulk is the builder for creating many Posts entities in bulk.
type PostsCreateBulk struct {
	config
	err      error
	builders []*PostsCreate
}

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

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

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