// 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"
	"time"
)

// Creates and initializes a browser session in Amazon Bedrock AgentCore. The
// session enables agents to navigate and interact with web content, extract
// information from websites, and perform web-based tasks as part of their response
// generation.
//
// To create a session, you must specify a browser identifier and a name. You can
// also configure the viewport dimensions to control the visible area of web
// content. The session remains active until it times out or you explicitly stop it
// using the StopBrowserSession operation.
//
// The following operations are related to StartBrowserSession :
//
// [GetBrowserSession]
//
// [UpdateBrowserStream]
//
// [SaveBrowserSessionProfile]
//
// [StopBrowserSession]
//
// [InvokeBrowser]
//
// [SaveBrowserSessionProfile]: https://docs.aws.amazon.com/bedrock-agentcore/latest/APIReference/API_SaveBrowserSessionProfile.html
// [UpdateBrowserStream]: https://docs.aws.amazon.com/bedrock-agentcore/latest/APIReference/API_UpdateBrowserStream.html
// [GetBrowserSession]: https://docs.aws.amazon.com/bedrock-agentcore/latest/APIReference/API_GetBrowserSession.html
// [StopBrowserSession]: https://docs.aws.amazon.com/bedrock-agentcore/latest/APIReference/API_StopBrowserSession.html
// [InvokeBrowser]: https://docs.aws.amazon.com/bedrock-agentcore/latest/APIReference/API_InvokeBrowser.html
func (c *Client) StartBrowserSession(ctx context.Context, params *StartBrowserSessionInput, optFns ...func(*Options)) (*StartBrowserSessionOutput, error) {
	if params == nil {
		params = &StartBrowserSessionInput{}
	}

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

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

type StartBrowserSessionInput struct {

	// The unique identifier of the browser to use for this session. This identifier
	// specifies which browser environment to initialize for the session.
	//
	// This member is required.
	BrowserIdentifier *string

	// A list of certificates to install in the browser session.
	Certificates []types.Certificate

	// A unique, case-sensitive identifier to ensure that the API request completes no
	// more than one time. If this token matches a previous request, Amazon Bedrock
	// AgentCore ignores the request, but does not return an error. This parameter
	// helps prevent the creation of duplicate sessions if there are temporary network
	// issues.
	ClientToken *string

	// A list of files containing enterprise policies for the browser.
	EnterprisePolicies []types.BrowserEnterprisePolicy

	// A list of browser extensions to load into the browser session.
	Extensions []types.BrowserExtension

	// The name of the browser session. This name helps you identify and manage the
	// session. The name does not need to be unique.
	Name *string

	// The browser profile configuration to use for this session. A browser profile
	// contains persistent data such as cookies and local storage that can be reused
	// across multiple browser sessions. If specified, the session initializes with the
	// profile's stored data, enabling continuity for tasks that require authentication
	// or personalized settings.
	ProfileConfiguration *types.BrowserProfileConfiguration

	// Optional proxy configuration for routing browser traffic through
	// customer-specified proxy servers. When provided, enables HTTP Basic
	// authentication via Amazon Web Services Secrets Manager and domain-based routing
	// rules. Requires secretsmanager:GetSecretValue IAM permission for the specified
	// secret ARNs.
	ProxyConfiguration *types.ProxyConfiguration

	// The duration in seconds (time-to-live) after which the session automatically
	// terminates, regardless of ongoing activity. Defaults to 3600 seconds (1 hour).
	// Recommended minimum: 60 seconds. Maximum allowed: 28,800 seconds (8 hours).
	SessionTimeoutSeconds *int32

	// The trace identifier for request tracking.
	TraceId *string

	// The parent trace information for distributed tracing.
	TraceParent *string

	// The dimensions of the browser viewport for this session. This determines the
	// visible area of the web content and affects how web pages are rendered. If not
	// specified, Amazon Bedrock AgentCore uses a default viewport size.
	ViewPort *types.ViewPort

	noSmithyDocumentSerde
}

type StartBrowserSessionOutput struct {

	// The identifier of the browser.
	//
	// This member is required.
	BrowserIdentifier *string

	// The timestamp when the browser session was created.
	//
	// This member is required.
	CreatedAt *time.Time

	// The unique identifier of the created browser session.
	//
	// This member is required.
	SessionId *string

	// The streams associated with this browser session. These include the automation
	// stream and live view stream.
	Streams *types.BrowserSessionStream

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

	noSmithyDocumentSerde
}

func (c *Client) addOperationStartBrowserSessionMiddlewares(stack *middleware.Stack, options Options) (err error) {
	err = stack.Serialize.Add(&awsRestjson1_serializeOpStartBrowserSession{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsRestjson1_deserializeOpStartBrowserSession{}, 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_opStartBrowserSessionMiddleware(stack, options); err != nil {
		return err
	}
	if err = addOpStartBrowserSessionValidationMiddleware(stack); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware(options.Region, "StartBrowserSession"), 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_initializeOpStartBrowserSession struct {
	tokenProvider IdempotencyTokenProvider
}

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

func (m *idempotencyToken_initializeOpStartBrowserSession) 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.(*StartBrowserSessionInput)
	if !ok {
		return out, metadata, fmt.Errorf("expected middleware input to be of type *StartBrowserSessionInput ")
	}

	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_opStartBrowserSessionMiddleware(stack *middleware.Stack, cfg Options) error {
	return stack.Initialize.Add(&idempotencyToken_initializeOpStartBrowserSession{tokenProvider: cfg.IdempotencyTokenProvider}, middleware.Before)
}
