// Copyright 2012-2026 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.

using Xunit.Abstractions;

namespace ZB.MOM.NatsNet.Server.IntegrationTests;

/// <summary>
/// Abstract base class for all integration tests.
/// Skips the entire test class if the server cannot boot (i.e., the .NET server
/// runtime is not yet complete). Individual test classes inherit from this class.
/// </summary>
[Trait("Category", "Integration")]
public abstract class IntegrationTestBase : IDisposable
{
    // =========================================================================
    // Constructor — Skip guard
    // =========================================================================

    /// <summary>
    /// Initializes the test base and verifies that the server can boot.
    /// If <see cref="Helpers.TestServerHelper.CanBoot()"/> returns false the test
    /// is skipped via <c>Xunit.SkippableFact</c>'s <c>Skip.If</c> mechanism.
    /// </summary>
    protected IntegrationTestBase(ITestOutputHelper output)
    {
        Output = output;
        Skip.If(!Helpers.TestServerHelper.CanBoot(), "Server cannot boot — skipping integration tests.");
    }

    // =========================================================================
    // Protected members
    // =========================================================================

    /// <summary>xUnit output helper, available to derived test classes.</summary>
    protected ITestOutputHelper Output { get; }

    /// <summary>
    /// Returns true if integration tests should be skipped.
    /// Convenience method for use in individual test methods that call
    /// <c>Skip.If(ShouldSkip(), "reason")</c>.
    /// </summary>
    protected static bool ShouldSkip() => !Helpers.TestServerHelper.CanBoot();

    /// <summary>
    /// Returns true if the server runtime is unavailable.
    /// Alias for <see cref="ShouldSkip"/> used by some test batches.
    /// </summary>
    protected static bool ServerRuntimeUnavailable => !Helpers.TestServerHelper.CanBoot();

    /// <summary>
    /// Returns true if integration tests are enabled (server can boot).
    /// Alias used by NoRace batch tests.
    /// </summary>
    protected static bool IntegrationEnabled => Helpers.TestServerHelper.CanBoot();

    /// <summary>Standard skip message for integration tests.</summary>
    protected const string SkipMessage = "Server cannot boot — skipping integration tests.";

    // =========================================================================
    // IDisposable
    // =========================================================================

    /// <summary>
    /// Override in subclasses to perform per-test cleanup (e.g., shut down servers,
    /// delete temp dirs). The base implementation does nothing.
    /// </summary>
    public virtual void Dispose() { }
}
