using Akka.Actor;
using Akka.Event;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Audit;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Deployment;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Health;
using ZB.MOM.WW.ScadaBridge.Commons.Messages.Notification;

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

/// <summary>
/// The <see cref="ICentralTransport"/> used only when the Host injects none — a fail-loud placeholder,
/// not a working transport. Production always injects <see cref="Grpc.GrpcCentralTransport"/>; a null
/// here is a wiring bug, so every send warns and each Ask-returning method answers a
/// <see cref="Status.Failure"/> so the caller sees the same transient failure the old ClusterClient
/// path produced when no client was registered (rows stay buffered, the pass re-runs). The heartbeat
/// stays fire-and-forget: swallowed and logged, never faulting the heartbeat timer.
/// </summary>
/// <remarks>
/// This replaced the previous default (<c>AkkaCentralTransport</c> with no registered ClusterClient),
/// deleted when the site→central path became gRPC-only in the ClusterClient→gRPC migration's Phase 4.
/// It exists mostly so TestKit suites that only exercise central→site command dispatch — and never
/// inject a transport — construct the actor without wiring a real channel.
/// </remarks>
public sealed class NoOpCentralTransport : ICentralTransport
{
    private readonly ILoggingAdapter _log;

    /// <summary>Creates the placeholder transport with the owning actor's logging adapter.</summary>
    /// <param name="log">The actor's logging adapter, used for the "no transport configured" warnings.</param>
    public NoOpCentralTransport(ILoggingAdapter log) => _log = log;

    private void Fail(string what, IActorRef replyTo)
    {
        _log.Warning(
            "No site→central transport is configured; dropping {0} as a transient failure. This is a "
            + "wiring bug — the Host must inject a GrpcCentralTransport.", what);
        replyTo.Tell(new Status.Failure(new InvalidOperationException(
            $"No site→central transport configured (dropping {what}).")));
    }

    /// <inheritdoc />
    public void SubmitNotification(NotificationSubmit message, IActorRef replyTo) => Fail(nameof(SubmitNotification), replyTo);

    /// <inheritdoc />
    public void QueryNotificationStatus(NotificationStatusQuery message, IActorRef replyTo) => Fail(nameof(QueryNotificationStatus), replyTo);

    /// <inheritdoc />
    public void IngestAuditEvents(IngestAuditEventsCommand message, IActorRef replyTo) => Fail(nameof(IngestAuditEvents), replyTo);

    /// <inheritdoc />
    public void IngestCachedTelemetry(IngestCachedTelemetryCommand message, IActorRef replyTo) => Fail(nameof(IngestCachedTelemetry), replyTo);

    /// <inheritdoc />
    public void ReconcileSite(ReconcileSiteRequest message, IActorRef replyTo) => Fail(nameof(ReconcileSite), replyTo);

    /// <inheritdoc />
    public void ReportSiteHealth(SiteHealthReport message, IActorRef replyTo) => Fail(nameof(ReportSiteHealth), replyTo);

    /// <inheritdoc />
    public void SendHeartbeat(HeartbeatMessage message, IActorRef self) =>
        _log.Warning("No site→central transport is configured; heartbeat dropped (wiring bug).");
}
