import Config

get_env_as_boolean = fn key, default ->
  System.get_env(key, default)
  |> String.downcase()
  |> String.trim()
  |> case do
    "true" -> true
    _ -> false
  end
end

if System.get_env("ADD_OBAN_CRONTAB_IN_DEV", "false") == "true" do
  config :oli, Oban, plugins: [Oban.Plugins.Pruner, {Oban.Plugins.Cron, crontab: []}]
else
  nil
end

config :oli,
  env: :dev,
  s3_xapi_bucket_name: System.get_env("S3_XAPI_BUCKET_NAME", "torus-xapi-dev"),
  s3_media_bucket_name: System.get_env("S3_MEDIA_BUCKET_NAME", "torus-media-dev"),
  media_url: System.get_env("MEDIA_URL", "http://localhost:9000/torus-media-dev"),
  problematic_query_detection:
    get_env_as_boolean.("DEV_PROBLEMATIC_QUERY_DETECTION_ENABLED", "false"),
  load_testing_mode: get_env_as_boolean.("LOAD_TESTING_MODE", "false"),
  slack_webhook_url: System.get_env("SLACK_WEBHOOK_URL"),
  blackboard_application_client_id: System.get_env("BLACKBOARD_APPLICATION_CLIENT_ID"),
  branding: [
    name: System.get_env("BRANDING_NAME", "OLI Torus"),
    logo: System.get_env("BRANDING_LOGO", "/branding/dev/oli_torus_logo.png"),
    logo_dark:
      System.get_env(
        "BRANDING_LOGO_DARK",
        System.get_env("BRANDING_LOGO", "/branding/dev/oli_torus_logo_dark.png")
      ),
    favicons: System.get_env("BRANDING_FAVICONS_DIR", "/branding/dev/favicons")
  ],
  log_incomplete_requests: get_env_as_boolean.("LOG_INCOMPLETE_REQUESTS", "true")

config :oli, :vendor_property,
  workspace_logo:
    System.get_env("VENDOR_PROPERTY_WORKSPACE_LOGO", "/branding/dev/oli_torus_icon.png"),
  support_email: System.get_env("VENDOR_PROPERTY_SUPPORT_EMAIL", "support@example.com"),
  knowledgebase_url: System.get_env("KNOWLEDGEBASE_URL", "#")

# Configure your database
config :oli, Oli.Repo,
  username: System.get_env("DB_USER", "postgres"),
  password: System.get_env("DB_PASSWORD", "postgres"),
  database: System.get_env("DB_NAME", "oli_dev"),
  hostname: System.get_env("DB_HOST", "localhost"),
  show_sensitive_data_on_connection_error: true,
  pool_size: 10,
  timeout: 600_000,
  ownership_timeout: 600_000,
  log: String.to_existing_atom(System.get_env("DEV_DB_LOG_LEVEL", "debug"))

# Configure email for development
config :oli, Oli.Mailer, adapter: Swoosh.Adapters.Local

config :oli,
  ecl_username: System.get_env("ECL_USERNAME", ""),
  ecl_password: System.get_env("ECL_PASSWORD", "")

config :oli, :stripe_provider,
  public_secret: "pk_test_TYooMQauvdEDq54NiTphI7jx",
  private_secret: "sk_test_4eC39HqLyjWDarjtT1zdp7dc"

config :oli, :cashnet_provider,
  cashnet_store: System.get_env("CASHNET_STORE"),
  cashnet_checkout_url: System.get_env("CASHNET_CHECKOUT_URL"),
  cashnet_client: System.get_env("CASHNET_CLIENT"),
  cashnet_gl_number: System.get_env("CASHNET_GL_NUMBER")

# For development, we disable any cache and enable
# debugging and code reloading.
#
# The watchers configuration can be used to run external
# watchers to your application. For example, we can use it
# to bundle .js and .css sources.
config :oli, OliWeb.Endpoint,
  http: [
    port: String.to_integer(System.get_env("HTTP_PORT", "80"))
  ],
  url: [
    scheme: System.get_env("SCHEME", "http"),
    host: System.get_env("HOST", "localhost"),
    port: String.to_integer(System.get_env("PORT", "80"))
  ],
  https: [
    port: String.to_integer(System.get_env("HTTPS_PORT", "443")),
    otp_app: :oli,
    keyfile: System.get_env("SSL_KEY_PATH", "priv/ssl/localhost.key"),
    certfile: System.get_env("SSL_CERT_PATH", "priv/ssl/localhost.crt")
  ],
  check_origin: false,
  code_reloader: true,
  debug_errors: true,
  secret_key_base: "7xBZ6iTMM2Y3Q6x9Gdr5VXYFxX1HCw5y2O+fr5jN8ZBhm5LH/alqyEtd/TjIRmV+",
  watchers: [
    node: [
      "node_modules/webpack/bin/webpack.js",
      "--mode",
      "development",
      "--watch",
      "--watch-options-stdin",
      cd: Path.expand("../assets", __DIR__)
    ],
    node: [
      "node_modules/webpack/bin/webpack.js",
      "--config",
      "webpack.config.node.js",
      "--mode",
      "production",
      "--watch",
      "--watch-options-stdin",
      cd: Path.expand("../assets", __DIR__)
    ],
    tailwind: {Tailwind, :install_and_run, [:default, ~w(--watch)]}
  ]

