// Go parity: golang/nats-server/server/jetstream_cluster_1_test.go
// Covers: per-stream RAFT groups, stream assignment proposal, replica count
// enforcement, leader election for stream group, data replication across
// stream replicas, placement scaling, stepdown behavior.
using System.Collections.Concurrent;
using System.Reflection;
using System.Text;
using NATS.Server.JetStream;
using NATS.Server.JetStream.Api;
using NATS.Server.JetStream.Cluster;
using NATS.Server.JetStream.Models;
using NATS.Server.JetStream.Publish;
using NATS.Server.Raft;

namespace NATS.Server.JetStream.Tests.JetStream.Cluster;

/// <summary>
/// Tests covering per-stream RAFT groups: stream assignment proposal,
/// replica count enforcement, leader election, data replication across
/// replicas, placement scaling, and stepdown behavior.
/// Ported from Go jetstream_cluster_1_test.go.
/// </summary>
public class StreamReplicaGroupTests
{
    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterMultiReplicaStreams server/jetstream_cluster_1_test.go:299
    // ---------------------------------------------------------------

    [Fact]
    public void Replica_group_r3_creates_three_raft_nodes()
    {
        var group = new StreamReplicaGroup("TEST", replicas: 3);

        group.Nodes.Count.ShouldBe(3);
        group.StreamName.ShouldBe("TEST");
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterSingleReplicaStreams server/jetstream_cluster_1_test.go:223
    // ---------------------------------------------------------------

    [Fact]
    public void Replica_group_r1_creates_single_raft_node()
    {
        var group = new StreamReplicaGroup("R1S", replicas: 1);

        group.Nodes.Count.ShouldBe(1);
        group.Leader.IsLeader.ShouldBeTrue();
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterMultiReplicaStreams server/jetstream_cluster_1_test.go:299
    // ---------------------------------------------------------------

    [Fact]
    public void Replica_group_zero_replicas_creates_one_node()
    {
        var group = new StreamReplicaGroup("ZERO", replicas: 0);

        group.Nodes.Count.ShouldBe(1);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterMultiReplicaStreams server/jetstream_cluster_1_test.go:299
    // ---------------------------------------------------------------

    [Fact]
    public void Replica_group_negative_replicas_creates_one_node()
    {
        var group = new StreamReplicaGroup("NEG", replicas: -1);

        group.Nodes.Count.ShouldBe(1);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterStreamLeaderStepDown server/jetstream_cluster_1_test.go:4925
    // ---------------------------------------------------------------

    [Fact]
    public void Replica_group_elects_initial_leader_on_creation()
    {
        var group = new StreamReplicaGroup("ELECT", replicas: 3);

        group.Leader.ShouldNotBeNull();
        group.Leader.IsLeader.ShouldBeTrue();
        group.Leader.Role.ShouldBe(RaftRole.Leader);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterStreamLeaderStepDown server/jetstream_cluster_1_test.go:4925
    // ---------------------------------------------------------------

    [Fact]
    public void Replica_group_leader_id_follows_naming_convention()
    {
        var group = new StreamReplicaGroup("MY_STREAM", replicas: 3);

        group.Leader.Id.ShouldStartWith("my_stream-r");
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterStreamLeaderStepDown server/jetstream_cluster_1_test.go:4925
    // ---------------------------------------------------------------

    [Fact]
    public async Task Replica_group_stepdown_changes_leader()
    {
        var group = new StreamReplicaGroup("STEP", replicas: 3);
        var before = group.Leader.Id;

        await group.StepDownAsync(default);

        group.Leader.Id.ShouldNotBe(before);
        group.Leader.IsLeader.ShouldBeTrue();
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterLeader server/jetstream_cluster_1_test.go:73
    // ---------------------------------------------------------------

    [Fact]
    public async Task Replica_group_consecutive_stepdowns_cycle_leaders()
    {
        var group = new StreamReplicaGroup("CYCLE", replicas: 3);
        var leaders = new List<string> { group.Leader.Id };

        await group.StepDownAsync(default);
        leaders.Add(group.Leader.Id);

        await group.StepDownAsync(default);
        leaders.Add(group.Leader.Id);

        leaders[1].ShouldNotBe(leaders[0]);
        leaders[2].ShouldNotBe(leaders[1]);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterLeader server/jetstream_cluster_1_test.go:73
    // ---------------------------------------------------------------

    [Fact]
    public async Task Replica_group_stepdown_wraps_around()
    {
        var group = new StreamReplicaGroup("WRAP", replicas: 3);
        var ids = new HashSet<string>();

        for (var i = 0; i < 6; i++)
        {
            ids.Add(group.Leader.Id);
            await group.StepDownAsync(default);
        }

        // Should have cycled through all 3 nodes
        ids.Count.ShouldBe(3);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterStreamLeaderStepDown server/jetstream_cluster_1_test.go:4925
    // ---------------------------------------------------------------

    [Fact]
    public async Task Replica_group_leader_accepts_proposals()
    {
        var group = new StreamReplicaGroup("PROPOSE", replicas: 3);

        var index = await group.ProposeAsync("PUB test.1", default);

        index.ShouldBeGreaterThan(0);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterStreamLeaderStepDown server/jetstream_cluster_1_test.go:4925
    // ---------------------------------------------------------------

    [Fact]
    public async Task Replica_group_sequential_proposals_have_increasing_indices()
    {
        var group = new StreamReplicaGroup("SEQPROP", replicas: 3);

        var idx1 = await group.ProposeAsync("PUB test.1", default);
        var idx2 = await group.ProposeAsync("PUB test.2", default);
        var idx3 = await group.ProposeAsync("PUB test.3", default);

        idx2.ShouldBeGreaterThan(idx1);
        idx3.ShouldBeGreaterThan(idx2);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterStreamNormalCatchup server/jetstream_cluster_1_test.go:1607
    // ---------------------------------------------------------------

    [Fact]
    public async Task Replica_group_proposals_survive_stepdown()
    {
        var group = new StreamReplicaGroup("SURVIVE", replicas: 3);

        await group.ProposeAsync("PUB a.1", default);
        await group.ProposeAsync("PUB a.2", default);

        await group.StepDownAsync(default);

        // New leader should accept proposals
        var idx = await group.ProposeAsync("PUB a.3", default);
        idx.ShouldBeGreaterThan(0);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterExpandCluster server/jetstream_cluster_1_test.go:86
    // ---------------------------------------------------------------

    [Fact]
    public async Task Replica_group_apply_placement_scales_up()
    {
        var group = new StreamReplicaGroup("SCALEUP", replicas: 1);
        group.Nodes.Count.ShouldBe(1);

        await group.ApplyPlacementAsync([1, 2, 3], default);

        group.Nodes.Count.ShouldBe(3);
        group.Leader.IsLeader.ShouldBeTrue();
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterExpandCluster server/jetstream_cluster_1_test.go:86
    // ---------------------------------------------------------------

    [Fact]
    public async Task Replica_group_apply_placement_scales_down()
    {
        var group = new StreamReplicaGroup("SCALEDN", replicas: 5);
        group.Nodes.Count.ShouldBe(5);

        await group.ApplyPlacementAsync([1, 2], default);

        group.Nodes.Count.ShouldBe(2);
        group.Leader.IsLeader.ShouldBeTrue();
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterExpandCluster server/jetstream_cluster_1_test.go:86
    // ---------------------------------------------------------------

    [Fact]
    public async Task Replica_group_apply_same_size_is_noop()
    {
        var group = new StreamReplicaGroup("NOOP", replicas: 3);
        var leaderBefore = group.Leader.Id;

        await group.ApplyPlacementAsync([1, 2, 3], default);

        group.Nodes.Count.ShouldBe(3);
        // Leader should remain the same since placement is a no-op
        group.Leader.Id.ShouldBe(leaderBefore);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterMultiReplicaStreams server/jetstream_cluster_1_test.go:299
    // ---------------------------------------------------------------

    [Fact]
    public void Replica_group_all_nodes_share_cluster()
    {
        var group = new StreamReplicaGroup("SHARED", replicas: 3);

        foreach (var node in group.Nodes)
            node.Members.Count.ShouldBe(3);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterStreamSynchedTimeStamps server/jetstream_cluster_1_test.go:977
    // ---------------------------------------------------------------

    [Fact]
    public async Task Stream_manager_creates_replica_group_on_stream_create()
    {
        var meta = new JetStreamMetaGroup(3);
        var streamManager = new StreamManager(meta);

        streamManager.CreateOrUpdate(new StreamConfig
        {
            Name = "REPL",
            Subjects = ["repl.>"],
            Replicas = 3,
        });

        // Use reflection to verify internal replica group was created
        var field = typeof(StreamManager)
            .GetField("_replicaGroups", BindingFlags.NonPublic | BindingFlags.Instance)!;
        var groups = (ConcurrentDictionary<string, StreamReplicaGroup>)field.GetValue(streamManager)!;

        groups.ContainsKey("REPL").ShouldBeTrue();
        groups["REPL"].Nodes.Count.ShouldBe(3);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterStreamLeaderStepDown server/jetstream_cluster_1_test.go:4925
    // ---------------------------------------------------------------

    [Fact]
    public async Task Stream_leader_stepdown_via_stream_manager_changes_leader()
    {
        var meta = new JetStreamMetaGroup(3);
        var streamManager = new StreamManager(meta);

        streamManager.CreateOrUpdate(new StreamConfig
        {
            Name = "SD",
            Subjects = ["sd.>"],
            Replicas = 3,
        });

        var field = typeof(StreamManager)
            .GetField("_replicaGroups", BindingFlags.NonPublic | BindingFlags.Instance)!;
        var groups = (ConcurrentDictionary<string, StreamReplicaGroup>)field.GetValue(streamManager)!;
        var leaderBefore = groups["SD"].Leader.Id;

        await streamManager.StepDownStreamLeaderAsync("SD", default);

        groups["SD"].Leader.Id.ShouldNotBe(leaderBefore);
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterStreamDelete server/jetstream_cluster_1_test.go:472
    // ---------------------------------------------------------------

    [Fact]
    public void Stream_delete_removes_replica_group()
    {
        var meta = new JetStreamMetaGroup(3);
        var streamManager = new StreamManager(meta);

        streamManager.CreateOrUpdate(new StreamConfig
        {
            Name = "DELRG",
            Subjects = ["delrg.>"],
            Replicas = 3,
        });

        streamManager.Delete("DELRG").ShouldBeTrue();

        var field = typeof(StreamManager)
            .GetField("_replicaGroups", BindingFlags.NonPublic | BindingFlags.Instance)!;
        var groups = (ConcurrentDictionary<string, StreamReplicaGroup>)field.GetValue(streamManager)!;

        groups.ContainsKey("DELRG").ShouldBeFalse();
    }

    // ---------------------------------------------------------------
    // Go: TestJetStreamClusterStreamUpdate server/jetstream_cluster_1_test.go:1433
    // ---------------------------------------------------------------

    [Fact]
    public void Stream_update_preserves_replica_group_when_replicas_unchanged()
    {
        var meta = new JetStreamMetaGroup(3);
        var streamManager = new StreamManager(meta);

        streamManager.CreateOrUpdate(new StreamConfig
        {
            Name = "UPD",
            Subjects = ["upd.>"],
            Replicas = 3,
        });

        var field = typeof(StreamManager)
            .GetField("_replicaGroups", BindingFlags.NonPublic | BindingFlags.Instance)!;
        var groups = (ConcurrentDictionary<string, StreamReplicaGroup>)field.GetValue(streamManager)!;
        var groupBefore = groups["UPD"];

        streamManager.CreateOrUpdate(new StreamConfig
        {
            Name = "UPD",
            Subjects = ["upd.>", "upd2.>"],
            Replicas = 3,
            MaxMsgs = 100,
        });

        // Same replica count means the group reference should be the same
        groups["UPD"].ShouldBeSameAs(groupBefore);
    }
}
