// Code generated by Fern. DO NOT EDIT.

package users_test

import (
	context "context"
	fmt "fmt"
	management "github.com/auth0/go-auth0/v2/management"
	client "github.com/auth0/go-auth0/v2/management/client"
	option "github.com/auth0/go-auth0/v2/management/option"
	require "github.com/stretchr/testify/require"
	gowiremock "github.com/wiremock/go-wiremock"
	wiremocktestcontainersgo "github.com/wiremock/wiremock-testcontainers-go"
	http "net/http"
	os "os"
	testing "testing"
)

// TestMain sets up shared test fixtures for all tests in this package// Global test fixtures
var (
	WireMockContainer *wiremocktestcontainersgo.WireMockContainer
	WireMockBaseURL   string
	WireMockClient    *gowiremock.Client
)

// TestMain sets up shared test fixtures for all tests in this package
func TestMain(m *testing.M) {
	// Setup shared WireMock container
	ctx := context.Background()
	container, err := wiremocktestcontainersgo.RunContainerAndStopOnCleanup(
		ctx,
		&testing.T{},
		wiremocktestcontainersgo.WithImage("docker.io/wiremock/wiremock:3.9.1"),
	)
	if err != nil {
		fmt.Printf("Failed to start WireMock container: %v\n", err)
		os.Exit(1)
	}

	// Store global references
	WireMockContainer = container

	// Try to get the base URL using the standard method first
	baseURL, err := container.Endpoint(ctx, "")
	if err == nil {
		// Standard method worked (running outside DinD)
		// This uses the mapped port (e.g., localhost:59553)
		WireMockBaseURL = "http://" + baseURL
		WireMockClient = container.Client
	} else {
		// Standard method failed, use internal IP fallback (DinD environment)
		fmt.Printf("Standard endpoint resolution failed, using internal IP fallback: %v\n", err)

		inspect, err := container.Inspect(ctx)
		if err != nil {
			fmt.Printf("Failed to inspect WireMock container: %v\n", err)
			os.Exit(1)
		}

		// Find the IP address from the container's networks
		var containerIP string
		for _, network := range inspect.NetworkSettings.Networks {
			if network.IPAddress != "" {
				containerIP = network.IPAddress
				break
			}
		}

		if containerIP == "" {
			fmt.Printf("Failed to get WireMock container IP address\n")
			os.Exit(1)
		}

		// In DinD, use the internal port directly (8080 for WireMock HTTP)
		// Don't use the mapped port since it doesn't exist in this environment
		WireMockBaseURL = fmt.Sprintf("http://%s:8080", containerIP)

		// The container.Client was created with a bad URL, so we need a new one
		WireMockClient = gowiremock.NewClient(WireMockBaseURL)
	}

	fmt.Printf("WireMock available at: %s\n", WireMockBaseURL)

	// Run all tests
	code := m.Run()

	// Cleanup
	if WireMockContainer != nil {
		WireMockContainer.Terminate(ctx)
	}

	// Exit with the same code as the tests
	os.Exit(code)
}

func TestUsersListWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Get(gowiremock.URLPathTemplate("/users")).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"start": 1.1, "limit": 1.1, "length": 1.1, "total": 1.1, "users": []interface{}{map[string]interface{}{"user_id": "user_id", "email": "email", "email_verified": true, "username": "username", "phone_number": "phone_number", "phone_verified": true, "created_at": "created_at", "updated_at": "updated_at", "identities": []interface{}{map[string]interface{}{}}, "app_metadata": map[string]interface{}{"key": "value"}, "user_metadata": map[string]interface{}{"key": "value"}, "picture": "picture", "name": "name", "nickname": "nickname", "multifactor": []interface{}{"multifactor"}, "last_ip": "last_ip", "last_login": "last_login", "logins_count": 1, "blocked": true, "given_name": "given_name", "family_name": "family_name"}}},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	request := &management.ListUsersRequestParameters{
		Page: management.Int(
			1,
		),
		PerPage: management.Int(
			1,
		),
		IncludeTotals: management.Bool(
			true,
		),
		Sort: management.String(
			"sort",
		),
		Connection: management.String(
			"connection",
		),
		Fields: management.String(
			"fields",
		),
		IncludeFields: management.Bool(
			true,
		),
		Q: management.String(
			"q",
		),
		SearchEngine: management.SearchEngineVersionsEnumV1.Ptr(),
		PrimaryOrder: management.Bool(
			true,
		),
	}
	_, invocationErr := client.Users.List(
		context.TODO(),
		request,
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUsersCreateWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Post(gowiremock.URLPathTemplate("/users")).WithBodyPattern(gowiremock.MatchesJsonSchema(`{
                    "$schema": "https://json-schema.org/draft/2020-12/schema",
                    "type": "object",
                    "required": ["connection"],
                    "properties": {
                        "connection": {"type": "string"}
                    },
                    "additionalProperties": true
                }`, "V202012")).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"user_id": "user_id", "email": "email", "email_verified": true, "username": "username", "phone_number": "phone_number", "phone_verified": true, "created_at": "created_at", "updated_at": "updated_at", "identities": []interface{}{map[string]interface{}{"connection": "connection", "user_id": "user_id", "provider": "ad", "isSocial": true, "access_token": "access_token", "access_token_secret": "access_token_secret", "refresh_token": "refresh_token"}}, "app_metadata": map[string]interface{}{"key": "value"}, "user_metadata": map[string]interface{}{"key": "value"}, "picture": "picture", "name": "name", "nickname": "nickname", "multifactor": []interface{}{"multifactor"}, "last_ip": "last_ip", "last_login": "last_login", "logins_count": 1, "blocked": true, "given_name": "given_name", "family_name": "family_name"},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	request := &management.CreateUserRequestContent{
		Connection: "connection",
	}
	_, invocationErr := client.Users.Create(
		context.TODO(),
		request,
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUsersListUsersByEmailWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Get(gowiremock.URLPathTemplate("/users-by-email")).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			[]interface{}{map[string]interface{}{"user_id": "user_id", "email": "email", "email_verified": true, "username": "username", "phone_number": "phone_number", "phone_verified": true, "created_at": "created_at", "updated_at": "updated_at", "identities": []interface{}{map[string]interface{}{}}, "app_metadata": map[string]interface{}{"key": "value"}, "user_metadata": map[string]interface{}{"key": "value"}, "picture": "picture", "name": "name", "nickname": "nickname", "multifactor": []interface{}{"multifactor"}, "last_ip": "last_ip", "last_login": "last_login", "logins_count": 1, "blocked": true, "given_name": "given_name", "family_name": "family_name"}},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	request := &management.ListUsersByEmailRequestParameters{
		Fields: management.String(
			"fields",
		),
		IncludeFields: management.Bool(
			true,
		),
		Email: "email",
	}
	_, invocationErr := client.Users.ListUsersByEmail(
		context.TODO(),
		request,
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUsersGetWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Get(gowiremock.URLPathTemplate("/users/{id}")).WithPathParam(
		"id",
		gowiremock.Matching("id"),
	).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"user_id": "user_id", "email": "email", "email_verified": true, "username": "username", "phone_number": "phone_number", "phone_verified": true, "created_at": "created_at", "updated_at": "updated_at", "identities": []interface{}{map[string]interface{}{"connection": "connection", "user_id": "user_id", "provider": "ad", "isSocial": true, "access_token": "access_token", "access_token_secret": "access_token_secret", "refresh_token": "refresh_token"}}, "app_metadata": map[string]interface{}{"key": "value"}, "user_metadata": map[string]interface{}{"key": "value"}, "picture": "picture", "name": "name", "nickname": "nickname", "multifactor": []interface{}{"multifactor"}, "last_ip": "last_ip", "last_login": "last_login", "logins_count": 1, "blocked": true, "given_name": "given_name", "family_name": "family_name"},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	request := &management.GetUserRequestParameters{
		Fields: management.String(
			"fields",
		),
		IncludeFields: management.Bool(
			true,
		),
	}
	_, invocationErr := client.Users.Get(
		context.TODO(),
		"id",
		request,
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUsersDeleteWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Delete(gowiremock.URLPathTemplate("/users/{id}")).WithPathParam(
		"id",
		gowiremock.Matching("id"),
	).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	invocationErr := client.Users.Delete(
		context.TODO(),
		"id",
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUsersUpdateWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Patch(gowiremock.URLPathTemplate("/users/{id}")).WithPathParam(
		"id",
		gowiremock.Matching("id"),
	).WithBodyPattern(gowiremock.MatchesJsonSchema(`{
                    "$schema": "https://json-schema.org/draft/2020-12/schema",
                    "type": "object",
                    "required": [],
                    "properties": {
                        
                    },
                    "additionalProperties": true
                }`, "V202012")).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"user_id": "user_id", "email": "email", "email_verified": true, "username": "username", "phone_number": "phone_number", "phone_verified": true, "created_at": "created_at", "updated_at": "updated_at", "identities": []interface{}{map[string]interface{}{"connection": "connection", "user_id": "user_id", "provider": "ad", "isSocial": true, "access_token": "access_token", "access_token_secret": "access_token_secret", "refresh_token": "refresh_token"}}, "app_metadata": map[string]interface{}{"key": "value"}, "user_metadata": map[string]interface{}{"key": "value"}, "picture": "picture", "name": "name", "nickname": "nickname", "multifactor": []interface{}{"multifactor"}, "last_ip": "last_ip", "last_login": "last_login", "logins_count": 1, "blocked": true, "given_name": "given_name", "family_name": "family_name"},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	request := &management.UpdateUserRequestContent{}
	_, invocationErr := client.Users.Update(
		context.TODO(),
		"id",
		request,
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUsersRegenerateRecoveryCodeWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Post(gowiremock.URLPathTemplate("/users/{id}/recovery-code-regeneration")).WithPathParam(
		"id",
		gowiremock.Matching("id"),
	).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{"recovery_code": "recovery_code"},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	_, invocationErr := client.Users.RegenerateRecoveryCode(
		context.TODO(),
		"id",
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}

func TestUsersRevokeAccessWithWireMock(
	t *testing.T,
) {
	// wiremock client and server initialized in shared main_test.go
	defer WireMockClient.Reset()
	stub := gowiremock.Post(gowiremock.URLPathTemplate("/users/{id}/revoke-access")).WithPathParam(
		"id",
		gowiremock.Matching("id"),
	).WithBodyPattern(gowiremock.MatchesJsonSchema(`{
                    "$schema": "https://json-schema.org/draft/2020-12/schema",
                    "type": "object",
                    "required": [],
                    "properties": {
                        
                    },
                    "additionalProperties": true
                }`, "V202012")).WillReturnResponse(
		gowiremock.NewResponse().WithJSONBody(
			map[string]interface{}{},
		).WithStatus(http.StatusOK),
	)
	err := WireMockClient.StubFor(stub)
	require.NoError(t, err, "Failed to create WireMock stub")

	client := client.NewWithOptions(
		option.WithBaseURL(
			WireMockBaseURL,
		),
	)
	request := &management.RevokeUserAccessRequestContent{}
	invocationErr := client.Users.RevokeAccess(
		context.TODO(),
		"id",
		request,
	)

	require.NoError(t, invocationErr, "Client method call should succeed")
	ok, countErr := WireMockClient.Verify(stub.Request(), 1)
	require.NoError(t, countErr, "Failed to verify WireMock request was matched")
	require.True(t, ok, "WireMock request was not matched")
}
