# Pods as a table — optional namespace (-n) or all namespaces (-A)
def kgp [namespace?: string, --all (-A)] {
  let flags = if $all { [--all-namespaces] } else if ($namespace != null) { [-n $namespace] } else { [] }
  ^kubectl get pods ...$flags -o json | from json | get items
  | each {|p|
      let cs = $p.status.containerStatuses? | default []
      {
        name:      $p.metadata.name
        namespace: $p.metadata.namespace
        phase:     $p.status.phase
        ready:     $"(($cs | where ready == true | length))/(($cs | length))"
        restarts:  ($cs | each { $in.restartCount } | math sum)
        age:       ((date now) - ($p.metadata.creationTimestamp | into datetime))
      }
  }
}

# Deployments as a table — optional namespace (-n) or all namespaces (-A)
def kgd [namespace?: string, --all (-A)] {
  let flags = if $all { [--all-namespaces] } else if ($namespace != null) { [-n $namespace] } else { [] }
  ^kubectl get deployments ...$flags -o json | from json | get items
  | each {|d| {
      name:       $d.metadata.name
      namespace:  $d.metadata.namespace
      ready:      $"($d.status.readyReplicas? | default 0)/($d.spec.replicas)"
      up-to-date: ($d.status.updatedReplicas? | default 0)
      available:  ($d.status.availableReplicas? | default 0)
      age:        ((date now) - ($d.metadata.creationTimestamp | into datetime))
  }}
}

# Services as a table — optional namespace (-n) or all namespaces (-A)
def kgs [namespace?: string, --all (-A)] {
  let flags = if $all { [--all-namespaces] } else if ($namespace != null) { [-n $namespace] } else { [] }
  ^kubectl get services ...$flags -o json | from json | get items
  | each {|s| {
      name:       $s.metadata.name
      namespace:  $s.metadata.namespace
      type:       $s.spec.type
      cluster-ip: $s.spec.clusterIP
      ports:      ($s.spec.ports | each { $"($in.port):($in.targetPort)/($in.protocol)" } | str join ",")
      age:        ((date now) - ($s.metadata.creationTimestamp | into datetime))
  }}
}

# Nodes as a table with roles and kubelet version
def kgn [] {
  ^kubectl get nodes -o json | from json | get items
  | each {|n|
      let ready = ($n.status.conditions | where type == "Ready" | first | get status)
      let roles = ($n.metadata.labels | transpose key value
        | where ($in.key | str starts-with "node-role.kubernetes.io/")
        | each { $in.key | str replace "node-role.kubernetes.io/" "" }
        | str join ",")
      {
        name:    $n.metadata.name
        status:  (if $ready == "True" { "Ready" } else { "NotReady" })
        roles:   (if ($roles | is-empty) { "<none>" } else { $roles })
        version: $n.status.nodeInfo.kubeletVersion
        age:     ((date now) - ($n.metadata.creationTimestamp | into datetime))
      }
  }
}

# Kubectl contexts as a table, current marked with *
def ctx [] {
  let current = (^kubectl config current-context | str trim)
  ^kubectl config view -o json | from json | get contexts
  | each {|c| {
      current:   (if $c.name == $current { "*" } else { "" })
      name:      $c.name
      cluster:   $c.context.cluster
      user:      $c.context.user
      namespace: ($c.context.namespace? | default "default")
  }}
}

# Switch kubectl context
def ctx-use [name: string] {
  ^kubectl config use-context $name
}

# Helm releases as a table — optional namespace (-n) or all namespaces (-A)
def helm-ls [namespace?: string, --all (-A)] {
  let flags = if $all { [--all-namespaces] } else if ($namespace != null) { [-n $namespace] } else { [] }
  ^helm list ...$flags -o json | from json
}
