package queue_test

import (
	"os"
	"path/filepath"
	"testing"
	"time"

	"github.com/google/uuid"
	"github.com/jfraeys/fetch_ml/internal/queue"
)

func TestBackendFallbackToFilesystemWhenRedisUnavailable(t *testing.T) {
	root := t.TempDir()
	fsPath := filepath.Join(root, "queue-fs")

	b, err := queue.NewBackend(queue.BackendConfig{
		Backend:              queue.QueueBackendRedis,
		RedisAddr:            "127.0.0.1:0",
		FallbackToFilesystem: true,
		FilesystemPath:       fsPath,
	})
	if err != nil {
		t.Fatalf("expected fallback backend, got error: %v", err)
	}
	if _, ok := b.(*queue.FilesystemQueue); !ok {
		t.Fatalf("expected filesystem backend, got %T", b)
	}

	task := &queue.Task{
		ID:        uuid.New().String(),
		JobName:   "job-1",
		Args:      "--epochs 1",
		Status:    "queued",
		Priority:  5,
		CreatedAt: time.Now().UTC(),
		UserID:    "local",
		Username:  "local",
		CreatedBy: "local",
		Metadata:  map[string]string{"commit_id": "deadbeef"},
	}
	if err := b.AddTask(task); err != nil {
		t.Fatalf("add task: %v", err)
	}

	p := filepath.Join(fsPath, "pending", "entries", task.ID+".json")
	if _, err := os.Stat(p); err != nil {
		t.Fatalf("expected pending task file %s to exist: %v", p, err)
	}
}

func TestBackendFilesystemDirect(t *testing.T) {
	root := t.TempDir()
	fsPath := filepath.Join(root, "queue-fs")

	b, err := queue.NewBackend(queue.BackendConfig{
		Backend:        queue.QueueBackendFS,
		FilesystemPath: fsPath,
	})
	if err != nil {
		t.Fatalf("expected filesystem backend, got error: %v", err)
	}
	if _, ok := b.(*queue.FilesystemQueue); !ok {
		t.Fatalf("expected filesystem backend, got %T", b)
	}
}
