terraform {
  required_providers {
    ssh = {
      source = "loafoe/ssh"
      version = "2.6.0"
    }
    kubernetes = {
      source = "hashicorp/kubernetes"
      version = "2.27.0"
    }
    helm = {
      source = "hashicorp/helm"
      version = "2.12.1"
    }
    kubectl = {
      source = "gavinbunney/kubectl"
      version = "1.14.0"
    }
  }
}

locals {
  ssh_host        = "192.168.1.2"
  ssh_user        = "junv"
  ssh_port        = 22422
  ssh_private_key = file("~/.ssh/id_ed25519")
  kubeconfig_cmd  = "scp -P ${local.ssh_port} ${local.ssh_user}@${local.ssh_host}:/etc/rancher/k3s/k3s.yaml ~/.kube/config && sed -i '' 's/127.0.0.1/${local.ssh_host}/g' ~/.kube/config"
  token_cmd       = "ssh -p ${local.ssh_port} ${local.ssh_user}@${local.ssh_host} 'sudo cat /var/lib/rancher/k3s/server/node-token'"
}

provider "kubernetes" {
  config_path = "~/.kube/config"
}

provider "helm" {
  kubernetes {
    config_path = "~/.kube/config"
  }
}

provider "kubectl" {
  config_path = "~/.kube/config"
}

resource "ssh_resource" "k3s_install" {
  host        = local.ssh_host
  user        = local.ssh_user
  port        = local.ssh_port
  private_key = local.ssh_private_key
  timeout     = "15m"

  when = "create"

  commands = [
    "echo 'Installing k3s...'",
    "curl -sfL https://get.k3s.io > install.sh",
    "chmod +x install.sh",
    "INSTALL_K3S_EXEC='--tls-san ${local.ssh_host} --kube-apiserver-arg service-node-port-range=80-32767 --disable traefik' sudo -E ./install.sh",
    "echo 'Waiting for k3s to start (60s)...'",
    "sleep 60",
    "sudo chmod 644 /etc/rancher/k3s/k3s.yaml",
    "echo 'Checking node status...'",
    "sudo kubectl --kubeconfig=/etc/rancher/k3s/k3s.yaml get nodes -o wide",
    "echo 'K3s installation completed!'"
  ]
}

resource "ssh_resource" "dns_fix" {
  depends_on = [ssh_resource.k3s_install]

  host        = local.ssh_host
  user        = local.ssh_user
  port        = local.ssh_port
  private_key = local.ssh_private_key
  timeout     = "3m"

  when = "create"

  commands = [
    "cat << 'EOF' | sudo tee /etc/systemd/system/systemd-resolved-restart.service\n[Unit]\nDescription=Restart systemd-resolved\nAfter=network.target\n\n[Service]\nType=oneshot\nExecStart=/bin/systemctl restart systemd-resolved\n\n[Install]\nWantedBy=multi-user.target\nEOF",
    "sudo systemctl enable systemd-resolved-restart.service",
    "sudo systemctl start systemd-resolved-restart.service",
    "echo 'DNS fix service installed and enabled'"
  ]
}

output "k3s_token" {
  value     = local.token_cmd
  sensitive = false
}

output "kubeconfig_command" {
  value     = local.kubeconfig_cmd
  sensitive = false
}

output "next_steps" {
  value = <<EOT
K3s cluster setup complete! Next steps:

1. Get your kubeconfig:
   ${local.kubeconfig_cmd}

2. Test the connection:
   kubectl get nodes

3. To add worker nodes, get the node token:
   ${local.token_cmd}
EOT
}

output "argocd_access" {
  value = <<EOT
ArgoCD has been installed! To access the UI:

1. Get the admin password:
   kubectl -n argocd get secret argocd-initial-admin-secret -o jsonpath="{.data.password}" | base64 -d

2. Access ArgoCD:
   https://argo.junv.cc

3. Login with:
   Username: admin
   Password: (from step 1)
EOT
}
