package mysqlbenefactor

import (
	"context"
	"errors"

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

func (d *DB) IsExistBenefactorByPhoneNumber(ctx context.Context, phoneNumber string) (bool, entity.Benefactor, error) {
	const op = "mysqlbenefactor.IsExistBenefactorByPhoneNumber"

	bnf, err := d.GetByPhoneNumber(ctx, phoneNumber)
	if err != nil {
		var richErr richerror.RichError
		if errors.As(err, &richErr) && richErr.Kind() == richerror.KindNotFound {
			return false, entity.Benefactor{}, nil
		}

		return false, entity.Benefactor{}, richerror.New(op).WithErr(err)
	}

	return true, bnf, nil
}

func (d *DB) IsExistBenefactorByID(ctx context.Context, id uint) (bool, error) {
	const op = "mysqlbenefactor.IsExistBenefactorByID"

	_, err := d.GetByID(ctx, id)
	if err != nil {
		var richErr richerror.RichError
		if errors.As(err, &richErr) && richErr.Kind() == richerror.KindNotFound {
			return false, nil
		}

		return false, richerror.New(op).WithErr(err)
	}

	return true, nil
}
