using System.Threading;
using System.Threading.Tasks;
using ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Shared.Contracts;

namespace ZB.MOM.WW.OtOpcUa.Driver.Galaxy.Host.Backend;

/// <summary>
///     Phase 2 placeholder backend — accepts session open/close + responds to recycle, returns
///     "not-implemented" results for every data-plane call. Replaced by the lifted
///     <c>MxAccessClient</c>-backed implementation during the deferred Galaxy code move
///     (Task B.1 + parity gate). Keeps the IPC end-to-end testable today.
/// </summary>
public sealed class StubGalaxyBackend : IGalaxyBackend
{
    private long _nextSessionId;
    private long _nextSubscriptionId;

    // Stub backend never raises events — implements the interface members for symmetry.
#pragma warning disable CS0067
    public event System.EventHandler<OnDataChangeNotification>? OnDataChange;
    public event System.EventHandler<GalaxyAlarmEvent>? OnAlarmEvent;
    public event System.EventHandler<HostConnectivityStatus>? OnHostStatusChanged;
#pragma warning restore CS0067

    public Task<OpenSessionResponse> OpenSessionAsync(OpenSessionRequest req, CancellationToken ct)
    {
        var id = Interlocked.Increment(ref _nextSessionId);
        return Task.FromResult(new OpenSessionResponse { Success = true, SessionId = id });
    }

    public Task CloseSessionAsync(CloseSessionRequest req, CancellationToken ct) => Task.CompletedTask;

    public Task<DiscoverHierarchyResponse> DiscoverAsync(DiscoverHierarchyRequest req, CancellationToken ct)
        => Task.FromResult(new DiscoverHierarchyResponse
        {
            Success = false,
            Error = "stub: MXAccess code lift pending (Phase 2 Task B.1)",
            Objects = System.Array.Empty<GalaxyObjectInfo>(),
        });

    public Task<ReadValuesResponse> ReadValuesAsync(ReadValuesRequest req, CancellationToken ct)
        => Task.FromResult(new ReadValuesResponse
        {
            Success = false,
            Error = "stub: MXAccess code lift pending (Phase 2 Task B.1)",
            Values = System.Array.Empty<GalaxyDataValue>(),
        });

    public Task<WriteValuesResponse> WriteValuesAsync(WriteValuesRequest req, CancellationToken ct)
    {
        var results = new WriteValueResult[req.Writes.Length];
        for (var i = 0; i < req.Writes.Length; i++)
        {
            results[i] = new WriteValueResult
            {
                TagReference = req.Writes[i].TagReference,
                StatusCode = 0x80020000u, // Bad_InternalError
                Error = "stub: MXAccess code lift pending (Phase 2 Task B.1)",
            };
        }
        return Task.FromResult(new WriteValuesResponse { Results = results });
    }

    public Task<SubscribeResponse> SubscribeAsync(SubscribeRequest req, CancellationToken ct)
    {
        var sid = Interlocked.Increment(ref _nextSubscriptionId);
        return Task.FromResult(new SubscribeResponse
        {
            Success = true,
            SubscriptionId = sid,
            ActualIntervalMs = req.RequestedIntervalMs,
        });
    }

    public Task UnsubscribeAsync(UnsubscribeRequest req, CancellationToken ct) => Task.CompletedTask;

    public Task SubscribeAlarmsAsync(AlarmSubscribeRequest req, CancellationToken ct) => Task.CompletedTask;
    public Task AcknowledgeAlarmAsync(AlarmAckRequest req, CancellationToken ct) => Task.CompletedTask;

    public Task<HistoryReadResponse> HistoryReadAsync(HistoryReadRequest req, CancellationToken ct)
        => Task.FromResult(new HistoryReadResponse
        {
            Success = false,
            Error = "stub: MXAccess code lift pending (Phase 2 Task B.1)",
            Tags = System.Array.Empty<HistoryTagValues>(),
        });

    public Task<HistoryReadProcessedResponse> HistoryReadProcessedAsync(
        HistoryReadProcessedRequest req, CancellationToken ct)
        => Task.FromResult(new HistoryReadProcessedResponse
        {
            Success = false,
            Error = "stub: MXAccess code lift pending (Phase 2 Task B.1)",
            Values = System.Array.Empty<GalaxyDataValue>(),
        });

    public Task<HistoryReadAtTimeResponse> HistoryReadAtTimeAsync(
        HistoryReadAtTimeRequest req, CancellationToken ct)
        => Task.FromResult(new HistoryReadAtTimeResponse
        {
            Success = false,
            Error = "stub: MXAccess code lift pending (Phase 2 Task B.1)",
            Values = System.Array.Empty<GalaxyDataValue>(),
        });

    public Task<HistoryReadEventsResponse> HistoryReadEventsAsync(
        HistoryReadEventsRequest req, CancellationToken ct)
        => Task.FromResult(new HistoryReadEventsResponse
        {
            Success = false,
            Error = "stub: MXAccess code lift pending (Phase 2 Task B.1)",
            Events = System.Array.Empty<GalaxyHistoricalEvent>(),
        });

    public Task<RecycleStatusResponse> RecycleAsync(RecycleHostRequest req, CancellationToken ct)
        => Task.FromResult(new RecycleStatusResponse
        {
            Accepted = true,
            GraceSeconds = 15, // matches Phase 2 plan §B.8 default
        });
}
