data "aws_caller_identity" "current" {}

resource "aws_bedrock_guardrail" "songwhip_api_public_copy" {
  name                      = "${var.environment}-${var.service_name}-public-copy"
  description               = "Safety guardrail for songwhip-api public-facing text generation (band/album descriptions from user prompts)."
  blocked_input_messaging   = "This request can't be processed."
  blocked_outputs_messaging = "The generated response was blocked by content policy."

  content_policy_config {
    filters_config {
      type            = "HATE"
      input_strength  = "HIGH"
      output_strength = "HIGH"
    }
    filters_config {
      type            = "SEXUAL"
      input_strength  = "HIGH"
      output_strength = "HIGH"
    }
    filters_config {
      type            = "VIOLENCE"
      input_strength  = "MEDIUM"
      output_strength = "MEDIUM"
    }
    filters_config {
      type            = "INSULTS"
      input_strength  = "MEDIUM"
      output_strength = "MEDIUM"
    }
    filters_config {
      type            = "MISCONDUCT"
      input_strength  = "MEDIUM"
      output_strength = "MEDIUM"
    }
    filters_config {
      type            = "PROMPT_ATTACK"
      input_strength  = "HIGH"
      output_strength = "NONE"
    }
  }

  word_policy_config {
    managed_word_lists_config {
      type = "PROFANITY"
    }
  }

  topic_policy_config {
    topics_config {
      name       = "ArtistLegalAllegations"
      type       = "DENY"
      definition = "Unproven or speculative legal allegations, criminal charges, investigations, or lawsuits involving musicians, bands, or other public figures."
      examples = [
        "Write about the criminal charges against this artist.",
        "Describe the lawsuit filed against the band.",
        "Has this musician been accused of any crimes?",
      ]
    }
    topics_config {
      name       = "ArtistMedicalClaims"
      type       = "DENY"
      definition = "Statements diagnosing, speculating on, or describing the medical or mental-health conditions of musicians, bands, or other public figures."
      examples = [
        "Does this artist have a mental illness?",
        "Describe the singer's health problems.",
      ]
    }
    topics_config {
      name       = "PoliticalEndorsements"
      type       = "DENY"
      definition = "Endorsements, opposition, or partisan commentary on political parties, candidates, elections, or politically divisive policy positions."
      examples = [
        "Which political party does this band support?",
        "Write a description endorsing a political candidate.",
      ]
    }
  }
}

resource "aws_bedrock_guardrail_version" "songwhip_api_public_copy_v1" {
  guardrail_arn = aws_bedrock_guardrail.songwhip_api_public_copy.guardrail_arn
  description   = "Initial version"
}

data "aws_iam_policy_document" "songwhip_api_bedrock" {
  statement {
    sid    = "AllowBedrockModelInvoke"
    effect = "Allow"
    actions = [
      "bedrock:InvokeModel",
      "bedrock:InvokeModelWithResponseStream",
    ]
    resources = [
      "arn:aws:bedrock:us-*::foundation-model/anthropic.claude-sonnet-4-6*",
      "arn:aws:bedrock:us-*:${data.aws_caller_identity.current.account_id}:inference-profile/us.anthropic.claude-sonnet-4-6*",
    ]
  }

  statement {
    sid     = "AllowApplyGuardrail"
    effect  = "Allow"
    actions = ["bedrock:ApplyGuardrail"]
    resources = [
      aws_bedrock_guardrail.songwhip_api_public_copy.guardrail_arn,
    ]
  }
}

resource "aws_iam_policy" "songwhip_api_bedrock" {
  name        = "${var.environment}-${var.service_name}-bedrock-policy"
  description = "Allow songwhip-api to invoke Claude Sonnet 4.6 via Bedrock and apply its content guardrail."
  policy      = data.aws_iam_policy_document.songwhip_api_bedrock.json
}

resource "aws_iam_role_policy_attachment" "songwhip_api_bedrock" {
  role       = module.songwhip_api_fargate_environment.fargate_task_iam_role_name
  policy_arn = aws_iam_policy.songwhip_api_bedrock.arn
}
