package runner

import (
	"errors"
	"testing"

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

func TestFilterCandidatesByRequestedPlatform(t *testing.T) {
	candidates := []store.RuntimeModelCandidate{
		{PlatformID: "platform-a", PlatformModelID: "model-a"},
		{PlatformID: "platform-b", PlatformModelID: "model-b"},
	}

	t.Run("without selection", func(t *testing.T) {
		filtered, err := filterCandidatesByRequestedPlatform(candidates, map[string]any{})
		if err != nil || len(filtered) != len(candidates) {
			t.Fatalf("unexpected unfiltered candidates: %+v err=%v", filtered, err)
		}
	})

	t.Run("snake case platform id", func(t *testing.T) {
		filtered, err := filterCandidatesByRequestedPlatform(candidates, map[string]any{
			"platform_id": "platform-b",
		})
		if err != nil || len(filtered) != 1 || filtered[0].PlatformID != "platform-b" {
			t.Fatalf("unexpected platform filter result: %+v err=%v", filtered, err)
		}
	})

	t.Run("camel case platform model id", func(t *testing.T) {
		filtered, err := filterCandidatesByRequestedPlatform(candidates, map[string]any{
			"platformModelId": "model-a",
		})
		if err != nil || len(filtered) != 1 || filtered[0].PlatformModelID != "model-a" {
			t.Fatalf("unexpected platform model filter result: %+v err=%v", filtered, err)
		}
	})

	t.Run("selection must be accessible and enabled", func(t *testing.T) {
		filtered, err := filterCandidatesByRequestedPlatform(candidates, map[string]any{
			"platformId": "platform-missing",
		})
		if len(filtered) != 0 || !errors.Is(err, store.ErrNoModelCandidate) {
			t.Fatalf("unexpected unavailable platform result: %+v err=%v", filtered, err)
		}
	})
}
