package services

import (
	"testing"

	"github.com/nexuscrm/auth"
)

func TestBootstrapConnectedAppCallbackURLUsesLocalhostFallback(t *testing.T) {
	t.Setenv("API_BASE_URL", "")

	got := bootstrapConnectedAppCallbackURL()
	if got != "http://127.0.0.1:8080/callback" {
		t.Fatalf("bootstrapConnectedAppCallbackURL() = %q, want localhost absolute callback", got)
	}
}

func TestBootstrapConnectedAppCallbackURLUsesAPIBaseURL(t *testing.T) {
	t.Setenv("API_BASE_URL", "https://nexus.example/")

	got := bootstrapConnectedAppCallbackURL()
	if got != "https://nexus.example/callback" {
		t.Fatalf("bootstrapConnectedAppCallbackURL() = %q, want API_BASE_URL callback", got)
	}
}

func TestIssuesRefreshTokensTracksRegisteredGrantTypes(t *testing.T) {
	for _, test := range []struct {
		name       string
		grantTypes []string
		want       bool
	}{
		{name: "authorization code only", grantTypes: []string{auth.OAuthGrantAuthorizationCode}, want: false},
		{name: "refresh token enabled", grantTypes: []string{auth.OAuthGrantAuthorizationCode, auth.OAuthGrantRefreshToken}, want: true},
	} {
		t.Run(test.name, func(t *testing.T) {
			if got := issuesRefreshTokens(test.grantTypes); got != test.want {
				t.Fatalf("issuesRefreshTokens(%v) = %t, want %t", test.grantTypes, got, test.want)
			}
		})
	}
}
