package runner

import (
	"testing"
	"time"

	"github.com/easyai/easyai-ai-gateway/apps/api/internal/store"
)

func TestAsyncTaskRecoveryInsertOptsMakesDueTaskImmediatelyAvailable(t *testing.T) {
	now := time.Date(2026, time.July, 21, 3, 30, 0, 0, time.UTC)
	opts := asyncTaskRecoveryInsertOpts(store.AsyncTaskQueueItem{
		ID:        "due-task",
		NextRunAt: now.Add(-time.Second),
	}, now)

	if !opts.ScheduledAt.IsZero() {
		t.Fatalf("due recovery task should be immediately available, scheduled at %s", opts.ScheduledAt)
	}
}

func TestAsyncTaskRecoveryInsertOptsPreservesFutureDelay(t *testing.T) {
	now := time.Date(2026, time.July, 21, 3, 30, 0, 0, time.UTC)
	nextRunAt := now.Add(30 * time.Second)
	opts := asyncTaskRecoveryInsertOpts(store.AsyncTaskQueueItem{
		ID:        "delayed-task",
		NextRunAt: nextRunAt,
	}, now)

	if !opts.ScheduledAt.Equal(nextRunAt) {
		t.Fatalf("future recovery task should stay delayed until %s, got %s", nextRunAt, opts.ScheduledAt)
	}
}
