package avatar

import (
	"strings"
	"testing"
)

func TestURL_DiceBear(t *testing.T) {
	opts := DefaultOptions()
	u := URL(ProviderDiceBear, "test-bot", opts)

	if !strings.HasPrefix(u, "https://api.dicebear.com/9.x/bottts/png") {
		t.Errorf("unexpected DiceBear URL prefix: %s", u)
	}
	if !strings.Contains(u, "seed=test-bot") {
		t.Errorf("URL should contain seed=test-bot: %s", u)
	}
	if !strings.Contains(u, "size=256") {
		t.Errorf("URL should contain size=256: %s", u)
	}
}

func TestURL_DiceBear_CustomStyle(t *testing.T) {
	opts := DefaultOptions()
	opts.DiceBearStyle = DiceBearPixelArt
	u := URL(ProviderDiceBear, "my-agent", opts)

	if !strings.Contains(u, "/pixel-art/png") {
		t.Errorf("URL should use pixel-art style: %s", u)
	}
}

func TestURL_RoboHash(t *testing.T) {
	opts := DefaultOptions()
	u := URL(ProviderRoboHash, "robot-1", opts)

	if !strings.HasPrefix(u, "https://robohash.org/robot-1.png") {
		t.Errorf("unexpected RoboHash URL prefix: %s", u)
	}
	if !strings.Contains(u, "set=set1") {
		t.Errorf("URL should contain set=set1: %s", u)
	}
	if !strings.Contains(u, "size=256x256") {
		t.Errorf("URL should contain size=256x256: %s", u)
	}
}

func TestURL_RoboHash_CustomSet(t *testing.T) {
	opts := DefaultOptions()
	opts.RoboHashSet = RoboHashCats
	u := URL(ProviderRoboHash, "cat-bot", opts)

	if !strings.Contains(u, "set=set4") {
		t.Errorf("URL should contain set=set4 for cats: %s", u)
	}
}

func TestURL_Multiavatar(t *testing.T) {
	u := URL(ProviderMultiavatar, "multi-bot", DefaultOptions())

	if !strings.HasPrefix(u, "https://api.multiavatar.com/multi-bot.png") {
		t.Errorf("unexpected Multiavatar URL: %s", u)
	}
}

func TestURL_CustomSize(t *testing.T) {
	opts := DefaultOptions()
	opts.Size = 512
	u := URL(ProviderDiceBear, "big-bot", opts)

	if !strings.Contains(u, "size=512") {
		t.Errorf("URL should contain size=512: %s", u)
	}
}

func TestURL_ZeroSize_DefaultsTo256(t *testing.T) {
	opts := Options{Size: 0}
	u := URL(ProviderDiceBear, "zero", opts)

	if !strings.Contains(u, "size=256") {
		t.Errorf("zero size should default to 256: %s", u)
	}
}

func TestURL_UnknownProvider_FallsToDiceBear(t *testing.T) {
	u := URL(Provider("unknown"), "fallback", DefaultOptions())

	if !strings.HasPrefix(u, "https://api.dicebear.com/") {
		t.Errorf("unknown provider should fall back to DiceBear: %s", u)
	}
}

func TestURL_SpecialCharsInSeed(t *testing.T) {
	u := URL(ProviderDiceBear, "bot with spaces", DefaultOptions())

	if strings.Contains(u, " ") {
		t.Errorf("URL should not contain raw spaces: %s", u)
	}
}

func TestAllProviders(t *testing.T) {
	providers := AllProviders()
	if len(providers) != 3 {
		t.Errorf("expected 3 providers, got %d", len(providers))
	}
}

func TestDefaultOptions(t *testing.T) {
	opts := DefaultOptions()
	if opts.Size != 256 {
		t.Errorf("expected default size 256, got %d", opts.Size)
	}
	if opts.DiceBearStyle != DiceBearBottts {
		t.Errorf("expected default style bottts, got %s", opts.DiceBearStyle)
	}
	if opts.RoboHashSet != RoboHashRobots {
		t.Errorf("expected default set set1, got %s", opts.RoboHashSet)
	}
}

func TestURL_Deterministic(t *testing.T) {
	opts := DefaultOptions()
	u1 := URL(ProviderDiceBear, "same-seed", opts)
	u2 := URL(ProviderDiceBear, "same-seed", opts)

	if u1 != u2 {
		t.Errorf("same seed should produce same URL:\n  %s\n  %s", u1, u2)
	}
}

func TestURL_DifferentSeeds_DifferentURLs(t *testing.T) {
	opts := DefaultOptions()
	u1 := URL(ProviderDiceBear, "bot-a", opts)
	u2 := URL(ProviderDiceBear, "bot-b", opts)

	if u1 == u2 {
		t.Error("different seeds should produce different URLs")
	}
}
