defmodule Oban.Pro.MixProject do
  use Mix.Project

  @version "1.6.11"

  def project do
    [
      app: :oban_pro,
      version: @version,
      elixir: "~> 1.15",
      start_permanent: Mix.env() == :prod,
      elixirc_paths: elixirc_paths(Mix.env()),
      prune_code_paths: false,
      deps: deps(),
      docs: docs(),
      aliases: aliases(),
      package: package(),
      name: "Oban Pro",
      description: "Oban Pro Component",
      xref: [exclude: [Oban.JSON, Oban.Validation]],

      # Dialyzer
      dialyzer: [
        plt_add_apps: [:ex_unit, :mix, :postgrex],
        plt_core_path: "_build/#{Mix.env()}",
        flags: [:error_handling, :missing_return, :no_opaque, :underspecs]
      ]
    ]
  end

  def application do
    [
      extra_applications: [:logger],
      mod: {Oban.Pro.Application, []}
    ]
  end

  def cli do
    [
      preferred_envs: [
        "test.ci": :test,
        "test.reset": :test,
        "test.rollback": :test,
        "test.setup": :test
      ]
    ]
  end

  def package do
    [
      organization: "oban",
      files: ~w(lib/pro.ex lib/oban .formatter.exs mix.exs),
      licenses: ["Commercial"],
      links: []
    ]
  end

  defp elixirc_paths(:test), do: ["lib", "test/support"]
  defp elixirc_paths(_env), do: ["lib"]

  defp deps do
    [
      {:oban, "~> 2.19"},
      {:ecto_sql, "~> 3.10"},
      {:postgrex, "~> 0.16", optional: true},

      # Test and Dev
      {:stream_data, "~> 1.1", only: [:test, :dev]},
      {:tz, "~> 0.26", only: [:test, :dev]},
      {:benchee, "~> 1.0", only: [:test, :dev], runtime: false},
      {:credo, "~> 1.7.7-rc.0", only: [:test, :dev], runtime: false},
      {:dialyxir, "~> 1.0", only: [:test, :dev], runtime: false},

      # Dev
      {:ex_doc, "~> 0.21", only: :dev, runtime: false},
      {:makeup_diff, "~> 0.1", only: :dev, runtime: false},
      {:lys_publish, "~> 0.1", only: :dev, path: "../lys_publish", optional: true, runtime: false}
    ]
  end

  defp aliases do
    [
      release: [
        "cmd git tag v#{@version}",
        "cmd git push",
        "cmd git push --tags",
        "hex.publish package --yes",
        "lys.publish"
      ],
      "test.reset": ["ecto.drop -r Oban.Pro.Repo --quiet", "test.setup"],
      "test.rollback": ["ecto.rollback -r Oban.Pro.Repo --all --quiet"],
      "test.setup": [
        "ecto.create -r Oban.Pro.Repo --quiet",
        "ecto.migrate -r Oban.Pro.Repo --quiet"
      ],
      "test.ci": [
        "format --check-formatted",
        "deps.unlock --check-unused",
        "credo --strict",
        "test --raise",
        "dialyzer"
      ]
    ]
  end

  defp docs do
    [
      main: "overview",
      source_ref: "v#{@version}",
      formatters: ["html"],
      api_reference: false,
      extra_section: "GUIDES",
      extras: extras(),
      groups_for_extras: groups_for_extras(),
      groups_for_modules: groups_for_modules(),
      homepage_url: "/",
      logo: "assets/oban-pro-logo.svg",
      assets: %{"assets" => "assets"},
      nest_modules_by_prefix: nest_modules_by_prefix(),
      skip_undefined_reference_warnings_on: ["CHANGELOG.md"],
      before_closing_body_tag: fn _ ->
        """
        <script>document.querySelector('footer.footer p').remove()</script>
        """
      end
    ]
  end

  defp extras do
    [
      "guides/introduction/overview.md",
      "guides/introduction/installation.md",
      "guides/introduction/adoption.md",
      "guides/introduction/composition.md",
      "guides/testing/testing.md",
      "guides/testing/testing_workers.md",
      "guides/deployment/ci_cd.md",
      "guides/deployment/docker.md",
      "guides/deployment/fly.md",
      "guides/deployment/gigalixir.md",
      "guides/deployment/heroku.md",
      "guides/deployment/dependabot.md",
      "guides/upgrading/v1.0.md",
      "guides/upgrading/v1.4.md",
      "guides/upgrading/v1.5.md",
      "guides/upgrading/v1.6.md",
      "CHANGELOG.md": [filename: "changelog", title: "Changelog"]
    ]
  end

  defp groups_for_extras do
    [
      Introduction: ~r/guides\/introduction\/.?/,
      Testing: ~r/guides\/testing\/.?/,
      Deployment: ~r/guides\/deployment\/.?/,
      Upgrading: ~r/guides\/upgrading\/.?/
    ]
  end

  defp groups_for_modules do
    [
      Extensions: [
        Oban.Pro.Decorator,
        Oban.Pro.Relay,
        Oban.Pro.Testing,
        Oban.Pro.Worker
      ],
      Composition: [
        Oban.Pro.Batch,
        Oban.Pro.Workflow
      ],
      Plugins: [
        Oban.Pro.Plugins.DynamicCron,
        Oban.Pro.Plugins.DynamicLifeline,
        Oban.Pro.Plugins.DynamicPartitioner,
        Oban.Pro.Plugins.DynamicPrioritizer,
        Oban.Pro.Plugins.DynamicPruner,
        Oban.Pro.Plugins.DynamicQueues,
        Oban.Pro.Plugins.DynamicScaler
      ],
      Scaling: [
        Oban.Pro.Cloud
      ],
      Workers: [
        Oban.Pro.Workers.Batch,
        Oban.Pro.Workers.Chain,
        Oban.Pro.Workers.Chunk,
        Oban.Pro.Workers.Workflow
      ]
    ]
  end

  defp nest_modules_by_prefix do
    [Oban.Pro, Oban.Pro.Plugins, Oban.Pro.Workers]
  end
end
