# Target Group: {{ resource.original_name }}
resource "aws_lb_target_group" "{{ resource.terraform_name }}" {
  name        = "{{ resource.config.name }}"
  port        = {{ resource.config.port | d(80) }}
  protocol    = "{{ resource.config.protocol | d('HTTP') }}"
{% if resource.config.vpc_id is tf_ref %}
  vpc_id      = {{ resource.config.vpc_id }}
{% else %}
{% set vpc_resource = graph.get_resource(resource.config.vpc_id) %}
{% if vpc_resource and vpc_resource.terraform_name %}
  vpc_id      = aws_vpc.{{ vpc_resource.terraform_name }}.id
{% else %}
  # WARNING: VPC {{ resource.config.vpc_id }} not found in graph - using original ID
  vpc_id      = "{{ resource.config.vpc_id }}"
{% endif %}
{% endif %}
  target_type = "{{ resource.config.target_type | d('instance') }}"

  health_check {
    enabled             = {{ resource.config.health_check.enabled | d(true) | lower }}
    path                = "{{ resource.config.health_check.path | d('/') }}"
    port                = "{{ resource.config.health_check.port | d('traffic-port') }}"
    protocol            = "{{ resource.config.health_check.protocol | d('HTTP') }}"
    healthy_threshold   = {{ resource.config.health_check.healthy_threshold | d(3) }}
    unhealthy_threshold = {{ resource.config.health_check.unhealthy_threshold | d(3) }}
    timeout             = {{ resource.config.health_check.timeout_seconds | d(5) }}
    interval            = {{ resource.config.health_check.interval_seconds | d(30) }}
{% if resource.config.health_check.matcher %}
    matcher             = "{{ resource.config.health_check.matcher.HttpCode | d('200') }}"
{% endif %}
  }

  tags = {
    Name        = "{{ resource.original_name }}"
    Environment = var.environment
    ManagedBy   = "replimap"
    SourceId    = "{{ resource.id }}"
{% for key, value in resource.tags.items() if key != 'Name' %}
    {{ key | quote_key }} = {{ value | quote }}
{% endfor %}
  }
}

{% for target in resource.config.targets %}
{% if target.id and resource.config.target_type == 'instance' %}
# Target attachment: {{ target.id }}
resource "aws_lb_target_group_attachment" "{{ resource.terraform_name }}_target_{{ loop.index }}" {
  target_group_arn = aws_lb_target_group.{{ resource.terraform_name }}.arn
{% if target.id is tf_ref %}
  target_id        = {{ target.id }}
{% else %}
{% set instance_resource = graph.get_resource(target.id) %}
{% if instance_resource and instance_resource.terraform_name %}
  target_id        = aws_instance.{{ instance_resource.terraform_name }}.id
{% else %}
  # WARNING: Instance {{ target.id }} not found in graph - may be in different state
  target_id        = "{{ target.id }}"
{% endif %}
{% endif %}
{% if target.port %}
  port             = {{ target.port }}
{% endif %}
}

{% elif target.id and resource.config.target_type == 'ip' %}
# Target attachment (IP): {{ target.id }}
resource "aws_lb_target_group_attachment" "{{ resource.terraform_name }}_target_{{ loop.index }}" {
  target_group_arn  = aws_lb_target_group.{{ resource.terraform_name }}.arn
  target_id         = "{{ target.id }}"
{% if target.port %}
  port              = {{ target.port }}
{% endif %}
{% if target.availability_zone %}
  availability_zone = "{{ target.availability_zone }}"
{% endif %}
}

{% endif %}
{% endfor %}
