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

package bedrockagentcore

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

//	Starts a memory extraction job that processes events that failed extraction
//
// previously in an AgentCore Memory resource and produces structured memory
// records. When earlier extraction attempts have left events unprocessed, this job
// will pick up and extract those as well.
//
// To use this operation, you must have the
// bedrock-agentcore:StartMemoryExtractionJob permission.
func (c *Client) StartMemoryExtractionJob(ctx context.Context, params *StartMemoryExtractionJobInput, optFns ...func(*Options)) (*StartMemoryExtractionJobOutput, error) {
	if params == nil {
		params = &StartMemoryExtractionJobInput{}
	}

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

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

type StartMemoryExtractionJobInput struct {

	// Extraction job to start in this operation.
	//
	// This member is required.
	ExtractionJob *types.ExtractionJob

	// The unique identifier of the memory for which to start extraction jobs.
	//
	// This member is required.
	MemoryId *string

	// A unique, case-sensitive identifier to ensure idempotent processing of the
	// request.
	ClientToken *string

	noSmithyDocumentSerde
}

type StartMemoryExtractionJobOutput struct {

	// Extraction Job ID that was attempted to start.
	//
	// This member is required.
	JobId *string

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

	noSmithyDocumentSerde
}

func (c *Client) addOperationStartMemoryExtractionJobMiddlewares(stack *middleware.Stack, options Options) (err error) {
	err = stack.Serialize.Add(&awsRestjson1_serializeOpStartMemoryExtractionJob{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsRestjson1_deserializeOpStartMemoryExtractionJob{}, 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 = addIdempotencyToken_opStartMemoryExtractionJobMiddleware(stack, options); err != nil {
		return err
	}
	if err = addOpStartMemoryExtractionJobValidationMiddleware(stack); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware(options.Region, "StartMemoryExtractionJob"), 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
}

type idempotencyToken_initializeOpStartMemoryExtractionJob struct {
	tokenProvider IdempotencyTokenProvider
}

func (*idempotencyToken_initializeOpStartMemoryExtractionJob) ID() string {
	return "OperationIdempotencyTokenAutoFill"
}

func (m *idempotencyToken_initializeOpStartMemoryExtractionJob) HandleInitialize(ctx context.Context, in middleware.InitializeInput, next middleware.InitializeHandler) (
	out middleware.InitializeOutput, metadata middleware.Metadata, err error,
) {
	if m.tokenProvider == nil {
		return next.HandleInitialize(ctx, in)
	}

	input, ok := in.Parameters.(*StartMemoryExtractionJobInput)
	if !ok {
		return out, metadata, fmt.Errorf("expected middleware input to be of type *StartMemoryExtractionJobInput ")
	}

	if input.ClientToken == nil {
		t, err := m.tokenProvider.GetIdempotencyToken()
		if err != nil {
			return out, metadata, err
		}
		input.ClientToken = &t
	}
	return next.HandleInitialize(ctx, in)
}
func addIdempotencyToken_opStartMemoryExtractionJobMiddleware(stack *middleware.Stack, cfg Options) error {
	return stack.Initialize.Add(&idempotencyToken_initializeOpStartMemoryExtractionJob{tokenProvider: cfg.IdempotencyTokenProvider}, middleware.Before)
}
