package providers

import (
	"regexp"
	"testing"
)

// expectedTier1 lists all 12 Tier 1 Frontier provider names (PROV-01).
var expectedTier1 = []string{
	"openai", "anthropic", "google-ai", "vertex-ai", "aws-bedrock",
	"azure-openai", "meta-ai", "xai", "cohere", "mistral",
	"inflection", "ai21",
}

// expectedTier2 lists all 14 Tier 2 Inference Platform provider names (PROV-02).
var expectedTier2 = []string{
	"groq", "replicate", "anyscale", "together", "fireworks",
	"baseten", "deepinfra", "lepton", "modal", "cerebrium",
	"novita", "sambanova", "octoai", "friendli",
}

func TestTier1Count(t *testing.T) {
	reg, err := NewRegistry()
	if err != nil {
		t.Fatalf("NewRegistry failed: %v", err)
	}
	stats := reg.Stats()
	if got := stats.ByTier[1]; got != 12 {
		t.Errorf("Tier 1 count = %d, want 12", got)
	}
}

func TestTier2Count(t *testing.T) {
	reg, err := NewRegistry()
	if err != nil {
		t.Fatalf("NewRegistry failed: %v", err)
	}
	stats := reg.Stats()
	if got := stats.ByTier[2]; got != 14 {
		t.Errorf("Tier 2 count = %d, want 14", got)
	}
}

func TestAllPatternsCompile(t *testing.T) {
	reg, err := NewRegistry()
	if err != nil {
		t.Fatalf("NewRegistry failed: %v", err)
	}
	for _, p := range reg.List() {
		for i, pat := range p.Patterns {
			if _, err := regexp.Compile(pat.Regex); err != nil {
				t.Errorf("provider %q pattern[%d] regex %q failed to compile: %v",
					p.Name, i, pat.Regex, err)
			}
		}
	}
}

func TestAllProvidersHaveKeywords(t *testing.T) {
	reg, err := NewRegistry()
	if err != nil {
		t.Fatalf("NewRegistry failed: %v", err)
	}
	for _, p := range reg.List() {
		if len(p.Keywords) == 0 {
			t.Errorf("provider %q has no keywords (breaks AC pre-filter)", p.Name)
		}
	}
}

func TestTier1ProviderNames(t *testing.T) {
	reg, err := NewRegistry()
	if err != nil {
		t.Fatalf("NewRegistry failed: %v", err)
	}
	for _, name := range expectedTier1 {
		p, ok := reg.Get(name)
		if !ok {
			t.Errorf("Tier 1 provider %q not found in registry", name)
			continue
		}
		if p.Tier != 1 {
			t.Errorf("provider %q tier = %d, want 1", name, p.Tier)
		}
	}
}

func TestTier2ProviderNames(t *testing.T) {
	reg, err := NewRegistry()
	if err != nil {
		t.Fatalf("NewRegistry failed: %v", err)
	}
	for _, name := range expectedTier2 {
		p, ok := reg.Get(name)
		if !ok {
			t.Errorf("Tier 2 provider %q not found in registry", name)
			continue
		}
		if p.Tier != 2 {
			t.Errorf("provider %q tier = %d, want 2", name, p.Tier)
		}
	}
}
