// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
// Code generated by Microsoft (R) Go Code Generator. DO NOT EDIT.

package armneonpostgres

import (
	"context"
	"errors"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/arm"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/policy"
	"github.com/Azure/azure-sdk-for-go/sdk/azcore/runtime"
	"net/http"
	"net/url"
	"strings"
)

// NeonDatabasesClient contains the methods for the NeonDatabases group.
// Don't use this type directly, use NewNeonDatabasesClient() instead.
type NeonDatabasesClient struct {
	internal       *arm.Client
	subscriptionID string
}

// NewNeonDatabasesClient creates a new instance of NeonDatabasesClient with the specified values.
//   - subscriptionID - The ID of the target subscription. The value must be an UUID.
//   - credential - used to authorize requests. Usually a credential from azidentity.
//   - options - pass nil to accept the default values.
func NewNeonDatabasesClient(subscriptionID string, credential azcore.TokenCredential, options *arm.ClientOptions) (*NeonDatabasesClient, error) {
	cl, err := arm.NewClient(moduleName, moduleVersion, credential, options)
	if err != nil {
		return nil, err
	}
	client := &NeonDatabasesClient{
		subscriptionID: subscriptionID,
		internal:       cl,
	}
	return client, nil
}

// NewListPager - List NeonDatabase resources by Branch
//
// Generated from API version 2025-03-01
//   - resourceGroupName - The name of the resource group. The name is case insensitive.
//   - organizationName - Name of the Neon Organizations resource
//   - projectName - The name of the Project
//   - branchName - The name of the Branch
//   - options - NeonDatabasesClientListOptions contains the optional parameters for the NeonDatabasesClient.NewListPager method.
func (client *NeonDatabasesClient) NewListPager(resourceGroupName string, organizationName string, projectName string, branchName string, options *NeonDatabasesClientListOptions) *runtime.Pager[NeonDatabasesClientListResponse] {
	return runtime.NewPager(runtime.PagingHandler[NeonDatabasesClientListResponse]{
		More: func(page NeonDatabasesClientListResponse) bool {
			return page.NextLink != nil && len(*page.NextLink) > 0
		},
		Fetcher: func(ctx context.Context, page *NeonDatabasesClientListResponse) (NeonDatabasesClientListResponse, error) {
			ctx = context.WithValue(ctx, runtime.CtxAPINameKey{}, "NeonDatabasesClient.NewListPager")
			nextLink := ""
			if page != nil {
				nextLink = *page.NextLink
			}
			resp, err := runtime.FetcherForNextLink(ctx, client.internal.Pipeline(), nextLink, func(ctx context.Context) (*policy.Request, error) {
				return client.listCreateRequest(ctx, resourceGroupName, organizationName, projectName, branchName, options)
			}, nil)
			if err != nil {
				return NeonDatabasesClientListResponse{}, err
			}
			return client.listHandleResponse(resp)
		},
		Tracer: client.internal.Tracer(),
	})
}

// listCreateRequest creates the List request.
func (client *NeonDatabasesClient) listCreateRequest(ctx context.Context, resourceGroupName string, organizationName string, projectName string, branchName string, _ *NeonDatabasesClientListOptions) (*policy.Request, error) {
	urlPath := "/subscriptions/{subscriptionId}/resourceGroups/{resourceGroupName}/providers/Neon.Postgres/organizations/{organizationName}/projects/{projectName}/branches/{branchName}/neonDatabases"
	if client.subscriptionID == "" {
		return nil, errors.New("parameter client.subscriptionID cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{subscriptionId}", url.PathEscape(client.subscriptionID))
	if resourceGroupName == "" {
		return nil, errors.New("parameter resourceGroupName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{resourceGroupName}", url.PathEscape(resourceGroupName))
	if organizationName == "" {
		return nil, errors.New("parameter organizationName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{organizationName}", url.PathEscape(organizationName))
	if projectName == "" {
		return nil, errors.New("parameter projectName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{projectName}", url.PathEscape(projectName))
	if branchName == "" {
		return nil, errors.New("parameter branchName cannot be empty")
	}
	urlPath = strings.ReplaceAll(urlPath, "{branchName}", url.PathEscape(branchName))
	req, err := runtime.NewRequest(ctx, http.MethodGet, runtime.JoinPaths(client.internal.Endpoint(), urlPath))
	if err != nil {
		return nil, err
	}
	reqQP := req.Raw().URL.Query()
	reqQP.Set("api-version", "2025-03-01")
	req.Raw().URL.RawQuery = reqQP.Encode()
	req.Raw().Header["Accept"] = []string{"application/json"}
	return req, nil
}

// listHandleResponse handles the List response.
func (client *NeonDatabasesClient) listHandleResponse(resp *http.Response) (NeonDatabasesClientListResponse, error) {
	result := NeonDatabasesClientListResponse{}
	if err := runtime.UnmarshalAsJSON(resp, &result.NeonDatabaseListResult); err != nil {
		return NeonDatabasesClientListResponse{}, err
	}
	return result, nil
}
