# Task 050


create an aws_lex_bot for ordering pizzas and set nlu_intent_confidence_threshold to 0.5

## Intent

Has one resource "aws_lex_bot" with argument "enable_model_improvements" = true
 and argument "nlu_intent_confidence_threshold" = 0.5 and at least one resource "aws_lex_intent"

## 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"
  }

  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
  enable_model_improvements = true
  nlu_intent_confidence_threshold = 0.5
  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])
}

# 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
    resource.values.enable_model_improvements == true
    resource.values.nlu_intent_confidence_threshold == 0.5
}

# 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
}