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

package devopsagent

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

// Creates a new backlog task in the specified agent space
func (c *Client) CreateBacklogTask(ctx context.Context, params *CreateBacklogTaskInput, optFns ...func(*Options)) (*CreateBacklogTaskOutput, error) {
	if params == nil {
		params = &CreateBacklogTaskInput{}
	}

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

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

// Request structure for creating a new backlog task
type CreateBacklogTaskInput struct {

	// The unique identifier for the agent space where the task will be created
	//
	// This member is required.
	AgentSpaceId *string

	// The priority level of the task
	//
	// This member is required.
	Priority types.Priority

	// The type of task being created
	//
	// This member is required.
	TaskType types.TaskType

	// The title of the backlog task
	//
	// This member is required.
	Title *string

	// Client-provided token for idempotent operations
	ClientToken *string

	// Optional detailed description of the task
	Description *string

	// Optional reference information for the task
	Reference *types.ReferenceInput

	noSmithyDocumentSerde
}

// Response structure containing the created backlog task
type CreateBacklogTaskOutput struct {

	// The newly created task object
	//
	// This member is required.
	Task *types.Task

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

	noSmithyDocumentSerde
}

func (c *Client) addOperationCreateBacklogTaskMiddlewares(stack *middleware.Stack, options Options) (err error) {
	err = stack.Serialize.Add(&awsRestjson1_serializeOpCreateBacklogTask{}, middleware.After)
	if err != nil {
		return err
	}
	err = stack.Deserialize.Add(&awsRestjson1_deserializeOpCreateBacklogTask{}, 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); 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 = addEndpointPrefix_opCreateBacklogTaskMiddleware(stack); err != nil {
		return err
	}
	if err = addIdempotencyToken_opCreateBacklogTaskMiddleware(stack, options); err != nil {
		return err
	}
	if err = addOpCreateBacklogTaskValidationMiddleware(stack); err != nil {
		return err
	}
	if err = stack.Initialize.Add(newServiceMetadataMiddleware(options.Region, "CreateBacklogTask"), 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 endpointPrefix_opCreateBacklogTaskMiddleware struct {
}

func (*endpointPrefix_opCreateBacklogTaskMiddleware) ID() string {
	return "EndpointHostPrefix"
}

func (m *endpointPrefix_opCreateBacklogTaskMiddleware) HandleFinalize(ctx context.Context, in middleware.FinalizeInput, next middleware.FinalizeHandler) (
	out middleware.FinalizeOutput, metadata middleware.Metadata, err error,
) {
	if smithyhttp.GetHostnameImmutable(ctx) || smithyhttp.IsEndpointHostPrefixDisabled(ctx) {
		return next.HandleFinalize(ctx, in)
	}

	req, ok := in.Request.(*smithyhttp.Request)
	if !ok {
		return out, metadata, fmt.Errorf("unknown transport type %T", in.Request)
	}

	req.URL.Host = "dp." + req.URL.Host

	return next.HandleFinalize(ctx, in)
}
func addEndpointPrefix_opCreateBacklogTaskMiddleware(stack *middleware.Stack) error {
	return stack.Finalize.Insert(&endpointPrefix_opCreateBacklogTaskMiddleware{}, "ResolveEndpointV2", middleware.After)
}

type idempotencyToken_initializeOpCreateBacklogTask struct {
	tokenProvider IdempotencyTokenProvider
}

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

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

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