/*
 * Copyright The OpenTelemetry Authors
 * SPDX-License-Identifier: Apache-2.0
 */

package io.opentelemetry.contrib.metrics.micrometer.internal.instruments;

import static org.assertj.core.api.Assertions.assertThat;

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.FunctionCounter;
import io.micrometer.core.instrument.Meter;
import io.micrometer.core.instrument.Tag;
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
import io.opentelemetry.api.common.AttributeKey;
import io.opentelemetry.api.common.Attributes;
import io.opentelemetry.api.incubator.metrics.ExtendedLongCounterBuilder;
import io.opentelemetry.api.metrics.LongCounter;
import io.opentelemetry.api.metrics.LongCounterBuilder;
import io.opentelemetry.api.metrics.ObservableLongCounter;
import io.opentelemetry.context.Context;
import io.opentelemetry.contrib.metrics.micrometer.TestCallbackRegistrar;
import io.opentelemetry.contrib.metrics.micrometer.internal.Constants;
import io.opentelemetry.contrib.metrics.micrometer.internal.state.MeterProviderSharedState;
import io.opentelemetry.contrib.metrics.micrometer.internal.state.MeterSharedState;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.atomic.AtomicLong;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class MicrometerLongCounterTest {

  SimpleMeterRegistry meterRegistry;

  List<Runnable> callbacks;

  TestCallbackRegistrar callbackRegistrar;

  MeterProviderSharedState meterProviderSharedState;

  MeterSharedState meterSharedState;

  @BeforeEach
  void setUp() {
    meterRegistry = new SimpleMeterRegistry();
    callbacks = new ArrayList<>();
    callbackRegistrar = new TestCallbackRegistrar(callbacks);
    meterProviderSharedState = new MeterProviderSharedState(() -> meterRegistry, callbackRegistrar);
    meterSharedState = new MeterSharedState(meterProviderSharedState, "meter", "1.0", null);
  }

  @Test
  void add() {
    LongCounter underTest =
        MicrometerLongCounter.builder(meterSharedState, "counter")
            .setDescription("description")
            .setUnit("unit")
            .build();

    underTest.add(10);

    Counter counter = meterRegistry.find("counter").counter();
    assertThat(counter).isNotNull();
    Meter.Id id = counter.getId();
    assertThat(id.getName()).isEqualTo("counter");
    assertThat(id.getTags())
        .isEqualTo(
            Arrays.asList(
                Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
                Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0")));
    assertThat(id.getDescription()).isEqualTo("description");
    assertThat(id.getBaseUnit()).isEqualTo("unit");
    assertThat(counter.count()).isEqualTo(10.0);

    // test that counter can be increased
    underTest.add(10);
    assertThat(counter.count()).isEqualTo(20.0);

    // test that counter cannot be decreased
    underTest.add(-5);
    assertThat(counter.count()).isEqualTo(20.0);

    double expectedCount = 20.0;
    for (long value : RandomUtils.randomLongs(10, 0L, 500L)) {
      expectedCount += value;

      underTest.add(value);
      assertThat(counter.count()).isEqualTo(expectedCount);
    }
  }

  @Test
  void addWithAttributes() {
    LongCounter underTest =
        MicrometerLongCounter.builder(meterSharedState, "counter")
            .setDescription("description")
            .setUnit("unit")
            .build();

    assertThat(meterRegistry.getMeters()).isEmpty();

    Attributes attributes = Attributes.builder().put("key", "value").build();
    underTest.add(10, attributes);

    Counter counter = meterRegistry.find("counter").counter();
    assertThat(counter).isNotNull();
    Meter.Id id = counter.getId();
    assertThat(id.getName()).isEqualTo("counter");
    assertThat(id.getTags())
        .isEqualTo(
            Arrays.asList(
                Tag.of("key", "value"),
                Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
                Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0")));
    assertThat(id.getDescription()).isEqualTo("description");
    assertThat(id.getBaseUnit()).isEqualTo("unit");
    assertThat(counter.count()).isEqualTo(10.0);

    // test that counter can be increased
    underTest.add(10, attributes);
    assertThat(counter.count()).isEqualTo(20.0);

    // test that counter cannot be decreased
    underTest.add(-5, attributes);
    assertThat(counter.count()).isEqualTo(20.0);

    double expectedCount = 20.0;
    for (long value : RandomUtils.randomLongs(10, 0L, 500L)) {
      expectedCount += value;

      underTest.add(value, attributes);
      assertThat(counter.count()).isEqualTo(expectedCount);
    }
  }

  @Test
  void addWithAttributesAndContext() {
    LongCounter underTest =
        MicrometerLongCounter.builder(meterSharedState, "counter")
            .setDescription("description")
            .setUnit("unit")
            .build();

    assertThat(meterRegistry.getMeters()).isEmpty();

    Attributes attributes = Attributes.builder().put("key", "value").build();
    underTest.add(10, attributes, Context.root());

    Counter counter = meterRegistry.find("counter").counter();
    assertThat(counter).isNotNull();
    Meter.Id id = counter.getId();
    assertThat(id.getName()).isEqualTo("counter");
    assertThat(id.getTags())
        .isEqualTo(
            Arrays.asList(
                Tag.of("key", "value"),
                Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
                Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0")));
    assertThat(id.getDescription()).isEqualTo("description");
    assertThat(id.getBaseUnit()).isEqualTo("unit");
    assertThat(counter.count()).isEqualTo(10.0);

    // test that counter can be increased
    underTest.add(10, attributes, Context.root());
    assertThat(counter.count()).isEqualTo(20.0);

    // test that counter cannot be decreased
    underTest.add(-5, attributes, Context.root());
    assertThat(counter.count()).isEqualTo(20.0);

    double expectedCount = 20.0;
    for (long value : RandomUtils.randomLongs(10, 0L, 500L)) {
      expectedCount += value;

      underTest.add(value, attributes, Context.root());
      assertThat(counter.count()).isEqualTo(expectedCount);
    }
  }

  @Test
  void addWithAttributesAndAttributesAdvice() {
    LongCounterBuilder builder =
        MicrometerLongCounter.builder(meterSharedState, "counter")
            .setDescription("description")
            .setUnit("unit");

    ((ExtendedLongCounterBuilder) builder)
        .setAttributesAdvice(
            Arrays.asList(
                AttributeKey.stringKey(Constants.OTEL_INSTRUMENTATION_NAME),
                AttributeKey.stringKey(Constants.OTEL_INSTRUMENTATION_VERSION),
                AttributeKey.stringKey("key")));

    LongCounter underTest = builder.build();

    assertThat(meterRegistry.getMeters()).isEmpty();

    Attributes attributes =
        Attributes.builder().put("key", "value").put("unwanted", "value").build();
    underTest.add(10, attributes);

    Counter counter = meterRegistry.find("counter").counter();
    assertThat(counter).isNotNull();
    Meter.Id id = counter.getId();
    assertThat(id.getName()).isEqualTo("counter");
    assertThat(id.getTags())
        .isEqualTo(
            Arrays.asList(
                Tag.of("key", "value"),
                Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
                Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0")));
    assertThat(id.getDescription()).isEqualTo("description");
    assertThat(id.getBaseUnit()).isEqualTo("unit");
    assertThat(counter.count()).isEqualTo(10.0);

    // test that counter can be increased
    underTest.add(10, attributes);
    assertThat(counter.count()).isEqualTo(20.0);

    // test that counter cannot be decreased
    underTest.add(-5, attributes);
    assertThat(counter.count()).isEqualTo(20.0);

    double expectedCount = 20.0;
    for (long value : RandomUtils.randomLongs(10, 0L, 500L)) {
      expectedCount += value;

      underTest.add(value, attributes);
      assertThat(counter.count()).isEqualTo(expectedCount);
    }
  }

  @Test
  void observable() {
    AtomicLong atomicLong = new AtomicLong();
    ObservableLongCounter underTest =
        MicrometerLongCounter.builder(meterSharedState, "counter")
            .setDescription("description")
            .setUnit("unit")
            .buildWithCallback(measurement -> measurement.record(atomicLong.get()));

    assertThat(callbacks).hasSize(1);

    assertThat(meterRegistry.getMeters()).isEmpty();

    atomicLong.set(10L);
    callbackRegistrar.run();
    FunctionCounter counter = meterRegistry.find("counter").functionCounter();
    assertThat(counter).isNotNull();
    Meter.Id id = counter.getId();
    assertThat(id.getName()).isEqualTo("counter");
    assertThat(id.getTags())
        .containsExactlyInAnyOrder(
            Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
            Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
    assertThat(id.getDescription()).isEqualTo("description");
    assertThat(id.getBaseUnit()).isEqualTo("unit");
    assertThat(counter.count()).isEqualTo(10.0);

    // test that counter can be increased
    atomicLong.set(20L);
    callbackRegistrar.run();
    assertThat(counter.count()).isEqualTo(20.0);

    // test that counter cannot be decreased
    atomicLong.set(5L);
    callbackRegistrar.run();
    assertThat(counter.count()).isEqualTo(20.0);

    long value = 20L;
    for (long increment : RandomUtils.randomLongs(10, 0L, 500L)) {
      value += increment;
      atomicLong.set(value);
      callbackRegistrar.run();
      assertThat(counter.count()).isEqualTo((double) value);
    }

    underTest.close();

    assertThat(callbacks).isEmpty();
  }

  @Test
  void observableWithAttributes() {
    AtomicLong atomicLong = new AtomicLong();
    Attributes attributes = Attributes.builder().put("key", "value").build();
    ObservableLongCounter underTest =
        MicrometerLongCounter.builder(meterSharedState, "counter")
            .setDescription("description")
            .setUnit("unit")
            .buildWithCallback(measurement -> measurement.record(atomicLong.get(), attributes));

    assertThat(callbacks).hasSize(1);

    assertThat(meterRegistry.getMeters()).isEmpty();

    atomicLong.set(10L);
    callbackRegistrar.run();
    FunctionCounter counter = meterRegistry.find("counter").functionCounter();
    assertThat(counter).isNotNull();
    Meter.Id id = counter.getId();
    assertThat(id.getName()).isEqualTo("counter");
    assertThat(id.getTags())
        .containsExactlyInAnyOrder(
            Tag.of("key", "value"),
            Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
            Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
    assertThat(id.getDescription()).isEqualTo("description");
    assertThat(id.getBaseUnit()).isEqualTo("unit");
    assertThat(counter.count()).isEqualTo(10.0);

    // test that counter can be increased
    atomicLong.set(20L);
    callbackRegistrar.run();
    assertThat(counter.count()).isEqualTo(20.0);

    // test that counter cannot be decreased
    atomicLong.set(5L);
    callbackRegistrar.run();
    assertThat(counter.count()).isEqualTo(20.0);

    long value = 20L;
    for (long increment : RandomUtils.randomLongs(10, 0L, 500L)) {
      value += increment;
      atomicLong.set(value);
      callbackRegistrar.run();
      assertThat(counter.count()).isEqualTo((double) value);
    }

    underTest.close();

    assertThat(callbacks).isEmpty();
  }

  @Test
  void observableWithAttributesAndAttributesAdvice() {
    LongCounterBuilder builder =
        MicrometerLongCounter.builder(meterSharedState, "counter")
            .setDescription("description")
            .setUnit("unit");

    ((ExtendedLongCounterBuilder) builder)
        .setAttributesAdvice(
            Arrays.asList(
                AttributeKey.stringKey(Constants.OTEL_INSTRUMENTATION_NAME),
                AttributeKey.stringKey(Constants.OTEL_INSTRUMENTATION_VERSION),
                AttributeKey.stringKey("key")));

    AtomicLong atomicLong = new AtomicLong();
    Attributes attributes =
        Attributes.builder().put("key", "value").put("unwanted", "value").build();
    ObservableLongCounter underTest =
        builder.buildWithCallback(measurement -> measurement.record(atomicLong.get(), attributes));

    assertThat(callbacks).hasSize(1);

    assertThat(meterRegistry.getMeters()).isEmpty();

    atomicLong.set(10L);
    callbackRegistrar.run();
    FunctionCounter counter = meterRegistry.find("counter").functionCounter();
    assertThat(counter).isNotNull();
    Meter.Id id = counter.getId();
    assertThat(id.getName()).isEqualTo("counter");
    assertThat(id.getTags())
        .containsExactlyInAnyOrder(
            Tag.of("key", "value"),
            Tag.of(Constants.OTEL_INSTRUMENTATION_NAME, "meter"),
            Tag.of(Constants.OTEL_INSTRUMENTATION_VERSION, "1.0"));
    assertThat(id.getDescription()).isEqualTo("description");
    assertThat(id.getBaseUnit()).isEqualTo("unit");
    assertThat(counter.count()).isEqualTo(10.0);

    // test that counter can be increased
    atomicLong.set(20L);
    callbackRegistrar.run();
    assertThat(counter.count()).isEqualTo(20.0);

    // test that counter cannot be decreased
    atomicLong.set(5L);
    callbackRegistrar.run();
    assertThat(counter.count()).isEqualTo(20.0);

    long value = 20L;
    for (long increment : RandomUtils.randomLongs(10, 0L, 500L)) {
      value += increment;
      atomicLong.set(value);
      callbackRegistrar.run();
      assertThat(counter.count()).isEqualTo((double) value);
    }

    underTest.close();

    assertThat(callbacks).isEmpty();
  }
}
