# Hetzner Server Module
# Generated by Prism

terraform {
  required_providers {
    hcloud = {
      source = "hetznercloud/hcloud"
    }
  }
}

resource "hcloud_server" "main" {
  name        = "${var.project_name}-${var.environment}"
  server_type = var.server_type
  location    = var.location
  image       = "ubuntu-24.04"
  ssh_keys    = var.ssh_key_ids
  firewall_ids = var.firewall_ids

  user_data = var.cloud_init_file

  labels = var.labels

  public_net {
    ipv4_enabled = true
    ipv6_enabled = true
  }

  lifecycle {
    ignore_changes = [
      user_data,  # Don't recreate server if cloud-init changes
    ]
  }
}

# Attach server to private network
resource "hcloud_server_network" "main" {
  server_id = hcloud_server.main.id
  subnet_id = var.subnet_id
}

# Floating IP for production (optional)
resource "hcloud_floating_ip" "main" {
  count         = var.enable_floating_ip ? 1 : 0
  type          = "ipv4"
  home_location = var.location
  name          = "${var.project_name}-${var.environment}-ip"

  labels = var.labels
}

resource "hcloud_floating_ip_assignment" "main" {
  count          = var.enable_floating_ip ? 1 : 0
  floating_ip_id = hcloud_floating_ip.main[0].id
  server_id      = hcloud_server.main.id
}
