package main

import (
	"path"

	"github.com/cargo-lambda/cargo-lambda-cdk/cargolambdacdk"

	"github.com/aws/aws-cdk-go/awscdk/v2"
	"github.com/aws/constructs-go/constructs/v10"
	"github.com/aws/jsii-runtime-go"
)

type WorkspaceExampleCdkStackProps struct {
	awscdk.StackProps
}

func NewWorkspaceExampleCdkStack(scope constructs.Construct, id string, props *WorkspaceExampleCdkStackProps) awscdk.Stack {
	var sprops awscdk.StackProps
	if props != nil {
		sprops = props.StackProps
	}
	stack := awscdk.NewStack(scope, &id, &sprops)

	extension := cargolambdacdk.NewRustExtension(stack, jsii.String("Rust Extension"), &cargolambdacdk.RustExtensionProps{
		ManifestPath: jsii.String(path.Join("..", "..", "rust-workspace", "rust-extension")),
	})

	function := cargolambdacdk.NewRustFunction(stack, jsii.String("Rust Function"), &cargolambdacdk.RustFunctionProps{
		ManifestPath: jsii.String(path.Join("..", "..", "rust-workspace", "rust-function")),
	})
	function.AddLayers(extension)

	awscdk.NewCfnOutput(stack, jsii.String("Extension ARN"), &awscdk.CfnOutputProps{
		Value: extension.LayerVersionArn(),
	})

	awscdk.NewCfnOutput(stack, jsii.String("Function ARN"), &awscdk.CfnOutputProps{
		Value: function.FunctionArn(),
	})

	return stack
}

func main() {
	defer jsii.Close()

	app := awscdk.NewApp(nil)

	NewWorkspaceExampleCdkStack(app, "WorkspaceExampleCdkStack", &WorkspaceExampleCdkStackProps{
		awscdk.StackProps{
			Env: env(),
		},
	})

	app.Synth(nil)
}

// env determines the AWS environment (account+region) in which our stack is to
// be deployed. For more information see: https://docs.aws.amazon.com/cdk/latest/guide/environments.html
func env() *awscdk.Environment {
	// If unspecified, this stack will be "environment-agnostic".
	// Account/Region-dependent features and context lookups will not work, but a
	// single synthesized template can be deployed anywhere.
	//---------------------------------------------------------------------------
	return nil

	// Uncomment if you know exactly what account and region you want to deploy
	// the stack to. This is the recommendation for production stacks.
	//---------------------------------------------------------------------------
	// return &awscdk.Environment{
	//  Account: jsii.String("123456789012"),
	//  Region:  jsii.String("us-east-1"),
	// }

	// Uncomment to specialize this stack for the AWS Account and Region that are
	// implied by the current CLI configuration. This is recommended for dev
	// stacks.
	//---------------------------------------------------------------------------
	// return &awscdk.Environment{
	//  Account: jsii.String(os.Getenv("CDK_DEFAULT_ACCOUNT")),
	//  Region:  jsii.String(os.Getenv("CDK_DEFAULT_REGION")),
	// }
}
