package batch

import "testing"

func TestDecideReuse(t *testing.T) {
	t.Parallel()

	t.Run("active provider with covered family is reused", func(t *testing.T) {
		t.Parallel()

		decision := DecideReuse(ReuseInput{
			ProviderMatched:          true,
			ProviderID:               "api-deepseek-12345678",
			CanonicalModelFamilies:   []string{"kimi-k2.6"},
			MatchedAccountID:         101,
			MatchedAccountState:      MatchedAccountStateActive,
			ExistingProviderID:       "api-deepseek-12345678",
			ExistingAccessStatus:     AccessStatusActive,
			ExistingCanonicalFamilys: []string{"kimi 2.6"},
		})

		if !decision.ProvisionReused {
			t.Fatal("ProvisionReused = false, want true")
		}
		if decision.AccountResolution != AccountResolutionReused {
			t.Fatalf("AccountResolution = %q, want %q", decision.AccountResolution, AccountResolutionReused)
		}
		if decision.MatchedAccountState != MatchedAccountStateActive {
			t.Fatalf("MatchedAccountState = %q, want %q", decision.MatchedAccountState, MatchedAccountStateActive)
		}
	})

	t.Run("disabled or deprecated account is reactivated", func(t *testing.T) {
		t.Parallel()

		for _, state := range []MatchedAccountState{MatchedAccountStateDisabled, MatchedAccountStateDeprecated} {
			state := state
			t.Run(string(state), func(t *testing.T) {
				t.Parallel()

				decision := DecideReuse(ReuseInput{
					ProviderMatched:          true,
					ProviderID:               "api-kimi-12345678",
					CanonicalModelFamilies:   []string{"kimi-k2.6"},
					MatchedAccountID:         202,
					MatchedAccountState:      state,
					ExistingProviderID:       "api-kimi-12345678",
					ExistingAccessStatus:     AccessStatusActive,
					ExistingCanonicalFamilys: []string{"kimi-2.6"},
				})

				if !decision.ProvisionReused {
					t.Fatal("ProvisionReused = false, want true")
				}
				if decision.AccountResolution != AccountResolutionReactivated {
					t.Fatalf("AccountResolution = %q, want %q", decision.AccountResolution, AccountResolutionReactivated)
				}
			})
		}
	})

	t.Run("broken provider or account is replaced", func(t *testing.T) {
		t.Parallel()

		brokenProvider := DecideReuse(ReuseInput{
			ProviderMatched:          true,
			ProviderID:               "api-deepseek-12345678",
			CanonicalModelFamilies:   []string{"deepseek-v4-pro"},
			MatchedAccountState:      MatchedAccountStateActive,
			ExistingProviderID:       "api-deepseek-12345678",
			ExistingAccessStatus:     AccessStatusBroken,
			ExistingCanonicalFamilys: []string{"deepseek-v4-pro"},
		})
		if brokenProvider.ProvisionReused {
			t.Fatal("ProvisionReused = true, want false for broken provider")
		}
		if brokenProvider.AccountResolution != AccountResolutionReplaced {
			t.Fatalf("AccountResolution = %q, want %q", brokenProvider.AccountResolution, AccountResolutionReplaced)
		}

		brokenAccount := DecideReuse(ReuseInput{
			ProviderMatched:          true,
			ProviderID:               "api-deepseek-12345678",
			CanonicalModelFamilies:   []string{"deepseek-v4-pro"},
			MatchedAccountState:      MatchedAccountStateBroken,
			ExistingProviderID:       "api-deepseek-12345678",
			ExistingAccessStatus:     AccessStatusActive,
			ExistingCanonicalFamilys: []string{"deepseek-v4-pro"},
		})
		if brokenAccount.ProvisionReused {
			t.Fatal("ProvisionReused = true, want false for broken account")
		}
		if brokenAccount.AccountResolution != AccountResolutionReplaced {
			t.Fatalf("AccountResolution = %q, want %q", brokenAccount.AccountResolution, AccountResolutionReplaced)
		}
	})

	t.Run("same family different alias counts as covered", func(t *testing.T) {
		t.Parallel()

		decision := DecideReuse(ReuseInput{
			ProviderMatched:          true,
			ProviderID:               "api-kimi-12345678",
			CanonicalModelFamilies:   []string{"kimi-k2.6"},
			MatchedAccountState:      MatchedAccountStateActive,
			ExistingProviderID:       "api-kimi-12345678",
			ExistingAccessStatus:     AccessStatusActive,
			ExistingCanonicalFamilys: []string{"kimi 2.6"},
		})

		if !decision.ProvisionReused {
			t.Fatal("ProvisionReused = false, want true")
		}
		if !decision.FamilyCovered {
			t.Fatal("FamilyCovered = false, want true")
		}
	})

	t.Run("base url matched legacy provider is reused even when provider ids differ", func(t *testing.T) {
		t.Parallel()

		decision := DecideReuse(ReuseInput{
			ProviderMatched:          true,
			ProviderID:               "api-53hk-42797c06",
			CanonicalModelFamilies:   []string{"minimax-m2.7-highspeed"},
			MatchedAccountID:         101,
			MatchedAccountState:      MatchedAccountStateActive,
			ExistingProviderID:       "minimax-53hk",
			ExistingAccessStatus:     AccessStatusActive,
			ExistingCanonicalFamilys: []string{"minimax-m2.5-highspeed", "minimax-m2.7-highspeed"},
		})

		if !decision.ProvisionReused {
			t.Fatal("ProvisionReused = false, want true")
		}
		if decision.AccountResolution != AccountResolutionReused {
			t.Fatalf("AccountResolution = %q, want %q", decision.AccountResolution, AccountResolutionReused)
		}
	})
}
