# Create the Google service account and JSON key for AlmaForge group lookup.
# Protect Terraform state as a secret because it contains the private key.

terraform {
  required_version = ">= 1.3"

  required_providers {
    google = {
      source  = "hashicorp/google"
      version = ">= 6.0, < 8.0"
    }
  }
}

variable "project_id" {
  description = "Google Cloud project ID"
  type        = string
}

provider "google" {
  project = var.project_id
}

resource "google_service_account" "sso_sync" {
  account_id   = "almaforge-group-sync"
  display_name = "AlmaForge Google Workspace group lookup"
}

resource "google_service_account_key" "sso_sync_key" {
  service_account_id = google_service_account.sso_sync.name
}

output "service_account_client_id" {
  description = "Numeric client ID for Workspace domain-wide delegation"
  value       = google_service_account.sso_sync.unique_id
}

output "google_service_account_json" {
  description = "Service account credentials to install on each Auth Service host"
  value       = base64decode(google_service_account_key.sso_sync_key.private_key)
  sensitive   = true
}
