import { Duration, Stack, StackProps } from 'aws-cdk-lib';
import { Queue } from 'aws-cdk-lib/aws-sqs';
import { StringParameter } from 'aws-cdk-lib/aws-ssm';
import { Construct } from 'constructs';
import { toValidSsmParameterName } from './to-valid-ssm-parameter-name';

export interface ExampleStackProps extends StackProps {
  // Define stack properties here

  queueVisibilityTimeout?: Duration;
}

export class ExampleStack extends Stack {
  constructor(scope: Construct, id: string, props: ExampleStackProps = {}) {
    const { queueVisibilityTimeout, ...stackProps } = props;

    super(scope, id, stackProps);

    // The code that defines your stack goes here

    // Example resource
    const exampleQueue = new Queue(this, 'ExampleQueue', {
      visibilityTimeout: queueVisibilityTimeout ?? Duration.seconds(300),
    });

    /*
      The SQS queue's URL is only known after the CloudFormation template has been synthesized.
      Furthermore, the URL changes per environment. To ease E2E testing, the URL is stored as a SSM parameter.
    */
    new StringParameter(this, 'E2eExampleQueueUrl', {
      parameterName: toValidSsmParameterName(`/e2e/${exampleQueue.node.path}/url`),
      stringValue: exampleQueue.queueUrl,
    });
  }
}
