// Copyright 2012-2025 The NATS Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// Adapted from server/client.go (subscription struct) in the NATS server Go source.

using ZB.MOM.NatsNet.Server;
using ZB.MOM.NatsNet.Server.Mqtt;

namespace ZB.MOM.NatsNet.Server.Internal;

/// <summary>
/// Represents a client subscription in the NATS server.
/// Mirrors the Go <c>subscription</c> struct from client.go.
/// This is a minimal stub; full client integration will be added in later sessions.
/// </summary>
public sealed class Subscription
{
    /// <summary>The subject this subscription is listening on.</summary>
    public byte[] Subject { get; set; } = [];

    /// <summary>The queue group name, or null/empty for non-queue subscriptions.</summary>
    public byte[]? Queue { get; set; }

    /// <summary>The subscription identifier.</summary>
    public byte[]? Sid { get; set; }

    /// <summary>Queue weight for remote queue subscriptions.</summary>
    public int Qw;

    /// <summary>Closed flag (0 = open, 1 = closed).</summary>
    private int _closed;

    /// <summary>The client that owns this subscription. Null in test/stub scenarios.</summary>
    public NatsClient? Client { get; set; }

    /// <summary>
    /// System message callback. Set on subscriptions created via <c>systemSubscribe</c>
    /// so that the internal receive queue can dispatch to the right handler.
    /// Mirrors Go <c>subscription.icb sysMsgHandler</c> in server/events.go.
    /// </summary>
    public SysMsgHandler? SysMsgCb { get; set; }

    /// <summary>
    /// MQTT-specific metadata attached to this subscription.
    /// Non-null only for subscriptions created by MQTT clients.
    /// Mirrors Go <c>subscription.mqtt *mqttSub</c>.
    /// </summary>
    internal MqttSub? Mqtt { get; set; }

    /// <summary>
    /// Internal delivery callback for server-side subscriptions (e.g. JSA subscriptions).
    /// When set, this delegate is called instead of routing via the normal client connection.
    /// Mirrors Go <c>subscription.icb msgHandler</c>.
    /// </summary>
    internal Action<Subscription?, ClientConnection?, INatsAccount?, string, string, byte[]>?
        InternalCallback { get; set; }

    /// <summary>Marks this subscription as closed.</summary>
    public void Close() => Interlocked.Exchange(ref _closed, 1);

    /// <summary>Returns true if this subscription has been closed.</summary>
    public bool IsClosed() => Interlocked.CompareExchange(ref _closed, 0, 0) == 1;
}

/// <summary>
/// Represents the kind of client connection.
/// Mirrors Go's <c>clientKind</c> enum.
/// This is a minimal stub; full implementation in later sessions.
/// </summary>
public enum ClientKind
{
    Client = 0,
    Router = 1,
    Gateway = 2,
    System = 3,
    Leaf = 4,
    JetStream = 5,
    Account = 6,
}

/// <summary>
/// Minimal client stub for subscription routing.
/// Full implementation will be added in later sessions.
/// </summary>
public class NatsClient
{
    /// <summary>The kind of client connection.</summary>
    public ClientKind Kind { get; set; }

    /// <summary>Whether this is a hub leaf node. Stub for now.</summary>
    public virtual bool IsHubLeafNode() => false;
}
