terraform {
  required_version = ">= 1.5.0"
}

############################
# Locals
############################

locals {
  service_name = "{{ service.name }}"

  labels = {
    app.kubernetes.io/name       = local.service_name
    app.kubernetes.io/managed-by = "iacgen"
  }
}

############################
# Namespace (optional)
############################

resource "kubernetes_namespace" "this" {
  count = var.create_namespace ? 1 : 0

  metadata {
    name = var.namespace

    labels = {
      app.kubernetes.io/part-of = local.service_name
    }
  }
}

############################
# Deployment
############################

resource "kubernetes_deployment" "this" {
  metadata {
    name      = local.service_name
    namespace = var.namespace
    labels    = local.labels
  }

  spec {
    replicas = var.replicas

    selector {
      match_labels = local.labels
    }

    template {
      metadata {
        labels = local.labels
      }

      spec {
        container {
          name  = local.service_name
          image = var.image

          port {
            container_port = var.port
          }

          resources {
            limits = {
              cpu    = var.cpu_limit
              memory = var.memory_limit
            }
            requests = {
              cpu    = var.cpu_request
              memory = var.memory_request
            }
          }

          # Basic HTTP liveness probe
          liveness_probe {
            http_get {
              path = var.health_check_path
              port = var.port
            }
            initial_delay_seconds = 10
            period_seconds        = 10
          }

          # Basic HTTP readiness probe
          readiness_probe {
            http_get {
              path = var.health_check_path
              port = var.port
            }
            initial_delay_seconds = 5
            period_seconds        = 5
          }
        }
      }
    }
  }
}

############################
# Service
############################

resource "kubernetes_service" "this" {
  metadata {
    name      = local.service_name
    namespace = var.namespace
    labels    = local.labels
  }

  spec {
    selector = local.labels

    port {
      port        = var.port
      target_port = var.port
    }

    type = var.service_type
  }
}

############################
# Horizontal Pod Autoscaler (optional)
############################

resource "kubernetes_horizontal_pod_autoscaler_v2" "this" {
  count = var.enable_autoscaling ? 1 : 0

  metadata {
    name      = "${local.service_name}-hpa"
    namespace = var.namespace
  }

  spec {
    min_replicas = var.min_replicas
    max_replicas = var.max_replicas

    scale_target_ref {
      api_version = "apps/v1"
      kind        = "Deployment"
      name        = kubernetes_deployment.this.metadata[0].name
    }

    metric {
      type = "Resource"

      resource {
        name = "cpu"

        target {
          type                = "Utilization"
          average_utilization = var.target_cpu_utilization
        }
      }
    }
  }
}
