// SPEC-597-EXEMPT: canonical mapping (npm-package↔framework, keyword catalogue)
// — must enumerate framework names by design; no detection logic to migrate.
// engine/infrastructure/terraform-generator.ts — Terraform HCL generator (SPEC-171)
import { getTerraformVersion } from '../../config/version-defaults.js';
function renderProvider(env) {
    return `terraform {
  required_providers {
    aws = {
      source  = "hashicorp/aws"
      version = "${getTerraformVersion('aws-provider')}"
    }
  }
}

provider "aws" {
  region = var.aws_region
}

variable "aws_region" {
  description = "AWS region"
  default     = "us-east-1"
}

variable "environment" {
  description = "Deployment environment"
  default     = "${env}"
}
`;
}
function renderRds(component, env) {
    const instanceClass = env === 'prod' ? 'db.t3.medium' : 'db.t3.micro';
    const safeName = component.name.replace(/-/g, '_');
    return `
resource "aws_db_instance" "${safeName}" {
  identifier        = "\${var.environment}-${component.name}"
  engine            = "postgres"
  engine_version    = "${getTerraformVersion('postgres-engine')}"
  instance_class    = "${instanceClass}"
  allocated_storage = ${env === 'prod' ? 100 : 20}
  username          = "dbadmin"
  password          = var.db_password_${safeName}
  skip_final_snapshot = ${env !== 'prod'}

  tags = {
    Environment = var.environment
    Name        = "${component.name}"
  }
}

variable "db_password_${safeName}" {
  description = "Password for ${component.name} database"
  sensitive   = true
}
`;
}
function renderRedis(component, env) {
    const nodeType = env === 'prod' ? 'cache.t3.medium' : 'cache.t3.micro';
    const safeName = component.name.replace(/-/g, '_');
    return `
resource "aws_elasticache_cluster" "${safeName}" {
  cluster_id           = "\${var.environment}-${component.name}"
  engine               = "redis"
  node_type            = "${nodeType}"
  num_cache_nodes      = 1
  parameter_group_name = "default.redis7"

  tags = {
    Environment = var.environment
    Name        = "${component.name}"
  }
}
`;
}
function renderSqs(component) {
    const safeName = component.name.replace(/-/g, '_');
    return `
resource "aws_sqs_queue" "${safeName}" {
  name                      = "\${var.environment}-${component.name}"
  message_retention_seconds = 86400
  visibility_timeout_seconds = 30

  tags = {
    Environment = var.environment
    Name        = "${component.name}"
  }
}
`;
}
function renderS3(component, emitCallerIdentity) {
    const safeName = component.name.replace(/-/g, '_');
    const callerIdentityBlock = emitCallerIdentity
        ? `\ndata "aws_caller_identity" "current" {}\n`
        : '';
    return `
resource "aws_s3_bucket" "${safeName}" {
  bucket = "\${var.environment}-${component.name}-\${data.aws_caller_identity.current.account_id}"

  tags = {
    Environment = var.environment
    Name        = "${component.name}"
  }
}

resource "aws_s3_bucket_versioning" "${safeName}_versioning" {
  bucket = aws_s3_bucket.${safeName}.id
  versioning_configuration {
    status = "Enabled"
  }
}
${callerIdentityBlock}`;
}
function renderEcs(component, env) {
    const cpu = env === 'prod' ? 1024 : 256;
    const memory = env === 'prod' ? 2048 : 512;
    return `
resource "aws_ecs_cluster" "${component.name}_cluster" {
  name = "\${var.environment}-${component.name}"

  tags = {
    Environment = var.environment
  }
}

resource "aws_ecs_task_definition" "${component.name}" {
  family                   = "\${var.environment}-${component.name}"
  requires_compatibilities = ["FARGATE"]
  network_mode             = "awsvpc"
  cpu                      = ${cpu}
  memory                   = ${memory}

  container_definitions = jsonencode([{
    name  = "${component.name}"
    image = "your-ecr-image:latest"
    portMappings = [{
      containerPort = 3000
      hostPort      = 3000
    }]
  }])

  tags = {
    Environment = var.environment
  }
}
`;
}
function renderLambda(component) {
    return `
resource "aws_lambda_function" "${component.name}" {
  function_name = "\${var.environment}-${component.name}"
  role          = aws_iam_role.lambda_exec_${component.name.replace(/-/g, '_')}.arn
  handler       = "index.handler"
  runtime       = "nodejs20.x"
  filename      = "lambda_payload.zip"

  environment {
    variables = {
      ENVIRONMENT = var.environment
    }
  }

  tags = {
    Environment = var.environment
    Name        = "${component.name}"
  }
}

resource "aws_iam_role" "lambda_exec_${component.name.replace(/-/g, '_')}" {
  name = "\${var.environment}-${component.name}-exec"

  assume_role_policy = jsonencode({
    Version = "2012-10-17"
    Statement = [{
      Action    = "sts:AssumeRole"
      Effect    = "Allow"
      Principal = { Service = "lambda.amazonaws.com" }
    }]
  })
}
`;
}
function renderComponent(component, env, emitCallerIdentity) {
    switch (component.kind) {
        case 'rds':
        case 'generic-database':
            return renderRds(component, env);
        case 'supabase':
            return `\n# Supabase is a managed service — provision via Supabase dashboard or Terraform provider.\n# See: https://registry.terraform.io/providers/supabase/supabase/latest\n`;
        case 'redis':
        case 'generic-cache':
            return renderRedis(component, env);
        case 'sqs':
        case 'generic-queue':
            return renderSqs(component);
        case 's3':
        case 'generic-storage':
            return renderS3(component, emitCallerIdentity);
        case 'ecs':
            return renderEcs(component, env);
        case 'lambda':
            return renderLambda(component);
        case 'railway-service':
        case 'k8s-deployment':
        case 'k8s-service':
        case 'k8s-ingress':
        case 'cdn-cloudfront':
            return `\n# Component "${component.name}" (${component.kind}) — provision via the respective provider dashboard or CLI.\n`;
    }
}
/**
 * Generate a Terraform main.tf for the given components and environment.
 */
export function generateTerraform(components, env) {
    const terraformComponents = components.filter((c) => [
        'rds',
        'supabase',
        'redis',
        'sqs',
        's3',
        'ecs',
        'lambda',
        'generic-database',
        'generic-cache',
        'generic-queue',
        'generic-storage',
    ].includes(c.kind));
    let callerIdentityEmitted = false;
    const blocks = terraformComponents
        .map((c) => {
        const isS3 = c.kind === 's3' || c.kind === 'generic-storage';
        const emitCallerIdentity = isS3 && !callerIdentityEmitted;
        if (emitCallerIdentity) {
            callerIdentityEmitted = true;
        }
        return renderComponent(c, env, emitCallerIdentity);
    })
        .join('\n');
    const content = renderProvider(env) + blocks;
    return {
        format: 'terraform',
        filename: 'main.tf',
        content,
    };
}
//# sourceMappingURL=terraform-generator.js.map