import Config

alias Fishjam.ConfigReader

# config/runtime.exs is executed for all environments, including
# during releases. It is executed after compilation and before the
# system starts, so it is typically used to load production configuration
# and secrets from environment variables or elsewhere. Do not define
# any compile-time configuration in here, as it won't be applied.
# The block below contains prod specific runtime configuration.
config :ex_dtls, impl: :nif

structured_logging? = ConfigReader.read_boolean("FJ_STRUCTURED_LOGGING") || false
config :logger, backends: [if(structured_logging?, do: LoggerJSON, else: :console)]

config :logger, level: ConfigReader.read_logger_level()

prod? = config_env() == :prod

ip = ConfigReader.read_ip("FJ_IP") || Application.fetch_env!(:fishjam, :ip)

port = ConfigReader.read_port("FJ_PORT") || Application.fetch_env!(:fishjam, :port)

host =
  case ConfigReader.read_string("FJ_HOST") do
    nil -> "#{:inet.ntoa(ip)}:#{port}"
    other -> other
  end

components_used = ConfigReader.read_components_used()
sip_used? = Fishjam.Component.SIP in components_used

config :fishjam,
  jwt_max_age: 24 * 3600,
  media_files_path:
    ConfigReader.read_string("FJ_RESOURCES_BASE_PATH", "fishjam_resources") |> Path.expand(),
  address: host,
  metrics_ip: ConfigReader.read_ip("FJ_METRICS_IP") || {127, 0, 0, 1},
  metrics_port: ConfigReader.read_port("FJ_METRICS_PORT") || 9568,
  dist_config: ConfigReader.read_dist_config(),
  webrtc_config: ConfigReader.read_webrtc_config(),
  components_used: components_used,
  sip_config: ConfigReader.read_sip_config(sip_used?),
  s3_config: ConfigReader.read_s3_config(),
  git_commit: ConfigReader.read_git_commit()

case ConfigReader.read_string("FJ_SERVER_API_TOKEN") do
  nil when prod? == true ->
    raise """
    environment variable FJ_SERVER_API_TOKEN is missing.
    FJ_SERVER_API_TOKEN is used for HTTP requests and
    server WebSocket authorization.
    """

  nil ->
    :ok

  token ->
    config :fishjam, server_api_token: token
end

external_uri = URI.parse("//" <> host)

config :fishjam, FishjamWeb.Endpoint,
  secret_key_base:
    ConfigReader.read_string("FJ_SECRET_KEY_BASE") || Base.encode64(:crypto.strong_rand_bytes(48)),
  url: [
    host: external_uri.host,
    port: external_uri.port || 443,
    path: external_uri.path || "/"
  ]

# In order to use HTTPS in development, a self-signed
# certificate can be generated by running the following
# Mix task: mix phx.gen.cert
case ConfigReader.read_ssl_config() do
  {ssl_key_path, ssl_cert_path} ->
    config :fishjam, FishjamWeb.Endpoint,
      https: [
        ip: ip,
        port: port,
        cipher_suite: :strong,
        keyfile: ssl_key_path,
        certfile: ssl_cert_path
      ]

  nil ->
    config :fishjam, FishjamWeb.Endpoint, http: [ip: ip, port: port]
end

config :fishjam,
  feature_flags: [
    request_routing_enabled?:
      ConfigReader.read_boolean("FJ_FEATURE_FLAG_REQUEST_ROUTING_ENABLED") || false
  ]

check_origin = ConfigReader.read_check_origin("FJ_CHECK_ORIGIN")

if check_origin != nil do
  config :fishjam, FishjamWeb.Endpoint, check_origin: check_origin
end

if prod? do
  config :fishjam, FishjamWeb.Endpoint, url: [scheme: "https"]
end
