using Akka.Actor;

namespace ZB.MOM.WW.ScadaBridge.Communication.Actors;

/// <summary>
/// The central→site command-send seam, injected into <see cref="CentralCommunicationActor"/>
/// below the <see cref="SiteEnvelope"/> handler. The sole implementation is
/// <see cref="ZB.MOM.WW.ScadaBridge.Communication.Grpc.GrpcSiteTransport"/>, over the site
/// <c>SiteCommandService</c> gRPC plane. The seam predates that: it was introduced so the Akka
/// <c>ClusterClient</c> path and the gRPC plane could run side by side during the migration, and
/// is kept because it is the natural substitution point for tests.
/// The producers above the seam (<c>CommunicationService</c>'s 27 commands, <c>SiteCallAuditActor</c>'s
/// 2 parked relays, and <c>DebugStreamBridgeActor</c>'s subscribe/unsubscribe) are unchanged — they
/// still <c>Ask</c>/<c>Tell</c> a <see cref="SiteEnvelope"/> to the actor, which delegates here.
/// </summary>
/// <remarks>
/// Both members run on the actor thread (from the <see cref="SiteEnvelope"/> and
/// <c>SiteAddressCacheLoaded</c> handlers), so implementations need no internal synchronisation for
/// their own per-site bookkeeping beyond what a background failback loop requires.
/// </remarks>
public interface ISiteCommandTransport
{
    /// <summary>
    /// Routes <paramref name="envelope"/>'s message to its site. Any reply the site produces is
    /// delivered to <paramref name="replyTo"/> — for an <c>Ask</c> that is the temporary ask actor
    /// (completing the caller's task); for a <c>Tell</c>-with-sender (the debug bridge) that is the
    /// originating actor. A message with no route (an unknown/unconfigured site) fails fast: a
    /// <see cref="Status.Failure"/> is delivered to <paramref name="replyTo"/> immediately instead of
    /// letting the caller's <c>Ask</c> burn its full timeout — central still never buffers for an
    /// unreachable site.
    /// </summary>
    /// <param name="envelope">The site-addressed command envelope.</param>
    /// <param name="replyTo">Where a reply (or a <see cref="Status.Failure"/>) is delivered.</param>
    void Send(SiteEnvelope envelope, IActorRef replyTo);

    /// <summary>
    /// Reconciles per-site transport resources (gRPC channel pairs)
    /// against the freshly loaded site set. Called once per DB refresh tick with the same cache
    /// message the actor already receives — the ONE DB-poll loop feeds both transports.
    /// </summary>
    /// <param name="cache">The loaded site address cache (Akka contacts + gRPC endpoints + known ids).</param>
    void ReconcileSites(SiteAddressCacheLoaded cache);
}
