package service

import (
	"context"
	"testing"
)

// =============================================================================
// Request Metadata Context Tests
// =============================================================================

func TestRequestMetadataFallbackStats(t *testing.T) {
	isMaxTokens, thinking, prefetchAccount, prefetchGroup, singleAccount, accountSwitch := RequestMetadataFallbackStats()

	if isMaxTokens != 0 {
		t.Errorf("isMaxTokens = %d, want 0", isMaxTokens)
	}
	if thinking != 0 {
		t.Errorf("thinking = %d, want 0", thinking)
	}
	if prefetchAccount != 0 {
		t.Errorf("prefetchAccount = %d, want 0", prefetchAccount)
	}
	if prefetchGroup != 0 {
		t.Errorf("prefetchGroup = %d, want 0", prefetchGroup)
	}
	if singleAccount != 0 {
		t.Errorf("singleAccount = %d, want 0", singleAccount)
	}
	if accountSwitch != 0 {
		t.Errorf("accountSwitch = %d, want 0", accountSwitch)
	}
}

func TestWithIsMaxTokensOneHaikuRequest(t *testing.T) {
	ctx := context.Background()

	// Test setting true
	ctx1 := WithIsMaxTokensOneHaikuRequest(ctx, true, false)
	val, ok := IsMaxTokensOneHaikuRequestFromContext(ctx1)
	if !ok {
		t.Error("IsMaxTokensOneHaikuRequestFromContext returned !ok")
	}
	if val != true {
		t.Errorf("IsMaxTokensOneHaikuRequestFromContext = %v, want true", val)
	}

	// Test setting false
	ctx2 := WithIsMaxTokensOneHaikuRequest(ctx, false, false)
	val2, ok2 := IsMaxTokensOneHaikuRequestFromContext(ctx2)
	if !ok2 {
		t.Error("IsMaxTokensOneHaikuRequestFromContext returned !ok")
	}
	if val2 != false {
		t.Errorf("IsMaxTokensOneHaikuRequestFromContext = %v, want false", val2)
	}
}

func TestWithThinkingEnabled(t *testing.T) {
	ctx := context.Background()

	// Test setting true
	ctx1 := WithThinkingEnabled(ctx, true, false)
	val, ok := ThinkingEnabledFromContext(ctx1)
	if !ok {
		t.Error("ThinkingEnabledFromContext returned !ok")
	}
	if val != true {
		t.Errorf("ThinkingEnabledFromContext = %v, want true", val)
	}

	// Test setting false
	ctx2 := WithThinkingEnabled(ctx, false, false)
	val2, ok2 := ThinkingEnabledFromContext(ctx2)
	if !ok2 {
		t.Error("ThinkingEnabledFromContext returned !ok")
	}
	if val2 != false {
		t.Errorf("ThinkingEnabledFromContext = %v, want false", val2)
	}
}

func TestWithPrefetchedStickySession(t *testing.T) {
	ctx := context.Background()

	// Test setting values
	ctx1 := WithPrefetchedStickySession(ctx, 123, 456, false)
	accountID, ok := PrefetchedStickyAccountIDFromContext(ctx1)
	if !ok {
		t.Error("PrefetchedStickyAccountIDFromContext returned !ok")
	}
	if accountID != 123 {
		t.Errorf("PrefetchedStickyAccountIDFromContext = %d, want 123", accountID)
	}

	groupID, ok2 := PrefetchedStickyGroupIDFromContext(ctx1)
	if !ok2 {
		t.Error("PrefetchedStickyGroupIDFromContext returned !ok")
	}
	if groupID != 456 {
		t.Errorf("PrefetchedStickyGroupIDFromContext = %d, want 456", groupID)
	}
}

func TestWithSingleAccountRetry(t *testing.T) {
	ctx := context.Background()

	// Test setting true
	ctx1 := WithSingleAccountRetry(ctx, true, false)
	val, ok := SingleAccountRetryFromContext(ctx1)
	if !ok {
		t.Error("SingleAccountRetryFromContext returned !ok")
	}
	if val != true {
		t.Errorf("SingleAccountRetryFromContext = %v, want true", val)
	}
}

func TestWithAccountSwitchCount(t *testing.T) {
	ctx := context.Background()

	// Test setting count
	ctx1 := WithAccountSwitchCount(ctx, 5, false)
	val, ok := AccountSwitchCountFromContext(ctx1)
	if !ok {
		t.Error("AccountSwitchCountFromContext returned !ok")
	}
	if val != 5 {
		t.Errorf("AccountSwitchCountFromContext = %d, want 5", val)
	}
}

func TestContextDefaults(t *testing.T) {
	ctx := context.Background()

	// All context getters should return !ok for fresh context
	_, ok := IsMaxTokensOneHaikuRequestFromContext(ctx)
	if ok {
		t.Error("IsMaxTokensOneHaikuRequestFromContext returned ok for fresh context")
	}

	_, ok = ThinkingEnabledFromContext(ctx)
	if ok {
		t.Error("ThinkingEnabledFromContext returned ok for fresh context")
	}

	_, ok = PrefetchedStickyAccountIDFromContext(ctx)
	if ok {
		t.Error("PrefetchedStickyAccountIDFromContext returned ok for fresh context")
	}

	_, ok = PrefetchedStickyGroupIDFromContext(ctx)
	if ok {
		t.Error("PrefetchedStickyGroupIDFromContext returned ok for fresh context")
	}

	_, ok = SingleAccountRetryFromContext(ctx)
	if ok {
		t.Error("SingleAccountRetryFromContext returned ok for fresh context")
	}

	_, ok = AccountSwitchCountFromContext(ctx)
	if ok {
		t.Error("AccountSwitchCountFromContext returned ok for fresh context")
	}
}

func TestBridgeOldKeys(t *testing.T) {
	// Test that bridgeOldKeys=true allows setting values
	// even when old keys might already exist
	ctx := context.Background()
	ctx1 := WithIsMaxTokensOneHaikuRequest(ctx, true, true) // bridgeOldKeys=true
	val, ok := IsMaxTokensOneHaikuRequestFromContext(ctx1)
	if !ok {
		t.Error("IsMaxTokensOneHaikuRequestFromContext returned !ok with bridgeOldKeys=true")
	}
	if val != true {
		t.Errorf("IsMaxTokensOneHaikuRequestFromContext = %v, want true", val)
	}
}
