// codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/usecase/blockedemail/check.go
package blockedemail

import (
	"context"
	"strings"

	"go.uber.org/zap"

	"github.com/gocql/gocql"
	dom_blockedemail "codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/internal/domain/blockedemail"
	"codeberg.org/mapleopentech/monorepo/cloud/maplefile-backend/pkg/validation"
)

type CheckBlockedEmailUseCase interface {
	Execute(ctx context.Context, userID gocql.UUID, email string) (bool, error)
}

type checkBlockedEmailUseCaseImpl struct {
	logger *zap.Logger
	repo   dom_blockedemail.BlockedEmailRepository
}

func NewCheckBlockedEmailUseCase(
	logger *zap.Logger,
	repo dom_blockedemail.BlockedEmailRepository,
) CheckBlockedEmailUseCase {
	logger = logger.Named("CheckBlockedEmailUseCase")
	return &checkBlockedEmailUseCaseImpl{
		logger: logger,
		repo:   repo,
	}
}

func (uc *checkBlockedEmailUseCaseImpl) Execute(ctx context.Context, userID gocql.UUID, email string) (bool, error) {
	// Normalize email
	normalizedEmail := strings.ToLower(strings.TrimSpace(email))

	isBlocked, err := uc.repo.IsBlocked(ctx, userID, normalizedEmail)
	if err != nil {
		uc.logger.Error("Failed to check if email is blocked",
			zap.Any("error", err),
			zap.Any("user_id", userID),
			zap.String("email", validation.MaskEmail(normalizedEmail)))
		return false, err
	}

	uc.logger.Debug("Checked blocked status",
		zap.Any("user_id", userID),
		zap.String("email", validation.MaskEmail(normalizedEmail)),
		zap.Bool("is_blocked", isBlocked))

	return isBlocked, nil
}
