package dao

import (
	"context"
	"rapidashplugin/entity"

	"go.knocknote.io/rapidash"
	"golang.org/x/xerrors"
)

type UserField interface {
	Count(context.Context) (int64, error)
	Create(context.Context, *entity.UserField) error
	Delete(context.Context, *entity.UserField) error
	DeleteByID(context.Context, uint64) error
	DeleteByIDs(context.Context, []uint64) error
	DeleteByUserIDAndFieldID(context.Context, uint64, uint64) error
	FindAll(context.Context) (entity.UserFields, error)
	FindByID(context.Context, uint64) (*entity.UserField, error)
	FindByIDs(context.Context, []uint64) (entity.UserFields, error)
	FindByUserID(context.Context, uint64) (entity.UserFields, error)
	FindByUserIDAndFieldID(context.Context, uint64, uint64) (*entity.UserField, error)
	FindByUserIDs(context.Context, []uint64) (entity.UserFields, error)
	Update(context.Context, *entity.UserField) error
	UpdateByID(context.Context, uint64, map[string]interface{}) error
	UpdateByIDs(context.Context, []uint64, map[string]interface{}) error
	UpdateByUserIDAndFieldID(context.Context, uint64, uint64, map[string]interface{}) error
}

type UserFieldImpl struct {
	tx *rapidash.Tx
}

func NewUserField(ctx context.Context, tx *rapidash.Tx) UserField {
	return &UserFieldImpl{tx: tx}
}

// generated by eevee
func (d *UserFieldImpl) Count(ctx context.Context) (r int64, e error) {
	builder := rapidash.NewQueryBuilder("user_fields")
	count, err := d.tx.CountByQueryBuilderContext(ctx, builder)
	if err != nil {
		return 0, xerrors.Errorf("failed to Count: %w", err)
	}
	return int64(count), nil
}

// generated by eevee
func (d *UserFieldImpl) Create(ctx context.Context, value *entity.UserField) (e error) {
	id, err := d.tx.CreateByTableContext(ctx, "user_fields", value)
	if err != nil {
		return xerrors.Errorf("failed to Create: %w", err)
	}
	value.ID = uint64(id)
	return nil
}

// generated by eevee
func (d *UserFieldImpl) Delete(ctx context.Context, value *entity.UserField) (e error) {
	builder := rapidash.NewQueryBuilder("user_fields").Eq("id", value.ID)
	if err := d.tx.DeleteByQueryBuilderContext(ctx, builder); err != nil {
		return xerrors.Errorf("failed to Delete: %w", err)
	}
	return nil
}

// generated by eevee
func (d *UserFieldImpl) DeleteByID(ctx context.Context, a0 uint64) (e error) {
	builder := rapidash.NewQueryBuilder("user_fields").Eq("id", a0)
	if err := d.tx.DeleteByQueryBuilderContext(ctx, builder); err != nil {
		return xerrors.Errorf("failed to Delete: %w", err)
	}
	return nil
}

// generated by eevee
func (d *UserFieldImpl) DeleteByIDs(ctx context.Context, a0 []uint64) (e error) {
	builder := rapidash.NewQueryBuilder("user_fields").In("id", a0)
	if err := d.tx.DeleteByQueryBuilderContext(ctx, builder); err != nil {
		return xerrors.Errorf("failed to Delete: %w", err)
	}
	return nil
}

// generated by eevee
func (d *UserFieldImpl) DeleteByUserIDAndFieldID(ctx context.Context, a0 uint64, a1 uint64) (e error) {
	builder := rapidash.NewQueryBuilder("user_fields").Eq("user_id", a0).Eq("field_id", a1)
	if err := d.tx.DeleteByQueryBuilderContext(ctx, builder); err != nil {
		return xerrors.Errorf("failed to Delete: %w", err)
	}
	return nil
}

// generated by eevee
func (d *UserFieldImpl) FindAll(ctx context.Context) (r entity.UserFields, e error) {
	values := entity.UserFields{}
	if err := d.tx.FindAllByTable("user_fields", &values); err != nil {
		return values, xerrors.Errorf("failed to FindAll %w", err)
	}
	return values, nil
}

