# Root Terragrunt config for foxhunt production
# All child modules inherit provider, backend, and common inputs from here.

locals {
  region     = "fr-par"
  zone       = "fr-par-2"
  project_id = get_env("SCW_DEFAULT_PROJECT_ID")
}

# Remote state in Scaleway Object Storage (bucket foxhunt-tfstate, fr-par) — migrated off GitLab 2026-06-21.
# The s3 backend reads AWS_* env vars (set them to the Scaleway access/secret keys); SCW_* drives the provider.
remote_state {
  backend = "s3"
  generate = {
    path      = "backend.tf"
    if_exists = "overwrite"
  }
  config = {
    bucket = "foxhunt-tfstate"
    key    = "${path_relative_to_include()}/terraform.tfstate"
    region = "fr-par"

    endpoints = {
      s3 = "https://s3.fr-par.scw.cloud"
    }

    # Scaleway S3-compat: skip AWS-specific preflight calls
    skip_credentials_validation = true
    skip_region_validation      = true
    skip_requesting_account_id  = true
    skip_metadata_api_check     = true

    # OpenTofu-native lock (no DynamoDB)
    use_lockfile = true
  }
}

# Generate provider config for all child modules
generate "provider" {
  path      = "provider.tf"
  if_exists = "overwrite"
  contents  = <<-EOF
    terraform {
      required_version = ">= 1.8.0"
      required_providers {
        scaleway = {
          source  = "scaleway/scaleway"
          version = "~> 2.46"
        }
        random = {
          source  = "hashicorp/random"
          version = "~> 3.6"
        }
      }
    }

    provider "scaleway" {
      region     = "${local.region}"
      zone       = "${local.zone}"
      project_id = "${local.project_id}"
    }
  EOF
}

# Common inputs available to all child modules
inputs = {
  region     = local.region
  zone       = local.zone
  project_id = local.project_id
}
