# {{ project_name }} - Main Terraform Configuration
# Generated by typedialog-prov-gen

terraform {
  required_version = ">= 1.0"

  required_providers {
    {% if infrastructure.providers contains "hetzner" %}
    hetzner = {
      source  = "hetznercloud/hetzner"
      version = "~> 1.44"
    }
    {% endif %}
    {% if infrastructure.providers contains "lxd" %}
    lxd = {
      source  = "terraform-lxd/lxd"
      version = "~> 1.10"
    }
    {% endif %}
    {% if infrastructure.providers contains "aws" %}
    aws = {
      source  = "hashicorp/aws"
      version = "~> 5.0"
    }
    {% endif %}
  }
}

# Local variables from Nickel configuration
locals {
  project_name = "{{ project_name }}"
  environment  = var.environment

  common_tags = {
    Project     = local.project_name
    Environment = local.environment
    ManagedBy   = "Terraform"
    Generated   = "typedialog-prov-gen"
  }
}

{% if infrastructure.database %}
# Database resources
resource "{{ infrastructure.database.db_type }}_instance" "main" {
  name     = "${local.project_name}-${local.environment}-db"
  size     = var.database_size
  region   = var.region

  tags = local.common_tags
}
{% endif %}

{% if infrastructure.monitoring contains "prometheus" %}
# Monitoring instance
resource "compute_instance" "monitoring" {
  name         = "${local.project_name}-${local.environment}-monitoring"
  machine_type = var.monitoring_machine_type
  zone         = var.zone

  boot_disk {
    initialize_params {
      image = var.monitoring_image
    }
  }

  metadata_startup_script = file("${path.module}/../../scripts/install-prometheus.sh")

  tags = merge(local.common_tags, {
    Role = "monitoring"
  })
}
{% endif %}

# Output values
output "infrastructure_id" {
  description = "Infrastructure resource IDs"
  value = {
    {% if infrastructure.database %}
    database_id = {{ infrastructure.database.db_type }}_instance.main.id
    {% endif %}
    {% if infrastructure.monitoring contains "prometheus" %}
    monitoring_id = compute_instance.monitoring.id
    {% endif %}
  }
}

output "connection_strings" {
  description = "Connection information"
  sensitive   = true
  value = {
    {% if infrastructure.database %}
    database_url = {{ infrastructure.database.db_type }}_instance.main.connection_string
    {% endif %}
  }
}
