import Accent.Config
import Config

alias Ueberauth.Strategy.OIDC

port = get_env("PORT", :integer) || 4000
canonical_uri = get_env("CANONICAL_URL", :uri) || parse_env("http://localhost:#{port}", :uri)
static_uri = get_env("STATIC_URL", :uri) || canonical_uri

if config_env() === :test do
  config :accent, Accent.Endpoint,
    adapter: Bandit.PhoenixAdapter,
    http: [port: 4001],
    server: false,
    url: [port: 80, scheme: "http", host: "example.com"],
    static_url: [port: 80, scheme: "http", host: "example.com"]

  config :accent,
    canonical_host: nil,
    force_ssl: false,
    restricted_domain: nil
else
  config :accent, Accent.Endpoint,
    adapter: Bandit.PhoenixAdapter,
    http: [port: port],
    url: get_endpoint_url_config(canonical_uri),
    secret_key_base: get_env("SECRET_KEY_BASE") || "UNSAFE_pJhJ9q2qmMXYUCR0HyfufqtHQ+W1rqsr9M5q2aYo6Lo=",
    static_url: get_endpoint_url_config(static_uri),
    debug_errors: get_env("DEBUG_ERRORS", :boolean)

  config :accent,
    canonical_host: get_uri_part(canonical_uri, :host),
    disable_canonical_host: get_env("DISABLE_CANONICAL_HOST_REDIRECT", :boolean),
    force_ssl: get_uri_part(canonical_uri, :scheme) === "https",
    restricted_domain: get_env("RESTRICTED_PROJECT_CREATOR_EMAIL_DOMAIN") || get_env("RESTRICTED_DOMAIN")
end

if config_env() === :test do
  config :accent, Accent.Repo,
    pool_size: System.schedulers_online() * 2,
    url: get_env("DATABASE_URL")
else
  ecto_ipv6? = get_env("ECTO_IPV6", :boolean)

  config :accent, Accent.Repo,
    timeout: get_env("DATABASE_TIMEOUT", :integer) || 29_000,
    queue_target: get_env("DATABASE_QUEUE_TARGET", :integer) || 500,
    queue_interval: get_env("DATABASE_QUEUE_INTERVAL", :integer) || 2000,
    pool_size: get_env("DATABASE_POOL_SIZE", :integer),
    ssl: get_env("DATABASE_SSL", :boolean),
    ssl_opts: [verify: :verify_none],
    url: get_env("DATABASE_URL") || "postgres://localhost/accent_development",
    socket_options: if(ecto_ipv6?, do: [:inet6], else: [])
end

providers = []

providers =
  if get_env("GOOGLE_API_CLIENT_ID"),
    do: [{:google, {Ueberauth.Strategy.Google, [scope: "email openid"]}} | providers],
    else: providers

providers =
  if get_env("SLACK_CLIENT_ID"),
    do: [
      {:slack, {Ueberauth.Strategy.Slack, [default_user_scope: "users:read", team: get_env("SLACK_TEAM_ID")]}}
      | providers
    ],
    else: providers

providers =
  if get_env("GITHUB_CLIENT_ID"),
    do: [{:github, {Ueberauth.Strategy.Github, [default_scope: "user"]}} | providers],
    else: providers

providers =
  if get_env("GITLAB_CLIENT_ID"),
    do: [{:gitlab, {Ueberauth.Strategy.Gitlab, [default_scope: "read_user"]}} | providers],
    else: providers

providers =
  if get_env("DISCORD_CLIENT_ID"),
    do: [{:discord, {Ueberauth.Strategy.Discord, [default_scope: "identify email"]}} | providers],
    else: providers

providers =
  if get_env("MICROSOFT_CLIENT_ID"),
    do: [{:microsoft, {Ueberauth.Strategy.Microsoft, [prompt: "select_account"]}} | providers],
    else: providers

providers = if get_env("AUTH0_CLIENT_ID"), do: [{:auth0, {Ueberauth.Strategy.Auth0, []}} | providers], else: providers

providers =
  if get_env("OIDC_CLIENT_ID"),
    do: [{:oidc, {OIDC, [default: [provider: :default_oidc, uid_field: :sub]]}} | providers],
    else: providers

providers =
  if get_env("DUMMY_LOGIN_ENABLED"),
    do: [{:dummy, {Accent.Auth.Ueberauth.DummyStrategy, []}} | providers],
    else: providers

config :accent, Accent.MachineTranslations,
  default_providers_config: %{
    "google_translate" => %{"key" => get_env("GOOGLE_TRANSLATIONS_SERVICE_ACCOUNT_KEY")},
    "deepl" => %{"key" => get_env("DEEPL_TRANSLATIONS_KEY")}
  }

config :accent, Accent.Prompts,
  use_provider_by_default: get_env("AI_ASSISTANT_USE_DEFAULT_PROVIDER", :string),
  default_providers_config: %{
    "openai" => %{
      "key" => get_env("OPENAI_API_KEY"),
      "model" => get_env("OPENAI_API_MODEL") || "gpt-4o",
      "base_url" => get_env("OPENAI_API_BASE_URL") || "https://api.openai.com/v1/"
    }
  }

