// Code generated by smithy-go-codegen DO NOT EDIT.

package bedrock

import (
	"context"
	"fmt"
	"github.com/aws/aws-sdk-go-v2/service/bedrock/types"
	"github.com/aws/smithy-go/middleware"
	smithyhttp "github.com/aws/smithy-go/transport/http"
)

// Lists the advanced prompt optimization jobs in your account.
func (c *Client) ListAdvancedPromptOptimizationJobs(ctx context.Context, params *ListAdvancedPromptOptimizationJobsInput, optFns ...func(*Options)) (*ListAdvancedPromptOptimizationJobsOutput, error) {
	if params == nil {
		params = &ListAdvancedPromptOptimizationJobsInput{}
	}

	result, metadata, err := c.invokeOperation(ctx, "ListAdvancedPromptOptimizationJobs", params, optFns, c.addOperationListAdvancedPromptOptimizationJobsMiddlewares)
	if err != nil {
		return nil, err
	}

	out := result.(*ListAdvancedPromptOptimizationJobsOutput)
	out.ResultMetadata = metadata
	return out, nil
}

// List Advanced Prompt Optimization Jobs Request
type ListAdvancedPromptOptimizationJobsInput struct {

	// The maximum number of results to return in the response.
	MaxResults *int32

	// If the total number of results is greater than the maxResults value provided in
	// the request, use this token in a subsequent request to get the next set of
	// results.
	NextToken *string

	// The field to sort the results by.
	SortBy types.SortJobsBy

	// The sort order for the results.
	SortOrder types.SortOrder

	noSmithyDocumentSerde
}

// List Advanced Prompt Optimization Jobs Response
type ListAdvancedPromptOptimizationJobsOutput struct {

	// A list of advanced prompt optimization job summaries.
	JobSummaries []types.AdvancedPromptOptimizationJobSummary

	// If the total number of results is greater than the maxResults value provided in
	// the request, use this token in a subsequent request to get the next set of
	// results.
	NextToken *string

	// Metadata pertaining to the operation's result.
	ResultMetadata middleware.Metadata

	noSmithyDocumentSerde
}

func (c *Client) addOperationListAdvancedPromptOptimizationJobsMiddlewares(stack *middleware.Stack, options Options) (err error) {
	err = stack.Serialize.Add(&awsRestjson1_serializeOpListAdvancedPromptOptimizationJobs{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsRestjson1_deserializeOpListAdvancedPromptOptimizationJobs{}, middleware.After)
	if err != nil {
		return err
	}

	if err = addlegacyEndpointContextSetter(stack, options); err != nil {
		return err
	}
	if err = addComputeContentLength(stack); err != nil {
		return err
	}
	if err = addResolveEndpointMiddleware(stack, options); err != nil {
		return err
	}
	if err = addComputePayloadSHA256(stack); err != nil {
		return err
	}
	if err = addRecordResponseTiming(stack, options); err != nil {
		return err
	}
	if err = smithyhttp.AddErrorCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = smithyhttp.AddCloseResponseBodyMiddleware(stack); err != nil {
		return err
	}
	if err = addCredentialSource(stack, options); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware(options.Region, "ListAdvancedPromptOptimizationJobs"), middleware.Before); err != nil {
		return err
	}
	if err = addRequestIDRetrieverMiddleware(stack); err != nil {
		return err
	}
	if err = addResponseErrorMiddleware(stack); err != nil {
		return err
	}
	if err = addRequestResponseLogging(stack, options); err != nil {
		return err
	}
	if err = addDisableHTTPSMiddleware(stack, options); err != nil {
		return err
	}
	if err = addInterceptors(stack, options); err != nil {
		return err
	}
	return nil
}

// ListAdvancedPromptOptimizationJobsPaginatorOptions is the paginator options for
// ListAdvancedPromptOptimizationJobs
type ListAdvancedPromptOptimizationJobsPaginatorOptions struct {
	// The maximum number of results to return in the response.
	Limit int32

	// Set to true if pagination should stop if the service returns a pagination token
	// that matches the most recent token provided to the service.
	StopOnDuplicateToken bool
}

// ListAdvancedPromptOptimizationJobsPaginator is a paginator for
// ListAdvancedPromptOptimizationJobs
type ListAdvancedPromptOptimizationJobsPaginator struct {
	options   ListAdvancedPromptOptimizationJobsPaginatorOptions
	client    ListAdvancedPromptOptimizationJobsAPIClient
	params    *ListAdvancedPromptOptimizationJobsInput
	nextToken *string
	firstPage bool
}

// NewListAdvancedPromptOptimizationJobsPaginator returns a new
// ListAdvancedPromptOptimizationJobsPaginator
func NewListAdvancedPromptOptimizationJobsPaginator(client ListAdvancedPromptOptimizationJobsAPIClient, params *ListAdvancedPromptOptimizationJobsInput, optFns ...func(*ListAdvancedPromptOptimizationJobsPaginatorOptions)) *ListAdvancedPromptOptimizationJobsPaginator {
	if params == nil {
		params = &ListAdvancedPromptOptimizationJobsInput{}
	}

	options := ListAdvancedPromptOptimizationJobsPaginatorOptions{}
	if params.MaxResults != nil {
		options.Limit = *params.MaxResults
	}

	for _, fn := range optFns {
		fn(&options)
	}

	return &ListAdvancedPromptOptimizationJobsPaginator{
		options:   options,
		client:    client,
		params:    params,
		firstPage: true,
		nextToken: params.NextToken,
	}
}

// HasMorePages returns a boolean indicating whether more pages are available
func (p *ListAdvancedPromptOptimizationJobsPaginator) HasMorePages() bool {
	return p.firstPage || (p.nextToken != nil && len(*p.nextToken) != 0)
}

// NextPage retrieves the next ListAdvancedPromptOptimizationJobs page.
func (p *ListAdvancedPromptOptimizationJobsPaginator) NextPage(ctx context.Context, optFns ...func(*Options)) (*ListAdvancedPromptOptimizationJobsOutput, error) {
	if !p.HasMorePages() {
		return nil, fmt.Errorf("no more pages available")
	}

	params := *p.params
	params.NextToken = p.nextToken

	var limit *int32
	if p.options.Limit > 0 {
		limit = &p.options.Limit
	}
	params.MaxResults = limit

	optFns = append([]func(*Options){
		addIsPaginatorUserAgent,
	}, optFns...)
	result, err := p.client.ListAdvancedPromptOptimizationJobs(ctx, &params, optFns...)
	if err != nil {
		return nil, err
	}
	p.firstPage = false

	prevToken := p.nextToken
	p.nextToken = result.NextToken

	if p.options.StopOnDuplicateToken &&
		prevToken != nil &&
		p.nextToken != nil &&
		*prevToken == *p.nextToken {
		p.nextToken = nil
	}

	return result, nil
}

// ListAdvancedPromptOptimizationJobsAPIClient is a client that implements the
// ListAdvancedPromptOptimizationJobs operation.
type ListAdvancedPromptOptimizationJobsAPIClient interface {
	ListAdvancedPromptOptimizationJobs(context.Context, *ListAdvancedPromptOptimizationJobsInput, ...func(*Options)) (*ListAdvancedPromptOptimizationJobsOutput, error)
}

var _ ListAdvancedPromptOptimizationJobsAPIClient = (*Client)(nil)
