defmodule Sanbase.Alert.TriggerOperationTextTest do
  use Sanbase.DataCase, async: true

  alias Sanbase.Alert.OperationText

  describe "combinators" do
    test "all_of text" do
      value = %{
        identifier: "santiment",
        current: 15,
        previous: 10,
        previous_average: 12,
        absolute_change: 5,
        percent_change: 50
      }

      operation = %{all_of: [%{amount_up: 4}, %{above: 10}, %{below: 20}]}

      {template, kv} = OperationText.to_template_kv(value, operation)
      text = Sanbase.TemplateEngine.run!(template, params: kv)

      assert text =~ "is below 20"
      assert text =~ "is above 10"
      assert text =~ "increased by 5"
    end

    test "some_of text" do
      value = %{
        identifier: "santiment",
        current: 15,
        previous: 10,
        previous_average: 12,
        absolute_change: 5,
        percent_change: 50
      }

      operation = %{some_of: [%{amount_up: 4}, %{amount_down: 10}, %{above: 10}, %{below: 2}]}

      {template, kv} = OperationText.to_template_kv(value, operation)
      text = Sanbase.TemplateEngine.run!(template, params: kv)

      assert text =~ "is above 10"
      assert text =~ "increased by 5"
      refute text =~ "is below 2"
      refute text =~ "decreased by 10"
    end

    test "none_of text" do
      value = %{
        identifier: "santiment",
        current: 15,
        previous: 10,
        previous_average: 12,
        absolute_change: 5,
        percent_change: 50
      }

      operation = %{none_of: [%{amount_up: 40}, %{amount_down: 10}, %{above: 100}, %{below: 2}]}

      {template, kv} = OperationText.to_template_kv(value, operation)
      text = Sanbase.TemplateEngine.run!(template, params: kv)

      assert text =~ "is not above 100"
      assert text =~ "did not increase by 40"
      assert text =~ "is not below 2"
      assert text =~ "did not decrease by 10"
    end
  end

  describe "details" do
    test "metric with aggregation first" do
      {template, kv} =
        OperationText.details(:metric, %{time_window: "1d", metric: "daily_opening_price_usd"})

      text = Sanbase.TemplateEngine.run!(template, params: kv)

      assert text =~ "Generated by the value of the metric at"
    end

    test "metric with aggregation last" do
      {template, kv} =
        OperationText.details(:metric, %{time_window: "1d", metric: "active_addresses_24h"})

      text = Sanbase.TemplateEngine.run!(template, params: kv)

      assert text =~ "Generated by the value of the metric at"
    end

    test "metric with aggregation sum" do
      {template, kv} =
        OperationText.details(:metric, %{time_window: "1d", metric: "transaction_volume"})

      text = Sanbase.TemplateEngine.run!(template, params: kv)

      assert text =~ "Generated by the sum of all metric values in the interval"
    end

    test "metric with aggregation avg" do
      {template, kv} =
        OperationText.details(:metric, %{time_window: "1d", metric: "daily_active_addresses"})

      text = Sanbase.TemplateEngine.run!(template, params: kv)

      assert text =~ "Generated by the average value of the metric in the interval"
    end

    test "metric with aggregation max" do
      {template, kv} =
        OperationText.details(:metric, %{time_window: "1d", metric: "daily_high_price_usd"})

      text = Sanbase.TemplateEngine.run!(template, params: kv)

      assert text =~ "Generated by the max value of the metric in the interval"
    end

    test "metric with aggregation min" do
      {template, kv} =
        OperationText.details(:metric, %{time_window: "1d", metric: "daily_low_price_usd"})

      text = Sanbase.TemplateEngine.run!(template, params: kv)

      assert text =~ "Generated by the min value of the metric in the interval"
    end
  end
end
