/**
 * Copyright IBM Corp. 2016, 2025
 * SPDX-License-Identifier: BUSL-1.1
 */

import { module, test } from 'qunit';
import { setupApplicationTest } from 'ember-qunit';
import { setupMirage } from 'ember-cli-mirage/test-support';
import { login } from 'vault/tests/helpers/auth/auth-helpers';
import replicationHandlers from 'vault/mirage/handlers/replication';
import { click, visit } from '@ember/test-helpers';
import { GENERAL } from 'vault/tests/helpers/general-selectors';

const SELECTORS = {
  title: '[data-test-replication-title]',
  primaryCluster: `${GENERAL.infoRowValue('primary_cluster_addr')}`,
  replicationSet: `${GENERAL.infoRowValue('Replication set')}`,
  knownSecondariesTitle: '.known-secondaries-card h3',
};
module('Acceptance | Enterprise | replication navigation', function (hooks) {
  setupApplicationTest(hooks);
  setupMirage(hooks);

  hooks.beforeEach(function () {
    replicationHandlers(this.server);
    return login();
  });

  test('navigate between replication types updates page', async function (assert) {
    await visit('/vault/replication');
    await click(GENERAL.navLink('Overview'));
    assert.dom(GENERAL.hdsPageHeaderTitle).hasText('Disaster recovery and performance');
    assert.dom(GENERAL.badge('primary')).exists('shows primary badge for dr');
    await click(GENERAL.navLink('Performance replication'));

    // Ensure data is expected for performance
    assert.dom(GENERAL.hdsPageHeaderTitle).hasText('Performance');
    assert.dom(GENERAL.badge('primary')).exists('shows primary badge for performance');
    assert.dom(SELECTORS.primaryCluster).hasText('perf-foobar');
    assert.dom(SELECTORS.replicationSet).hasText('perf-cluster-id');
    assert.dom(SELECTORS.knownSecondariesTitle).hasText('0 Known secondaries');

    // Nav to DR and see updated data
    await click(GENERAL.navLink('Disaster recovery'));
    assert.dom(GENERAL.hdsPageHeaderTitle).hasText('Disaster recovery');
    assert.dom(GENERAL.badge('primary')).exists('shows primary badge for dr');
    assert.dom(SELECTORS.primaryCluster).hasText('dr-foobar');
    assert.dom(SELECTORS.replicationSet).hasText('dr-cluster-id');
    assert.dom(SELECTORS.knownSecondariesTitle).hasText('1 Known secondaries');
  });
});
