package notification

import (
	"context"

	"github.com/google/uuid"
	"go.uber.org/zap"
)

// OnUserDeleted is the user-side soft-delete cascade hook. It marks
// every pending or retrying route owned by userID as `skipped` so the
// worker stops trying to deliver to a vanished account; published
// rows stay intact as audit trail.
//
// The catalog (`backend/README.md` §10) does not include a
// `user.*` kind, so this is the only place where the notification
// module reacts to user lifecycle events directly. The cascade is
// idempotent — repeated invocations on the same user simply find no
// pending rows.
func (s *Service) OnUserDeleted(ctx context.Context, userID uuid.UUID) error {
	if userID == uuid.Nil {
		return nil
	}
	skipped, err := s.deps.Store.SkipPendingRoutesForUser(ctx, userID, s.nowUTC())
	if err != nil {
		return err
	}
	if skipped > 0 {
		s.deps.Logger.Info("notification routes skipped on user delete",
			zap.String("user_id", userID.String()),
			zap.Int64("count", skipped),
		)
	}
	return nil
}
