package mysqlbenefactor

import (
	"context"

	"git.gocasts.ir/ebhomengo/niki/entity"
	errmsg "git.gocasts.ir/ebhomengo/niki/pkg/err_msg"
	richerror "git.gocasts.ir/ebhomengo/niki/pkg/rich_error"
)

func (d DB) CreateBenefactor(ctx context.Context, benefactor entity.Benefactor) (entity.Benefactor, error) {
	const op = "mysqlbenefactor.CreateBenefactor"

	res, err := d.conn.Conn().ExecContext(ctx, `insert into benefactors(phone_number) values(?)`,
		benefactor.PhoneNumber)
	if err != nil {
		return entity.Benefactor{}, richerror.New(op).WithErr(err).
			WithMessage(errmsg.ErrorMsgNotFound).WithKind(richerror.KindUnexpected)
	}

	//nolint
	// error is always nil
	id, _ := res.LastInsertId()
	benefactor.ID = uint(id)

	return benefactor, nil
}
