# Terraform minimal pour Windows - KUBERNETES UNIQUEMENT
terraform {
  required_version = ">= 1.5.0"
  
  required_providers {
    kubernetes = {
      source  = "hashicorp/kubernetes"
      version = "~> 2.23"
    }
  }
}

# Provider Kubernetes avec Docker Desktop
provider "kubernetes" {
  config_path    = "~/.kube/config"
  config_context = "docker-desktop"
}

# Namespace
resource "kubernetes_namespace" "devops" {
  metadata {
    name = "terraform-ns"
    
    labels = {
      created-by = "terraform"
      environment = "development"
    }
  }
}

# Déploiement simple
resource "kubernetes_deployment" "app" {
  metadata {
    name      = "terraform-app"
    namespace = kubernetes_namespace.devops.metadata[0].name
    
    labels = {
      app = "terraform-app"
    }
  }

  spec {
    replicas = 1

    selector {
      match_labels = {
        app = "terraform-app"
      }
    }

    template {
      metadata {
        labels = {
          app = "terraform-app"
        }
      }

      spec {
        container {
          name  = "app"
          image = "nginx:alpine"  # Image simple pour test
          
          port {
            container_port = 80
          }
          
          resources {
            limits = {
              cpu    = "100m"
              memory = "128Mi"
            }
            requests = {
              cpu    = "50m"
              memory = "64Mi"
            }
          }
        }
      }
    }
  }
  
  depends_on = [kubernetes_namespace.devops]
}

# Service
resource "kubernetes_service" "app" {
  metadata {
    name      = "terraform-service"
    namespace = kubernetes_namespace.devops.metadata[0].name
  }

  spec {
    selector = {
      app = kubernetes_deployment.app.spec[0].template[0].metadata[0].labels.app
    }

    port {
      port        = 8080
      target_port = 80
    }

    type = "NodePort"
  }
}

# Output utile
output "application_url" {
  value = "http://localhost:${kubernetes_service.app.spec[0].port[0].node_port}"
}

output "namespace" {
  value = kubernetes_namespace.devops.metadata[0].name
}

