namespace ZB.MOM.WW.OtOpcUa.Core.Abstractions;

/// <summary>
///     Required capability for every driver instance. Owns lifecycle, metadata, health.
///     Other capabilities (<see cref="ITagDiscovery"/>, <see cref="IReadable"/>,
///     <see cref="IWritable"/>, <see cref="ISubscribable"/>, <see cref="IAlarmSource"/>,
///     <see cref="IHistoryProvider"/>, <see cref="IRediscoverable"/>,
///     <see cref="IHostConnectivityProbe"/>) are composable — a driver implements only what its
///     backend actually supports.
/// </summary>
public interface IDriver
{
    /// <summary>Stable logical ID of this driver instance, sourced from the central config DB.</summary>
    string DriverInstanceId { get; }

    /// <summary>Driver type name (e.g. "Galaxy", "Modbus", "AbCip"). Matches <c>DriverInstance.DriverType</c>.</summary>
    string DriverType { get; }

    /// <summary>Initialize the driver from its <c>DriverConfig</c> JSON; open connections; prepare for first use.</summary>
    /// <param name="driverConfigJson">The driver configuration as JSON.</param>
    /// <param name="cancellationToken">Cancellation token for the operation.</param>
    /// <returns>A task that represents the asynchronous operation.</returns>
    Task InitializeAsync(string driverConfigJson, CancellationToken cancellationToken);

    /// <summary>
    ///     Apply a config change in place without tearing down the driver process.
    ///     Invoked by the v2 <c>DriverInstanceActor</c> when ApplyDelta reports that only this
    ///     driver's config changed in the new deployment.
    /// </summary>
    /// <remarks>
    ///     Per <c>docs/v2/driver-stability.md</c> §"In-process only (Tier A/B)" — Reinitialize is the
    ///     only Core-initiated recovery path for in-process drivers; if it fails, the driver instance
    ///     is marked Faulted and its nodes go Bad quality, but the server process keeps running.
    /// </remarks>
    /// <param name="driverConfigJson">The driver configuration as JSON.</param>
    /// <param name="cancellationToken">Cancellation token for the operation.</param>
    /// <returns>A task that represents the asynchronous operation.</returns>
    Task ReinitializeAsync(string driverConfigJson, CancellationToken cancellationToken);

    /// <summary>Stop the driver, close connections, release resources. Called on shutdown or driver removal.</summary>
    /// <param name="cancellationToken">Cancellation token for the operation.</param>
    /// <returns>A task that represents the asynchronous operation.</returns>
    Task ShutdownAsync(CancellationToken cancellationToken);

    /// <summary>Current health snapshot, polled by Core for the status dashboard and ServiceLevel.</summary>
    /// <returns>The driver's current health snapshot.</returns>
    DriverHealth GetHealth();

    /// <summary>
    ///     Approximate driver-attributable footprint in bytes (caches, queues, symbol tables).
    ///     Polled every 30s by Core; on cache-budget breach, Core asks the driver to flush via
    ///     <see cref="FlushOptionalCachesAsync"/>.
    /// </summary>
    /// <remarks>
    ///     Per <c>docs/v2/driver-stability.md</c> §"In-process only (Tier A/B) — driver-instance
    ///     allocation tracking". Tier C drivers (process-isolated) report through the same
    ///     interface but the cache-flush is internal to their host.
    /// </remarks>
    /// <returns>The approximate memory footprint in bytes.</returns>
    long GetMemoryFootprint();

    /// <summary>
    ///     Drop optional caches (symbol cache, browse cache, etc.) to bring footprint back below budget.
    ///     Required-for-correctness state must NOT be flushed.
    /// </summary>
    /// <param name="cancellationToken">Cancellation token for the operation.</param>
    /// <returns>A task that represents the asynchronous operation.</returns>
    Task FlushOptionalCachesAsync(CancellationToken cancellationToken);
}
