package ai

import "testing"

func TestApplyModelCatalogEmptyAllowsAll(t *testing.T) {
	upstream := []map[string]any{
		{"id": "gpt-4o", "object": "model"},
		{"id": "llama3", "object": "model"},
	}
	got := ApplyModelCatalog(upstream, nil)
	if len(got) != 2 {
		t.Fatalf("expected 2 models, got %d", len(got))
	}
}

func TestApplyModelCatalogAllowlist(t *testing.T) {
	upstream := []map[string]any{
		{"id": "gpt-4o", "object": "model", "owned_by": "OpenAI"},
		{"id": "llama3", "object": "model", "owned_by": "Local"},
	}
	catalog := []ModelCatalogEntry{
		{ModelID: "gpt-4o", Label: "GPT-4o", Enabled: true},
		{ModelID: "llama3", Label: "Llama 3", Enabled: false},
		{ModelID: "manual-model", Label: "Manuel", Enabled: true},
	}
	got := ApplyModelCatalog(upstream, catalog)
	if len(got) != 2 {
		t.Fatalf("expected 2 allowed models, got %d", len(got))
	}
	if got[0]["label"] != "GPT-4o" {
		t.Fatalf("expected label GPT-4o, got %v", got[0]["label"])
	}
	if got[1]["id"] != "manual-model" {
		t.Fatalf("expected manual-model, got %v", got[1]["id"])
	}
}

func TestIsModelAllowed(t *testing.T) {
	catalog := []ModelCatalogEntry{
		{ModelID: "gpt-4o", Enabled: true},
		{ModelID: "llama3", Enabled: false},
	}
	if !IsModelAllowed("gpt-4o", catalog) {
		t.Fatal("gpt-4o should be allowed")
	}
	if IsModelAllowed("llama3", catalog) {
		t.Fatal("llama3 should be blocked")
	}
	if IsModelAllowed("unknown", catalog) {
		t.Fatal("unknown model should be blocked when catalog is set")
	}
	if !IsModelAllowed("unknown", nil) {
		t.Fatal("unknown model should be allowed without catalog")
	}
}