// generated by eevee
func (d *UserFieldImpl) FindByID(ctx context.Context, a0 uint64) (r *entity.UserField, e error) {
	builder := rapidash.NewQueryBuilder("user_fields").Eq("id", a0)
	var value entity.UserField
	if err := d.tx.FindByQueryBuilderContext(ctx, builder, &value); err != nil {
		return nil, xerrors.Errorf("failed to Find: %w", err)
	}
	return &value, nil
}

// generated by eevee
func (d *UserFieldImpl) FindByIDs(ctx context.Context, a0 []uint64) (r entity.UserFields, e error) {
	builder := rapidash.NewQueryBuilder("user_fields").In("id", a0)
	values := entity.UserFields{}
	if err := d.tx.FindByQueryBuilderContext(ctx, builder, &values); err != nil {
		return nil, xerrors.Errorf("failed to Find: %w", err)
	}
	return values, nil
}

// generated by eevee
func (d *UserFieldImpl) FindByUserID(ctx context.Context, a0 uint64) (r entity.UserFields, e error) {
	builder := rapidash.NewQueryBuilder("user_fields").Eq("user_id", a0)
	var values entity.UserFields
	if err := d.tx.FindByQueryBuilderContext(ctx, builder, &values); err != nil {
		return nil, xerrors.Errorf("failed to Find: %w", err)
	}
	return values, nil
}

// generated by eevee
func (d *UserFieldImpl) FindByUserIDAndFieldID(ctx context.Context, a0 uint64, a1 uint64) (r *entity.UserField, e error) {
	builder := rapidash.NewQueryBuilder("user_fields").Eq("user_id", a0).Eq("field_id", a1)
	var value entity.UserField
	if err := d.tx.FindByQueryBuilderContext(ctx, builder, &value); err != nil {
		return nil, xerrors.Errorf("failed to Find: %w", err)
	}
	return &value, nil
}

// generated by eevee
func (d *UserFieldImpl) FindByUserIDs(ctx context.Context, a0 []uint64) (r entity.UserFields, e error) {
	builder := rapidash.NewQueryBuilder("user_fields").In("user_id", a0)
	values := entity.UserFields{}
	if err := d.tx.FindByQueryBuilderContext(ctx, builder, &values); err != nil {
		return nil, xerrors.Errorf("failed to Find: %w", err)
	}
	return values, nil
}

// generated by eevee
func (d *UserFieldImpl) Update(ctx context.Context, value *entity.UserField) (e error) {
	updateMap := map[string]interface{}{
		"created_at": value.CreatedAt,
		"field_id":   value.FieldID,
		"updated_at": value.UpdatedAt,
		"user_id":    value.UserID,
	}
	builder := rapidash.NewQueryBuilder("user_fields").Eq("id", value.ID)
	if err := d.tx.UpdateByQueryBuilderContext(ctx, builder, updateMap); err != nil {
		return xerrors.Errorf("failed to Update: %w", err)
	}
	return nil
}

// generated by eevee
func (d *UserFieldImpl) UpdateByID(ctx context.Context, a0 uint64, updateMap map[string]interface{}) (e error) {
	builder := rapidash.NewQueryBuilder("user_fields").Eq("id", a0)
	if err := d.tx.UpdateByQueryBuilderContext(ctx, builder, updateMap); err != nil {
		return xerrors.Errorf("failed to Update: %w", err)
	}
	return nil
}

// generated by eevee
func (d *UserFieldImpl) UpdateByIDs(ctx context.Context, a0 []uint64, updateMap map[string]interface{}) (e error) {
	builder := rapidash.NewQueryBuilder("user_fields").In("id", a0)
	if err := d.tx.UpdateByQueryBuilderContext(ctx, builder, updateMap); err != nil {
		return xerrors.Errorf("failed to Update: %w", err)
	}
	return nil
}

// generated by eevee
func (d *UserFieldImpl) UpdateByUserIDAndFieldID(ctx context.Context, a0 uint64, a1 uint64, updateMap map[string]interface{}) (e error) {
	builder := rapidash.NewQueryBuilder("user_fields").Eq("user_id", a0).Eq("field_id", a1)
	if err := d.tx.UpdateByQueryBuilderContext(ctx, builder, updateMap); err != nil {
		return xerrors.Errorf("failed to Update: %w", err)
	}
	return nil
}
