package controlplane

import (
	"encoding/json"
	"testing"
)

func TestDecodeModelsDeduplicatesAndRetainsMetadata(t *testing.T) {
	models, err := decodeModels([]byte(`{
        "object":"list",
        "data":[
            {"id":"gpt-5","owned_by":"openai","context_window":400000},
            {"id":"gpt-5","owned_by":"duplicate"},
            {"id":"text-embedding-3-small","owned_by":"openai"}
        ]
    }`))
	if err != nil {
		t.Fatal(err)
	}
	if len(models) != 2 || models[0].ProviderModelID != "gpt-5" || models[1].ProviderModelID != "text-embedding-3-small" {
		t.Fatalf("unexpected models: %#v", models)
	}
	var metadata map[string]any
	if err := json.Unmarshal(models[0].Metadata, &metadata); err != nil {
		t.Fatal(err)
	}
	if metadata["context_window"] != float64(400000) {
		t.Fatalf("metadata was not retained: %#v", metadata)
	}
}

func TestDecodeModelsRejectsMissingID(t *testing.T) {
	if _, err := decodeModels([]byte(`{"data":[{"owned_by":"openai"}]}`)); err == nil {
		t.Fatal("expected missing model id to be rejected")
	}
}
