// Code generated by eevee. DO NOT EDIT!

package repository

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

	"golang.org/x/xerrors"
)

type Group interface {
	ToModel(*entity.Group) *model.Group
	ToModels(entity.Groups) *model.Groups
	Create(context.Context, *entity.Group) (*model.Group, error)
	Creates(context.Context, entity.Groups) (*model.Groups, error)
}

type GroupImpl struct {
	groupDAO dao.Group
	repo     Repository
}

func NewGroup(ctx context.Context, tx *sql.Tx) *GroupImpl {
	return &GroupImpl{groupDAO: dao.NewGroup(ctx, tx)}
}

func (r *GroupImpl) ToModel(value *entity.Group) *model.Group {
	return r.createCollection(entity.Groups{value}).First()
}

func (r *GroupImpl) ToModels(values entity.Groups) *model.Groups {
	return r.createCollection(values)
}

func (r *GroupImpl) Create(ctx context.Context, value *entity.Group) (*model.Group, error) {
	if err := r.groupDAO.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 *GroupImpl) Creates(ctx context.Context, entities entity.Groups) (*model.Groups, 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.Group) {
		v.SetSavedValue(v.Group)
		v.SetAlreadyCreated(true)
	})
	return values, nil
}

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

func (r *GroupImpl) create(entity *entity.Group, values *model.Groups) *model.Group {
	value := model.NewGroup(entity, r.groupDAO)
	value.SetConverter(r.repo.(model.ModelConverter))
	return value
}
