// 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/parser.go and server/client.go in the NATS server Go source.

namespace ZB.MOM.NatsNet.Server.Protocol;

/// <summary>
/// Interface for the protocol handler callbacks invoked by <see cref="ProtocolParser.Parse"/>.
/// Decouples the state machine from the client implementation.
/// The client connection will implement this interface in later sessions.
/// </summary>
public interface IProtocolHandler
{
    // ---- Dynamic connection state ----

    bool IsMqtt { get; }
    bool Trace { get; }
    bool HasMappings { get; }
    bool IsAwaitingAuth { get; }

    /// <summary>
    /// Attempts to register the no-auth user for this connection.
    /// Returns true if a no-auth user was found and registered (allowing parse to continue).
    /// </summary>
    bool TryRegisterNoAuthUser();

    /// <summary>
    /// Returns true if this is a gateway inbound connection that has not yet received CONNECT.
    /// </summary>
    bool IsGatewayInboundNotConnected { get; }

    // ---- Protocol action handlers ----

    Exception? ProcessConnect(byte[] arg);
    Exception? ProcessInfo(byte[] arg);
    void ProcessPing();
    void ProcessPong();
    void ProcessErr(string arg);

    // ---- Sub/unsub handlers (kind-specific) ----

    Exception? ProcessClientSub(byte[] arg);
    Exception? ProcessClientUnsub(byte[] arg);
    Exception? ProcessRemoteSub(byte[] arg, bool isLeaf);
    Exception? ProcessRemoteUnsub(byte[] arg, bool isLeafUnsub);
    Exception? ProcessGatewayRSub(byte[] arg);
    Exception? ProcessGatewayRUnsub(byte[] arg);
    Exception? ProcessLeafSub(byte[] arg);
    Exception? ProcessLeafUnsub(byte[] arg);
    Exception? ProcessAccountSub(byte[] arg);
    void ProcessAccountUnsub(byte[] arg);

    // ---- Message processing ----

    void ProcessInboundMsg(byte[] msg);
    bool SelectMappedSubject();

    // ---- Tracing ----

    void TraceInOp(string name, byte[]? arg);
    void TraceMsg(byte[] msg);

    // ---- Error handling ----

    void SendErr(string msg);
    void AuthViolation();
    void CloseConnection(int reason);
    string KindString();
}
