# Setup for the nexuscrm tidbcloud extract. Single source of truth is the source
# config: bucket names, prefixes, and retention are read from source.yaml so infra
# and the engine never drift.

terraform {
  source = "${get_terragrunt_dir()}/../modules//s3-export-setup"
}

locals {
  src       = yamldecode(file("${get_terragrunt_dir()}/../../../sources/nexuscrm/source.yaml"))
  s3        = local.src.s3
  retention = local.src.retention
}

# Local state for now (gitignored). Pinned to this dir so it survives terragrunt's
# module cache. Switch to an S3 backend when setup is shared across machines.
generate "backend" {
  path      = "backend.tf"
  if_exists = "overwrite_terragrunt"
  contents  = <<EOF
terraform {
  backend "local" {
    path = "${get_terragrunt_dir()}/terraform.tfstate"
  }
}
EOF
}

inputs = {
  region               = local.s3.region
  source_name          = local.src.source.name
  data_bucket          = local.s3.data.bucket
  metadata_bucket      = local.s3.metadata.bucket
  snapshot_prefix      = local.s3.data.snapshot_prefix
  manifest_prefix      = local.s3.metadata.manifest_prefix
  log_prefix           = local.s3.metadata.runtime_log_prefix
  snapshot_expire_days = local.retention.s3_snapshots_days
  metadata_expire_days = local.retention.manifests_days
}
