################
# Search Service — shared resources (S3, Secrets, Datadog)
################
# ECS EC2 GPU infrastructure (cluster, capacity provider, task definition,
# service, ALB, security groups, IAM roles) is in ecs_ec2.tf.
# local.search_service_name is defined in main.tf.

################
# Secrets — Snowflake + Sentry for search service
################

module "ows_coda_search_secrets" {
  source = "git@github.com:theorchard/terraform-secrets-manager.git//?ref=1.6.1"
  for_each = local.is_ephemeral ? toset([]) : toset([
    "SNOWFLAKE_PRIVATE_KEY",
    "SNOWFLAKE_PRIVATE_KEY_PASS",
  ])

  application_family = var.application_family
  environment        = var.environment
  service_name       = local.search_service_name
  secret_name        = each.value
}

################
# S3 — Search index snapshots
################

# Stores gzip-compressed index snapshots so the search service can restore
# quickly after a restart instead of re-introspecting from Snowflake/GraphQL.
module "s3_ows_coda_search_snapshots" {
  count              = local.is_ephemeral ? 0 : 1
  source             = "git@github.com:theorchard/terraform-s3.git//modules/s3_bucket?ref=3.15.7"
  env                = var.environment
  bucket_name        = "${local.search_service_name}-snapshots"
  application_family = var.application_family

  apply_server_side_encryption_by_default = {
    sse_algorithm     = "aws:kms"
    kms_master_key_id = data.aws_kms_alias.s3.target_key_arn
  }

  lifecycle_rules_options_current_version_transition = [
    {
      prefix        = ""
      days          = 30
      storage_class = "STANDARD_IA"
      enabled       = true
    },
    {
      prefix        = ""
      days          = 90
      storage_class = "GLACIER_IR"
      enabled       = true
    }
  ]

  lifecycle_rules_abort_incomplete_multipart_upload_days = [
    {
      prefix  = ""
      days    = 1
      enabled = true
    }
  ]

  block_public_access = true
}

################
# Sentry — Search service project
################

module "sentry_ows_coda_search" {
  count  = local.is_ephemeral ? 0 : 1
  source = "git@github.com:theorchard/terraform-sentry.git//?ref=5.1.0"

  environment        = var.environment
  platform           = "node"
  service_name       = local.search_service_name
  teams              = ["coda"]
  application_family = var.application_family
}

################
# Datadog — Search service dashboard
################

module "ows_coda_search_service_dashboard" {
  count       = local.is_ephemeral ? 0 : 1
  source      = "git@github.com:theorchard/terraform-datadog.git//modules/service?ref=6.19.0"
  environment = var.environment
  # The terraform-datadog service module only supports "fargate" in its metric
  # lookup maps. The search service runs on ECS EC2, but "fargate" is used here
  # to satisfy the module's interface. Dashboard metrics still work because
  # Datadog autodiscovery is tag-based, not launch-type-based.
  environment_type                  = "fargate"
  service_name                      = local.search_service_name
  application_family                = var.application_family
  teams                             = ["coda"]
  notification_endpoints            = local.datadog_notification_endpoints
  escalation_notification_endpoints = local.datadog_escalation_notification_endpoints
}
