package usecase

import (
	"errors"
	"strings"

	errs "gateway/internal/library/errors"
	"gateway/internal/library/errors/code"
	"gateway/internal/model/notification"
)

var errb = errs.For(code.Notification)

func wrapRepoErr(err error, msg ...string) error {
	if err == nil {
		return nil
	}
	if errors.Is(err, notification.ErrNotFound) {
		return errb.ResNotFound("notification", "").WithCause(err)
	}
	if errors.Is(err, notification.ErrInvalidObjectID) {
		return errb.ResInvalidMeasureID("notification id").WithCause(err)
	}
	if errors.Is(err, notification.ErrDuplicateIdempotency) {
		return errb.ResAlreadyExist("notification idempotency key").WithCause(err)
	}
	if errors.Is(err, notification.ErrInvalidChannel) {
		return errb.InputInvalidFormat("invalid notification channel").WithCause(err)
	}
	if errors.Is(err, notification.ErrQuotaExceeded) {
		return errb.ResInsufficientQuota("notification quota exceeded").WithCause(err)
	}
	if e := errs.FromError(err); e != nil {
		return err
	}
	m := strings.TrimSpace(strings.Join(msg, " "))
	if m == "" {
		m = "notification repository error"
	}
	return errb.DBError(m).WithCause(err)
}

func wrapStoreErr(err error, msg ...string) error {
	if err == nil {
		return nil
	}
	if e := errs.FromError(err); e != nil {
		return err
	}
	m := strings.TrimSpace(strings.Join(msg, " "))
	if m == "" {
		m = "notification store error"
	}
	return errb.DBError(m).WithCause(err)
}
