<commit_before>defmodule Mix.Tasks.Addict.Generate.Configs do
  use Mix.Task

  def run(_) do
    configs_path = "./config/config.exs"

    if addict_config_already_exists?(configs_path) do
      Mix.shell.error "[x] Please remove the existing Addict configuration before generating a new one"
    else
      base_module = guess_application_name
      user_schema = "#{base_module}.User"
      repo        = "#{base_module}.Repo"
      mailgun_domain = ""
      mailgun_api_key = ""

      Mix.shell.info "[o] Generating Addict configuration"

      guessed = Mix.shell.yes? "Is your application root module #{base_module}?"
      unless guessed do
        base_module = Mix.shell.prompt("Please insert your application root module:") |> String.rstrip
      end

      guessed = Mix.shell.yes? "Is your Ecto Repository module #{repo}?"
      unless guessed do
        repo = Mix.shell.prompt("Please insert your Ecto Repository module:") |> String.rstrip
      end

      guessed = Mix.shell.yes? "Is your User Schema module #{user_schema}?"
      unless guessed do
        user_schema = Mix.shell.prompt("Please insert your User Schema module:") |> String.rstrip
      end

      use_mailgun = Mix.shell.yes? "Will you be using Mailgun?"
      if use_mailgun do
        mailgun_domain  = Mix.shell.prompt("Please insert your Mailgun domain: (e.g.: https://api.mailgun.net/v3/sandbox123456.mailgun.org)") |> String.rstrip
        mailgun_api_key = Mix.shell.prompt("Please insert your Mailgun API key:") |> String.rstrip
      end

      add_addict_configs(configs_path, user_schema, repo, use_mailgun, mailgun_domain, mailgun_api_key)
    end

    Mix.shell.info "[o] Done!"
  end

  defp guess_application_name do
    Mix.Project.config()[:app] |> Atom.to_string |> Mix.Utils.camelize
  end

  defp addict_config_already_exists?(configs_path) do
    {data} = with  {:ok, file} <- File.open(configs_path, [:read, :write, :utf8]),
          data <- IO.read(file, :all),
          :ok <- File.close(file),
      do: {data}

    String.contains?(data, "config :addict")
  end

  defp add_addict_configs(configs_path, user_schema, repo, use_mailgun, mailgun_domain, mailgun_api_key) do
    {:ok, file} = File.open(configs_path, [:read, :write, :utf8])
    IO.read(file, :all)
    secret_key = Comeonin.Bcrypt.gen_salt |> Base.encode16 |> String.downcase

    default_configs = """<commit_msg>Fix addict.generate.configs falsly detecting :addcit_demo as existing config<commit_after>defmodule Mix.Tasks.Addict.Generate.Configs do
  use Mix.Task

  def run(_) do
    configs_path = "./config/config.exs"

    if addict_config_already_exists?(configs_path) do
      Mix.shell.error "[x] Please remove the existing Addict configuration before generating a new one"
    else
      base_module = guess_application_name
      user_schema = "#{base_module}.User"
      repo        = "#{base_module}.Repo"
      mailgun_domain = ""
      mailgun_api_key = ""

      Mix.shell.info "[o] Generating Addict configuration"

      guessed = Mix.shell.yes? "Is your application root module #{base_module}?"
      unless guessed do
        base_module = Mix.shell.prompt("Please insert your application root module:") |> String.rstrip
      end

      guessed = Mix.shell.yes? "Is your Ecto Repository module #{repo}?"
      unless guessed do
        repo = Mix.shell.prompt("Please insert your Ecto Repository module:") |> String.rstrip
      end

      guessed = Mix.shell.yes? "Is your User Schema module #{user_schema}?"
      unless guessed do
        user_schema = Mix.shell.prompt("Please insert your User Schema module:") |> String.rstrip
      end

      use_mailgun = Mix.shell.yes? "Will you be using Mailgun?"
      if use_mailgun do
        mailgun_domain  = Mix.shell.prompt("Please insert your Mailgun domain: (e.g.: https://api.mailgun.net/v3/sandbox123456.mailgun.org)") |> String.rstrip
        mailgun_api_key = Mix.shell.prompt("Please insert your Mailgun API key:") |> String.rstrip
      end

      add_addict_configs(configs_path, user_schema, repo, use_mailgun, mailgun_domain, mailgun_api_key)
    end

    Mix.shell.info "[o] Done!"
  end

  defp guess_application_name do
    Mix.Project.config()[:app] |> Atom.to_string |> Mix.Utils.camelize
  end

  defp addict_config_already_exists?(configs_path) do
    {data} = with  {:ok, file} <- File.open(configs_path, [:read, :write, :utf8]),
          data <- IO.read(file, :all),
          :ok <- File.close(file),
      do: {data}

    Regex.match?  ~r/config :addict\s*?,/, data
  end

  defp add_addict_configs(configs_path, user_schema, repo, use_mailgun, mailgun_domain, mailgun_api_key) do
    {:ok, file} = File.open(configs_path, [:read, :write, :utf8])
    IO.read(file, :all)
    secret_key = Comeonin.Bcrypt.gen_salt |> Base.encode16 |> String.downcase

    default_configs = """
{'fraction_non_alphanumeric': 0.11601981833195706, 'fraction_numerical': 0.004541701073492981, 'mean_word_length': 3.2462751971954424, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 21, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4804088', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1560, 'n_words': 386}