# ## SSL Support
#
# In order to use HTTPS in development, a self-signed
# certificate can be generated by running the following
# Mix task:
#
#     mix phx.gen.cert
#
# Note that this task requires Erlang/OTP 20 or later.
# Run `mix help phx.gen.cert` for more information.
#
# The `http:` config above can be replaced with:
#
#     https: [
#       port: 4001,
#       cipher_suite: :strong,
#       keyfile: "priv/cert/selfsigned_key.pem",
#       certfile: "priv/cert/selfsigned.pem"
#     ],
#
# If desired, both `http:` and `https:` keys can be
# configured to run both http and https servers on
# different ports.

# Watch static and templates for browser reloading.
config :oli, OliWeb.Endpoint,
  live_reload: [
    patterns: [
      ~r"priv/static/.*(js|css|png|jpeg|jpg|gif|svg)$",
      ~r"priv/gettext/.*(po)$",
      ~r"lib/oli_web/{live,views}/.*(ex)$",
      ~r"lib/oli_web/templates/.*(eex)$"
    ]
  ]

# Do not include metadata nor timestamps in development logs
config :logger, :console, format: "[$level] $message\n"

truncate =
  System.get_env("LOGGER_TRUNCATE", "8192")
  |> String.downcase()
  |> case do
    "infinity" ->
      :infinity

    val ->
      String.to_integer(val)
  end

config :logger, truncate: truncate

# Set a higher stacktrace during development. Avoid configuring such
# in production as building large stacktraces may be expensive.
config :phoenix, :stacktrace_depth, 20

# Initialize plugs at runtime for faster development compilation
config :phoenix, :plug_init_mode, :runtime

config :phoenix_live_view,
  debug_heex_annotations: true,
  debug_attributes: true,
  enable_expensive_runtime_checks: true

# Configure Joken for jwt signing and verification
config :joken, default_signer: "secret"

config :appsignal, :config, active: false

# Configure AWS
config :ex_aws,
  region: System.get_env("AWS_REGION", "us-east-1"),
  access_key_id: [{:system, "AWS_ACCESS_KEY_ID"}],
  secret_access_key: [{:system, "AWS_SECRET_ACCESS_KEY"}]

# Configure S3 specifically
config :ex_aws, :s3,
  region: System.get_env("AWS_REGION", "us-east-1"),
  access_key_id: [
    {:system, "AWS_S3_ACCESS_KEY_ID"},
    {:system, "AWS_ACCESS_KEY_ID"},
    "your_minio_access_key"
  ],
  secret_access_key: [
    {:system, "AWS_S3_SECRET_ACCESS_KEY"},
    {:system, "AWS_SECRET_ACCESS_KEY"},
    "your_minio_secret_key"
  ],
  scheme: System.get_env("AWS_S3_SCHEME", "http") <> "://",
  port: String.to_integer(System.get_env("AWS_S3_PORT", "9000")),
  host: System.get_env("AWS_S3_HOST", "localhost")

config :oli, :xapi_upload_pipeline,
  producer_module: Oli.Analytics.XAPI.QueueProducer,
  uploader_module: Oli.Analytics.XAPI.LocalFileUploader,
  suppress_event_emitting: System.get_env("SUPPRESS_DEV_EVENT_EMITTING", "true") == "true",
  xapi_local_output_dir: System.get_env("XAPI_LOCAL_OUTPUT_DIR", "./xapi_output")

config :ex_aws, :hackney_opts,
  follow_redirect: true,
  recv_timeout: 200_000

# Configure development reCAPTCHA. This is a publicly available test key and will
# render a warning to prevent it from being used in production.
# https://developers.google.com/recaptcha/docs/faq#id-like-to-run-automated-tests-with-recaptcha.-what-should-i-do
config :oli, :recaptcha,
  verify_url: "https://www.google.com/recaptcha/api/siteverify",
  timeout: 5000,
  site_key: System.get_env("RECAPTCHA_SITE_KEY", "6LeIxAcTAAAAAJcZVRqyHh71UMIEGNQ_MXjiZKhI"),
  secret: System.get_env("RECAPTCHA_PRIVATE_KEY", "6LeIxAcTAAAAAGG-vFI1TnRWxMZNFuojJ4WifJWe")

config :oli, Oli.Vault,
  json_library: Jason,
  ciphers: [
    default:
      {Cloak.Ciphers.AES.GCM,
       tag: "AES.GCM.V1", key: Base.decode64!("HXCdm5z61eNgUpnXObJRv94k3JnKSrnfwppyb60nz6w=")}
  ]
