package robokassa

import "testing"

func TestShopsShopRouting(t *testing.T) {
	shops := Shops{
		ChannelWeb:     {MerchantLogin: "webshop", Password1: "w1", Password2: "w2"},
		ChannelAndroid: {MerchantLogin: "androidshop", Password1: "a1", Password2: "a2"},
	}
	// Order side: the exact channel wins.
	if c, ok := shops.Shop(ChannelAndroid); !ok || c.MerchantLogin != "androidshop" {
		t.Errorf("Shop(android) = %q/%v, want androidshop/true", c.MerchantLogin, ok)
	}
	// Order side: an unknown or empty channel falls back to the web shop (safe — always credits direct).
	if c, ok := shops.Shop("ios"); !ok || c.MerchantLogin != "webshop" {
		t.Errorf("Shop(ios) = %q/%v, want webshop/true (web fallback)", c.MerchantLogin, ok)
	}
	if c, ok := shops.Shop(""); !ok || c.MerchantLogin != "webshop" {
		t.Errorf("Shop(empty) = %q/%v, want webshop/true (web fallback)", c.MerchantLogin, ok)
	}
	// No web shop and an unknown channel → unconfigured.
	if _, ok := (Shops{ChannelAndroid: {MerchantLogin: "androidshop", Password1: "a1", Password2: "a2"}}).Shop("ios"); ok {
		t.Error("Shop(ios) with no web shop returned ok, want false")
	}
}

func TestShopsVerifierIsStrict(t *testing.T) {
	shops := Shops{
		ChannelWeb:     {MerchantLogin: "webshop", Password1: "w1", Password2: "w2"},
		ChannelAndroid: {MerchantLogin: "androidshop", Password1: "a1", Password2: "a2"},
	}
	// Callback side: the exact channel's own credentials, no web fallback (each callback is verified
	// only by its own shop's Password2).
	if c, ok := shops.Verifier(ChannelAndroid); !ok || c.MerchantLogin != "androidshop" {
		t.Errorf("Verifier(android) = %q/%v, want androidshop/true", c.MerchantLogin, ok)
	}
	if _, ok := shops.Verifier("ios"); ok {
		t.Error("Verifier(ios) returned ok, want false (no fallback)")
	}
}

func TestShopsConfigured(t *testing.T) {
	if (Shops{}).Configured() {
		t.Error("an empty set reported configured")
	}
	if (Shops{ChannelWeb: {}}).Configured() {
		t.Error("a shop with no merchant login reported configured")
	}
	if !(Shops{ChannelWeb: {MerchantLogin: "s", Password1: "1", Password2: "2"}}).Configured() {
		t.Error("a configured shop reported not configured")
	}
}