config :accent, Accent.WebappView,
  path: "priv/static/webapp/index.html",
  sentry_dsn: get_env("WEBAPP_SENTRY_DSN") || "",
  skip_subresource_integrity: get_env("WEBAPP_SKIP_SUBRESOURCE_INTEGRITY", :boolean)

config :accent, LanguageTool, languages: get_env("LANGUAGE_TOOL_LANGUAGES", :comma_separated_list)

config :new_relic_agent,
  app_name: get_env("NEW_RELIC_APP_NAME"),
  license_key: get_env("NEW_RELIC_LICENSE_KEY")

config :tesla, logger_enabled: true

config :ueberauth, OIDC,
  default_oidc: [
    fetch_userinfo: true,
    uid_field: get_env("OIDC_UID_FIELD") || "sub",
    client_id: get_env("OIDC_CLIENT_ID"),
    client_secret: get_env("OIDC_CLIENT_SECRET"),
    discovery_document_uri: get_env("OIDC_DISCOVERY_URI"),
    redirect_uri: "#{static_uri}/auth/oidc/callback",
    response_type: "code",
    scope: get_env("OIDC_SCOPE") || "openid profile email"
  ]

config :ueberauth, Ueberauth, providers: providers

config :ueberauth, Ueberauth.Strategy.Auth0.OAuth,
  domain: System.get_env("AUTH0_DOMAIN"),
  client_id: System.get_env("AUTH0_CLIENT_ID"),
  client_secret: System.get_env("AUTH0_CLIENT_SECRET")

config :ueberauth, Ueberauth.Strategy.Discord.OAuth,
  client_id: get_env("DISCORD_CLIENT_ID"),
  client_secret: get_env("DISCORD_CLIENT_SECRET")

config :ueberauth, Ueberauth.Strategy.Github.OAuth,
  client_id: get_env("GITHUB_CLIENT_ID"),
  client_secret: get_env("GITHUB_CLIENT_SECRET")

config :ueberauth, Ueberauth.Strategy.Gitlab.OAuth,
  client_id: get_env("GITLAB_CLIENT_ID"),
  client_secret: get_env("GITLAB_CLIENT_SECRET"),
  redirect_uri: "#{static_uri}/auth/gitlab/callback",
  site: get_env("GITLAB_SITE_URL") || "https://gitlab.com",
  authorize_url: "#{get_env("GITLAB_SITE_URL") || "https://gitlab.com"}/oauth/authorize",
  token_url: "#{get_env("GITLAB_SITE_URL") || "https://gitlab.com"}/oauth/token"

config :ueberauth, Ueberauth.Strategy.Google.OAuth,
  client_id: get_env("GOOGLE_API_CLIENT_ID"),
  client_secret: get_env("GOOGLE_API_CLIENT_SECRET")

config :ueberauth, Ueberauth.Strategy.Microsoft.OAuth,
  client_id: get_env("MICROSOFT_CLIENT_ID"),
  client_secret: get_env("MICROSOFT_CLIENT_SECRET"),
  tenant_id: get_env("MICROSOFT_TENANT_ID")

config :ueberauth, Ueberauth.Strategy.Slack.OAuth,
  client_id: get_env("SLACK_CLIENT_ID"),
  client_secret: get_env("SLACK_CLIENT_SECRET")

if get_env("SENTRY_DSN") do
  config :sentry,
    dsn: get_env("SENTRY_DSN"),
    environment_name: get_env("SENTRY_ENVIRONMENT_NAME"),
    included_environments: ~w(dev prod production),
    root_source_code_path: File.cwd!()
else
  config :sentry, included_environments: []
end

if get_env("TZDATA_AUTOUPDATE_DISABLED") do
  config :tzdata, :autoupdate, :disabled
end

config :accent, Accent.Mailer,
  mailer_from: get_env("MAILER_FROM"),
  x_smtpapi_header: get_env("SMTP_API_HEADER")

cond do
  get_env("SENDGRID_API_KEY") ->
    config :accent, Accent.Mailer,
      adapter: Bamboo.SendGridAdapter,
      api_key: get_env("SENDGRID_API_KEY")

  get_env("MANDRILL_API_KEY") ->
    config :accent, Accent.Mailer,
      adapter: Bamboo.MandrillAdapter,
      api_key: get_env("MANDRILL_API_KEY")

  get_env("MAILGUN_API_KEY") ->
    config :accent, Accent.Mailer,
      adapter: Bamboo.MailgunAdapter,
      api_key: get_env("MAILGUN_API_KEY"),
      domain: get_env("MAILGUN_DOMAIN"),
      base_uri: get_env("MAILGUN_BASE_URI")

  get_env("SMTP_ADDRESS") ->
    config :accent, Accent.Mailer,
      adapter: BambooSMTPAdapterWithTlsOptions,
      server: get_env("SMTP_ADDRESS"),
      port: get_env("SMTP_PORT"),
      username: get_env("SMTP_USERNAME"),
      password: get_env("SMTP_PASSWORD")

  config_env() == :test ->
    config :accent, Accent.Mailer,
      mailer_from: "accent-test@example.com",
      x_smtpapi_header: ~s({"category": ["test", "accent-api-test"]}),
      adapter: Bamboo.TestAdapter

  true ->
    config :accent, Accent.Mailer, adapter: Bamboo.LocalAdapter
end
