# Task 044


create an aws_lex bot with multiple slots

## Intent

Has one resource "aws_lex_bot" and one resource "aws_lex_intent"  with at least 2 slot blocks

## Reference Terraform solution

terraform {
  required_version = ">= 1.15.0"

  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "~> 6.0"
    }
  }
}

provider "aws" {
  region = "us-east-1"
}

resource "aws_lex_intent" "order_pizza_intent" {
  name                       = "OrderPizzaIntent"
  description                = "To order a pizza"
  
  sample_utterances = [
    "I would like to pick up a pizza",
    "I would like to order some pizzas",
  ]

  fulfillment_activity {
    type = "ReturnIntent"
  }

  slot {
    name                     = "PizzaType"
    description              = "Type of pizza to order"
    slot_constraint          = "Required" 
    slot_type                = "AMAZON.AlphaNumeric"
    priority                 = 1
    value_elicitation_prompt {
      message {
        content             = "What type of pizza would you like?"
        content_type        = "PlainText"
      }
      max_attempts         = 2
    }
  }

  slot {
    name                     = "PizzaSize"
    description              = "Size of pizza to order"
    slot_constraint          = "Required" 
    slot_type                = "AMAZON.NUMBER"
    priority                 = 2
    value_elicitation_prompt {
      message {
        content             = "What size of pizza would you like?"
        content_type        = "PlainText"
      }
      max_attempts         = 2
    }
  }

  slot {
    name                     = "PizzaQuantity"
    description              = "Number of pizzas to order"
    slot_constraint          = "Required" 
    slot_type                = "AMAZON.NUMBER"
    priority                 = 3
    value_elicitation_prompt {
      message {
        content             = "How many pizzas do you want to order?"
        content_type        = "PlainText"
      }
      max_attempts         = 2
    }
  }

  conclusion_statement {
    message {
      content              = "Your pizza order has been received."
      content_type         = "PlainText"
    }
  } 
}

resource "aws_lex_bot" "pizza_ordering_bot" {
  name                     = "PizzaOrderingBot"
  description              = "Bot to order pizzas"
  voice_id                 = "Joanna"
  idle_session_ttl_in_seconds = 300
  child_directed           = false
  locale                  = "en-US"
  process_behavior        = "BUILD"

  clarification_prompt {
    message {
      content      = "I didn't understand you, what type of pizza would you like to order?"
      content_type = "PlainText"
    }
    max_attempts = 5
  }

  abort_statement {
    message {
      content      = "Sorry, I am unable to assist at the moment."
      content_type = "PlainText"
    }
  }

  intent {
    intent_name    = aws_lex_intent.order_pizza_intent.name
    intent_version = aws_lex_intent.order_pizza_intent.version
  }
}


## OPA Rego validation policy

package terraform.policy

import rego.v1

default has_valid_resources = false

# Rule for multiple aws_lex_intent resources
has_valid_lex_intents if {
    count([intent | intent := input.planned_values.root_module.resources[_]; intent.type == "aws_lex_intent"; intent.values.name; intent.values.fulfillment_activity; intent.values.slot])
}

# Rule for aws_lex_bot resource with specific arguments
has_valid_lex_bot_instance if {
    some i
    resource := input.planned_values.root_module.resources[i]
    resource.type == "aws_lex_bot"
    is_boolean(resource.values.child_directed)
    resource.values.clarification_prompt
    resource.values.abort_statement
    resource.values.name
    # is_boolean(resource.values.create_version)
    # resource.values.description
    # is_boolean(resource.values.detect_sentiment)
    # resource.values.idle_session_ttl_in_seconds
    # resource.values.process_behavior
    # resource.values.voice_id
    # resource.values.locale
}

# Combined rule to ensure all conditions are met
has_valid_resources if {
    has_valid_lex_intents
    has_valid_lex_bot_instance
}

main if {
    has_valid_lex_intents
    has_valid_lex_bot_instance
    has_valid_resources
}