defmodule SpawnOperator.K8s.Activators.Scheduler.CronJob do
  @moduledoc false

  @behaviour SpawnOperator.K8s.Manifest

  @activator_cli_version "0.1.3"

  @impl true
  def manifest(resource, _opts \\ []) do
    Enum.map(resource.params["bindings"]["sources"], fn source ->
      %{"name" => name, "expr" => expr} = source

      sinks =
        Enum.filter(resource.params["bindings"]["sinks"], fn sink ->
          Enum.find(sink["binding"], fn %{"name" => binding_name} -> binding_name == name end)
        end)
        |> Enum.map(fn sink ->
          %{
            "name" => sink["name"],
            "image" => "eigr/spawn-activator-simple:#{@activator_cli_version}",
            "imagePullPolicy" => "IfNotPresent",
            "env" => [
              %{
                "name" => "SPAWN_INTERNAL_NATS_HOSTS",
                "valueFrom" => %{
                  "secretKeyRef" => %{
                    "name" => resource.params["activator"]["externalConnectorRef"],
                    "key" => "url"
                  }
                }
              }
            ],
            "command" => [
              "./activator-simple",
              "#{sink["system"]}",
              "#{sink["actor"]}",
              "#{sink["action"]}"
            ]
          }
        end)

      %{
        "apiVersion" => "batch/v1",
        "kind" => "CronJob",
        "metadata" => %{
          "name" => "#{name}-cron-job",
          "namespace" => resource.namespace
        },
        "spec" => %{
          "schedule" => expr,
          "jobTemplate" => %{
            "spec" => %{
              "template" => %{
                "spec" => %{
                  "containers" => sinks,
                  "restartPolicy" => "OnFailure"
                }
              }
            }
          }
        }
      }
    end)
  end
end
