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

package comprehendmedical

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

// Starts an asynchronous job to detect protected health information (PHI). Use
// the DescribePHIDetectionJob operation to track the status of a job.
func (c *Client) StartPHIDetectionJob(ctx context.Context, params *StartPHIDetectionJobInput, optFns ...func(*Options)) (*StartPHIDetectionJobOutput, error) {
	if params == nil {
		params = &StartPHIDetectionJobInput{}
	}

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

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

type StartPHIDetectionJobInput struct {

	// The Amazon Resource Name (ARN) of the AWS Identity and Access Management (IAM)
	// role that grants Amazon Comprehend Medical read access to your input data. For
	// more information, see [Role-Based Permissions Required for Asynchronous Operations].
	//
	// [Role-Based Permissions Required for Asynchronous Operations]: https://docs.aws.amazon.com/comprehend/latest/dg/access-control-managing-permissions-med.html#auth-role-permissions-med
	//
	// This member is required.
	DataAccessRoleArn *string

	// Specifies the format and location of the input data for the job.
	//
	// This member is required.
	InputDataConfig *types.InputDataConfig

	// The language of the input documents. All documents must be in the same language.
	//
	// This member is required.
	LanguageCode types.LanguageCode

	// Specifies where to send the output files.
	//
	// This member is required.
	OutputDataConfig *types.OutputDataConfig

	// A unique identifier for the request. If you don't set the client request token,
	// Amazon Comprehend Medical generates one.
	ClientRequestToken *string

	// The identifier of the job.
	JobName *string

	// An AWS Key Management Service key to encrypt your output files. If you do not
	// specify a key, the files are written in plain text.
	KMSKey *string

	noSmithyDocumentSerde
}

func (v *StartPHIDetectionJobInput) Serialize(s smithy.ShapeSerializer) {
	s.WriteStruct(schemas.StartPHIDetectionJobRequest)
	v.SerializeMembers(s)
	s.CloseStruct()
}

func (v *StartPHIDetectionJobInput) SerializeMembers(s smithy.ShapeSerializer) {
	if v.ClientRequestToken != nil {
		s.WriteString(schemas.StartPHIDetectionJobRequest_ClientRequestToken, *v.ClientRequestToken)
	}
	if v.DataAccessRoleArn != nil {
		s.WriteString(schemas.StartPHIDetectionJobRequest_DataAccessRoleArn, *v.DataAccessRoleArn)
	}
	if v.InputDataConfig != nil {
		s.WriteStruct(schemas.StartPHIDetectionJobRequest_InputDataConfig)
		v.InputDataConfig.SerializeMembers(s)
		s.CloseStruct()
	}
	if v.JobName != nil {
		s.WriteString(schemas.StartPHIDetectionJobRequest_JobName, *v.JobName)
	}
	if v.KMSKey != nil {
		s.WriteString(schemas.StartPHIDetectionJobRequest_KMSKey, *v.KMSKey)
	}
	if v.LanguageCode != "" {
		s.WriteString(schemas.StartPHIDetectionJobRequest_LanguageCode, string(v.LanguageCode))
	}
	if v.OutputDataConfig != nil {
		s.WriteStruct(schemas.StartPHIDetectionJobRequest_OutputDataConfig)
		v.OutputDataConfig.SerializeMembers(s)
		s.CloseStruct()
	}
}

type StartPHIDetectionJobOutput struct {

	// The identifier generated for the job. To get the status of a job, use this
	// identifier with the DescribePHIDetectionJob operation.
	JobId *string

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

	noSmithyDocumentSerde
}

func (v *StartPHIDetectionJobOutput) Serialize(s smithy.ShapeSerializer) {
	s.WriteStruct(schemas.StartPHIDetectionJobResponse)
	v.SerializeMembers(s)
	s.CloseStruct()
}

func (v *StartPHIDetectionJobOutput) SerializeMembers(s smithy.ShapeSerializer) {
	if v.JobId != nil {
		s.WriteString(schemas.StartPHIDetectionJobResponse_JobId, *v.JobId)
	}
}
func (v *StartPHIDetectionJobOutput) Deserialize(d smithy.ShapeDeserializer) error {
	return smithy.ReadStruct(d, schemas.StartPHIDetectionJobResponse, func(s *smithy.Schema) error {
		switch s {
		case schemas.StartPHIDetectionJobResponse_JobId:
			v.JobId = new(string)
			return d.ReadString(schemas.StartPHIDetectionJobResponse_JobId, v.JobId)
		}
		return nil
	})
}
func (c *Client) addOperationStartPHIDetectionJobMiddlewares(stack *middleware.Stack, options Options) (err error) {
	if err := stack.Serialize.Add(&serializeRequestMiddleware{options: &options, operationSchema: smithy.NewOperationSchema(schemas.StartPHIDetectionJob, schemas.StartPHIDetectionJobRequest, schemas.StartPHIDetectionJobResponse)}, middleware.After); err != nil {
		return err
	}
	if err := stack.Deserialize.Add(&deserializeResponseMiddleware{options: &options, operationSchema: smithy.NewOperationSchema(schemas.StartPHIDetectionJob, schemas.StartPHIDetectionJobRequest, schemas.StartPHIDetectionJobResponse), output: &StartPHIDetectionJobOutput{}}, middleware.After); 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 = addUserAgentFeatureProtocolRPCV2CBOR(stack, options); err != nil {
		return err
	}
	if err = addCredentialSource(stack, options); err != nil {
		return err
	}
	if err = addIdempotencyToken_opStartPHIDetectionJobMiddleware(stack, options); err != nil {
		return err
	}
	if err = addOpStartPHIDetectionJobValidationMiddleware(stack); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware(options.Region, "StartPHIDetectionJob"), 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_initializeOpStartPHIDetectionJob struct {
	tokenProvider IdempotencyTokenProvider
}

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

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

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