package jobs

import (
	"context"
	"log/slog"

	"github.com/riverqueue/river"
	"github.com/riverqueue/river/rivertype"
)

// SlogErrorHandler logs job errors and panics via the structured slog logger.
// Returning nil from HandleError and HandlePanic instructs river to follow its
// default retry schedule.
type SlogErrorHandler struct{}

func (*SlogErrorHandler) HandleError(
	ctx context.Context, job *rivertype.JobRow, err error,
) *river.ErrorHandlerResult {
	slog.Error("job error",
		"job_id", job.ID,
		"job_kind", job.Kind,
		"attempt", job.Attempt,
		"max_attempts", job.MaxAttempts,
		"err", err,
	)
	return nil
}

func (*SlogErrorHandler) HandlePanic(
	ctx context.Context, job *rivertype.JobRow, panicVal any, trace string,
) *river.ErrorHandlerResult {
	slog.Error("job panic",
		"job_id", job.ID,
		"job_kind", job.Kind,
		"panic", panicVal,
		"trace", trace,
	)
	return nil
}
