// Code generated by eevee. DO NOT EDIT!

package repository

import (
	"context"
	"database/sql"
	"simple/dao"
	"simple/entity"
	"simple/model"

	"golang.org/x/xerrors"
)

type User interface {
	ToModel(*entity.User) *model.User
	ToModels(entity.Users) *model.Users
	Create(context.Context, *entity.User) (*model.User, error)
	Creates(context.Context, entity.Users) (*model.Users, error)
	FindAll(context.Context) (*model.Users, error)
	FindByID(context.Context, uint64) (*model.User, error)
	FindByIDs(context.Context, []uint64) (*model.Users, error)
	UpdateByID(context.Context, uint64, map[string]interface{}) error
	UpdateByIDs(context.Context, []uint64, map[string]interface{}) error
	DeleteByID(context.Context, uint64) error
	DeleteByIDs(context.Context, []uint64) error
	Count(context.Context) (int64, error)
	Delete(context.Context, *entity.User) error
	Update(context.Context, *entity.User) error
}

type UserImpl struct {
	userDAO dao.User
	repo    Repository
}

func NewUser(ctx context.Context, tx *sql.Tx) *UserImpl {
	return &UserImpl{userDAO: dao.NewUser(ctx, tx)}
}

func (r *UserImpl) ToModel(value *entity.User) *model.User {
	return r.createCollection(entity.Users{value}).First()
}

func (r *UserImpl) ToModels(values entity.Users) *model.Users {
	return r.createCollection(values)
}

func (r *UserImpl) Create(ctx context.Context, value *entity.User) (*model.User, error) {
	if err := r.userDAO.Create(ctx, value); err != nil {
		return nil, xerrors.Errorf("cannot Create: %w", err)
	}
	v := r.ToModel(value)
	v.SetSavedValue(value)
	v.SetAlreadyCreated(true)
	return v, nil
}

func (r *UserImpl) Creates(ctx context.Context, entities entity.Users) (*model.Users, error) {
	for _, v := range entities {
		if _, err := r.Create(ctx, v); err != nil {
			return nil, xerrors.Errorf("cannot Create: %w", err)
		}
	}
	values := r.ToModels(entities)
	values.Each(func(v *model.User) {
		v.SetSavedValue(v.User)
		v.SetAlreadyCreated(true)
	})
	return values, nil
}

func (r *UserImpl) FindAll(a0 context.Context) (*model.Users, error) {
	values, err := r.userDAO.FindAll(a0)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindAll: %w", err)
	}
	collection := r.createCollection(values)
	collection.Each(func(v *model.User) {
		v.SetSavedValue(v.User)
		v.SetAlreadyCreated(true)
	})
	return collection, nil
}

func (r *UserImpl) FindByID(a0 context.Context, a1 uint64) (*model.User, error) {
	value, err := r.userDAO.FindByID(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByID: %w", err)
	}
	if value == nil {
		return nil, nil
	}
	v := r.createCollection(entity.Users{value}).First()
	v.SetSavedValue(v.User)
	v.SetAlreadyCreated(true)
	return v, nil
}

func (r *UserImpl) FindByIDs(a0 context.Context, a1 []uint64) (*model.Users, error) {
	values, err := r.userDAO.FindByIDs(a0, a1)
	if err != nil {
		return nil, xerrors.Errorf("failed to FindByIDs: %w", err)
	}
	collection := r.createCollection(values)
	collection.Each(func(v *model.User) {
		v.SetSavedValue(v.User)
		v.SetAlreadyCreated(true)
	})
	return collection, nil
}

func (r *UserImpl) UpdateByID(a0 context.Context, a1 uint64, a2 map[string]interface{}) error {
	if err := r.userDAO.UpdateByID(a0, a1, a2); err != nil {
		return xerrors.Errorf("failed to update: %w", err)
	}
	return nil
}

func (r *UserImpl) UpdateByIDs(a0 context.Context, a1 []uint64, a2 map[string]interface{}) error {
	if err := r.userDAO.UpdateByIDs(a0, a1, a2); err != nil {
		return xerrors.Errorf("failed to update: %w", err)
	}
	return nil
}

func (r *UserImpl) DeleteByID(a0 context.Context, a1 uint64) error {
	if err := r.userDAO.DeleteByID(a0, a1); err != nil {
		return xerrors.Errorf("failed to delete: %w", err)
	}
	return nil
}

func (r *UserImpl) DeleteByIDs(a0 context.Context, a1 []uint64) error {
	if err := r.userDAO.DeleteByIDs(a0, a1); err != nil {
		return xerrors.Errorf("failed to delete: %w", err)
	}
	return nil
}

func (r *UserImpl) Count(a0 context.Context) (r0 int64, r1 error) {
	r0, r1 = r.userDAO.Count(a0)
	if r1 != nil {
		r1 = xerrors.Errorf("failed to Count: %w", r1)
	}
	return
}

func (r *UserImpl) Delete(a0 context.Context, a1 *entity.User) (r0 error) {
	r0 = r.userDAO.Delete(a0, a1)
	if r0 != nil {
		r0 = xerrors.Errorf("failed to Delete: %w", r0)
	}
	return
}

func (r *UserImpl) Update(a0 context.Context, a1 *entity.User) (r0 error) {
	r0 = r.userDAO.Update(a0, a1)
	if r0 != nil {
		r0 = xerrors.Errorf("failed to Update: %w", r0)
	}
	return
}

func (r *UserImpl) createCollection(entities entity.Users) *model.Users {
	values := model.NewUsers(entities)
	for i := 0; i < len(entities); i += 1 {
		values.Add(r.create(entities[i], values))
	}
	return values
}

func (r *UserImpl) create(entity *entity.User, values *model.Users) *model.User {
	value := model.NewUser(entity, r.userDAO)
	value.SetConverter(r.repo.(model.ModelConverter))
	return value
}
