import Config

# Important: indicate date of last release, to generate a changelog for issues closed since then
changelog_issues_closed_after = "2022-08-30"

config :bonfire,
  # Note: you can run `Bonfire.Common.Config.put(:experimental_features_enabled, true)` to enable these in prod too
  experimental_features_enabled: true,
  # low limit so it is easier to test UX
  default_pagination_limit: 15,
  ui: [
    feed_object_extension_preloads_disabled: false
  ]

# config :pseudo_gettext, :locale, "en-pseudo_text" # uncomment to use https://en.wikipedia.org/wiki/Pseudolocalization and check that the app is properly localisable

config :bonfire, Bonfire.Common.Repo,
  database: System.get_env("POSTGRES_DB", "bonfire_dev"),
  # show_sensitive_data_on_connection_error: true,
  pool_size: String.to_integer(System.get_env("POOL_SIZE") || "10"),
  # EctoSparkles does the logging instead
  log: false,
  stacktrace: true

local_deps =
  Mess.read_umbrella(
    config_dir: "config/",
    use_local_forks?: System.get_env("WITH_FORKS", "1") == "1"
  )

# if System.get_env("WITH_FORKS", "1") == "1" , do:
# Mess.deps(
#   [path: Path.relative_to_cwd("config/deps.path")],
#   []
# ),
# else: []

# Watch static and templates for browser reloading.
config :bonfire, Bonfire.Web.Endpoint,
  server: true,
  debug_errors: false,
  check_origin: false,
  http: [protocol_options: [idle_timeout: 120_000]],
  watchers: [
    yarn: [
      "watch.js",
      cd: Path.expand("assets", File.cwd!())
    ],
    yarn: [
      "watch.css",
      cd: Path.expand("assets", File.cwd!())
    ],
    yarn: [
      "watch.assets",
      cd: Path.expand("assets", File.cwd!())
    ]
  ]

enable_code_reloading = System.get_env("HOT_CODE_RELOAD") != "0"

if enable_code_reloading do
  local_dep_names = Enum.map(local_deps, &elem(&1, 0))

  dep_paths =
    Enum.map(local_deps, fn dep ->
      case elem(dep, 1)[:path] do
        nil -> nil
        path -> "#{path}/lib"
      end
    end)
    |> Enum.reject(&is_nil/1)

  watch_paths = dep_paths ++ ["lib/"] ++ ["priv/static/"]

  IO.puts("Watching these deps for code reloading: #{inspect(local_dep_names)}")

  config :phoenix_live_reload,
    # watch the app's lib/ dir + the dep/lib/ dir of every locally-cloned dep
    dirs: watch_paths

  # filename patterns that should trigger page reloads (only within the above dirs)
  patterns = [
    ~r"^priv/static/.*(js|css|png|jpeg|jpg|gif|svg|webp)$",
    # ~r"^priv/gettext/.*(po)$",
    ~r{(web|templates)/.*(ex)$},
    ~r"(live_handler|live_handlers|routes)\.ex$"
  ]

  # filename patterns that should trigger hots reloads of components/CSS/etc (only within the above dirs)
  hot_patterns = [
    ~r"(_live)\.ex$",
    ~r{(live|views|pages|components)/.*(ex|css)$},
    ~r".*(heex|leex|sface)$",
    ~r"priv/catalogue/.*(ex)$"
  ]

  IO.puts("Watching these filenames for live reloading in the browser: #{inspect(patterns)}")

  config :bonfire, Bonfire.Web.Endpoint,
    code_reloader: true,
    reloadable_apps: [:bonfire] ++ local_dep_names,
    live_reload: [
      patterns: patterns ++ hot_patterns,
      notify: [
        live_view: hot_patterns
      ]
    ]
end

config :bonfire, Bonfire.Web.Endpoint, phoenix_profiler: [server: Bonfire.Web.Profiler]

config :surface,
  log_level: :debug

config :logger,
  level: :debug,
  # level: :info,
  # level: :warning,
  truncate: :infinity

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

config :phoenix, :stacktrace_depth, 30

config :phoenix, :plug_init_mode, :runtime

config :exsync,
  src_monitor: true,
  reload_timeout: 75,
  # addition_dirs: ["/forks"],
  extra_extensions: [".leex", ".heex", ".js", ".css", ".sface"]

config :versioce, :changelog,
  # Or your own datagrabber module
  datagrabber: Versioce.Changelog.DataGrabber.Git,
  # Or your own formatter module
  formatter: Versioce.Changelog.Formatter.Keepachangelog

config :versioce, :changelog,
  closed_after: changelog_issues_closed_after,
  changelog_file: "docs/CHANGELOG-autogenerated.md",
  datagrabber: Bonfire.Common.Changelog.Github.DataGrabber,
  anchors: %{
    added: ["Feature"],
    changed: ["Improvement", "UI/UX", "Refactor"],
    deprecated: ["[DEP]"],
    removed: ["[REM]"],
    fixed: ["Bug"],
    security: ["Security", "Safety"]
  }

# config :source_inspector, :enabled, true
config :phoenix_live_view, debug_heex_annotations: true
