{ lib, config, ... }:
let
  cfg = config.apps.atlantis;
  env = config.apps.env;

  values = lib.apps.appValues {
    inherit env;
    base = ../values/atlantis;
    extraValues = {};
  };

  kustomize = r:
    if r.kind == "Deployment" then
      lib.attrsets.recursiveUpdate r {
        spec.template.spec.containers =
        builtins.map (x:
        x // {
            livenessProbe.httpGet.path = "/healthz";
            readinessProble.httpGet.path = "/healthz";
            env = x.env ++ [ { name = "INERNAL_PORT"; value = 8000; } ];
        }) r.spec.template.spec.containers;
      }
      else if r.kind == "Service" then
      {}
    else r;
in
{
  options.apps.atlantis = lib.apps.appOptions {
      revision = lib.mkOption {
        type = lib.types.str;
        default = "main";
        description = "Revision";
      };

      hostname = lib.mkOption {
        type = lib.types.str;
        default = if env == "prod"
          then "maps.oceanbox.io"
          else "atlantis.beta.oceanbox.io";
        description = "Revision";
      };
  };

  config = lib.apps.appConfig cfg "${env}-atlantis" {
    helm.releases."${env}-atlantis" = {
      inherit values;
      chart = ../charts/atlantis;
      transformer = rs: builtins.map (x: kustomize x) rs;
    };
  };
}
