using ZB.MOM.WW.ScadaBridge.Commons.Types;

namespace ZB.MOM.WW.ScadaBridge.CentralUI.ScriptAnalysis;

/// <summary>
/// Runtime globals for an inbound API method Test Run. Mirrors
/// <see cref="InboundScriptHost"/>'s public surface so the same user code that
/// compiles for diagnostics also compiles against this type — but every
/// <c>Route</c> accessor throws <see cref="ScriptSandboxException"/> instead of
/// reaching a deployed site. Cross-site routing needs the cluster transport and
/// a live instance, neither of which exists in a central Test Run; pure logic
/// and <c>Parameters</c> still work, matching how <see cref="SandboxScriptHost"/>
/// throws on <c>Attributes</c> for shared scripts.
/// </summary>
public class SandboxInboundScriptHost
{
    /// <summary>Gets or initializes the script input parameters.</summary>
    public ScriptParameters Parameters { get; init; } = new();

    /// <summary>Gets or initializes the cancellation token for the test run.</summary>
    public CancellationToken CancellationToken { get; init; }

    /// <summary>Gets the route accessor; every call throws <see cref="ScriptSandboxException"/> in a test run.</summary>
    public RouteAccessor Route { get; } = new();

    /// <summary>Gets the database accessor; every call throws <see cref="ScriptSandboxException"/> in a test run.</summary>
    public DatabaseAccessor Database { get; } = new();

    /// <summary>
    /// Mirror of ZB.MOM.WW.ScadaBridge.InboundAPI.InboundDatabaseHelper — signatures match
    /// the runtime helper so the same user code compiles, but every call throws because a
    /// central Test Run has no configured database connection to reach (mirrors how
    /// <see cref="RouteAccessor"/> throws on cross-site routing).
    /// </summary>
    public class DatabaseAccessor
    {
        /// <summary>Always throws <see cref="ScriptSandboxException"/>; database access is unavailable in a Test Run.</summary>
        /// <typeparam name="T">The scalar result type.</typeparam>
        /// <param name="connectionName">Connection name (included in the exception message).</param>
        /// <param name="sql">Unused SQL.</param>
        /// <param name="parameters">Unused parameters.</param>
        /// <returns>Never returns; always throws <see cref="ScriptSandboxException"/>.</returns>
        public Task<T?> QuerySingleAsync<T>(string connectionName, string sql, object? parameters = null) =>
            throw Unavailable($"Database.QuerySingleAsync(\"{connectionName}\")");

        /// <summary>Always throws <see cref="ScriptSandboxException"/>; database access is unavailable in a Test Run.</summary>
        /// <param name="connectionName">Connection name (included in the exception message).</param>
        /// <param name="sql">Unused SQL.</param>
        /// <param name="parameters">Unused parameters.</param>
        /// <returns>Never returns; always throws <see cref="ScriptSandboxException"/>.</returns>
        public Task<IReadOnlyList<IReadOnlyDictionary<string, object?>>> QueryAsync(
            string connectionName, string sql, object? parameters = null) =>
            throw Unavailable($"Database.QueryAsync(\"{connectionName}\")");

        /// <summary>Always throws <see cref="ScriptSandboxException"/>; database access is unavailable in a Test Run.</summary>
        /// <param name="connectionName">Connection name (included in the exception message).</param>
        /// <param name="sql">Unused SQL.</param>
        /// <param name="parameters">Unused parameters.</param>
        /// <returns>Never returns; always throws <see cref="ScriptSandboxException"/>.</returns>
        public Task<int> ExecuteAsync(string connectionName, string sql, object? parameters = null) =>
            throw Unavailable($"Database.ExecuteAsync(\"{connectionName}\")");

        private static ScriptSandboxException Unavailable(string operation) =>
            new($"{operation} is not available in Test Run — database access needs the " +
                "central configuration / machine-data databases.");
    }

    /// <summary>Mirror of ZB.MOM.WW.ScadaBridge.InboundAPI.RouteHelper.</summary>
    public class RouteAccessor
    {
        /// <summary>
        /// Creates a sandbox route target that throws on every operation.
        /// </summary>
        /// <param name="instanceCode">The instance code (used only in the exception message).</param>
        /// <returns>A sandbox route target bound to <paramref name="instanceCode"/>.</returns>
        public RouteTarget To(string instanceCode) => new(instanceCode);
    }

    /// <summary>Mirror of ZB.MOM.WW.ScadaBridge.InboundAPI.RouteTarget — every call throws.</summary>
    public class RouteTarget
    {
        private readonly string _instanceCode;

        /// <summary>
        /// Initializes the sandbox route target for the given instance code.
        /// </summary>
        /// <param name="instanceCode">The instance code referenced by the routing expression (used in the exception message).</param>
        internal RouteTarget(string instanceCode) => _instanceCode = instanceCode;

        /// <summary>
        /// Always throws <see cref="ScriptSandboxException"/>; cross-site routing is unavailable in a Test Run.
        /// </summary>
        /// <param name="scriptName">Script name (included in the exception message).</param>
        /// <param name="parameters">Unused parameters.</param>
        /// <param name="cancellationToken">Unused token.</param>
        /// <returns>Never returns; always throws <see cref="ScriptSandboxException"/>.</returns>
        public Task<object?> Call(
            string scriptName,
            object? parameters = null,
            CancellationToken cancellationToken = default) =>
            throw Unavailable($"Call(\"{scriptName}\")");

        /// <summary>
        /// Always throws <see cref="ScriptSandboxException"/>; cross-site routing is unavailable in a Test Run.
        /// </summary>
        /// <param name="attributeName">Attribute name (included in the exception message).</param>
        /// <param name="cancellationToken">Unused token.</param>
        /// <returns>Never returns; always throws <see cref="ScriptSandboxException"/>.</returns>
        public Task<object?> GetAttribute(
            string attributeName,
            CancellationToken cancellationToken = default) =>
            throw Unavailable($"GetAttribute(\"{attributeName}\")");

        /// <summary>
        /// Always throws <see cref="ScriptSandboxException"/>; cross-site routing is unavailable in a Test Run.
        /// </summary>
        /// <param name="attributeNames">Attribute names (unused).</param>
        /// <param name="cancellationToken">Unused token.</param>
        /// <returns>Never returns; always throws <see cref="ScriptSandboxException"/>.</returns>
        public Task<IReadOnlyDictionary<string, object?>> GetAttributes(
            IEnumerable<string> attributeNames,
            CancellationToken cancellationToken = default) =>
            throw Unavailable("GetAttributes(...)");

        /// <summary>
        /// Always throws <see cref="ScriptSandboxException"/>; cross-site routing is unavailable in a Test Run.
        /// </summary>
        /// <param name="attributeName">Attribute name (included in the exception message).</param>
        /// <param name="value">Unused value.</param>
        /// <param name="cancellationToken">Unused token.</param>
        /// <returns>Never returns; always throws <see cref="ScriptSandboxException"/>.</returns>
        public Task SetAttribute(
            string attributeName,
            string value,
            CancellationToken cancellationToken = default) =>
            throw Unavailable($"SetAttribute(\"{attributeName}\")");

        /// <summary>
        /// Always throws <see cref="ScriptSandboxException"/>; cross-site routing is unavailable in a Test Run.
        /// </summary>
        /// <param name="attributeValues">Unused attribute values.</param>
        /// <param name="cancellationToken">Unused token.</param>
        /// <returns>Never returns; always throws <see cref="ScriptSandboxException"/>.</returns>
        public Task SetAttributes(
            IReadOnlyDictionary<string, string> attributeValues,
            CancellationToken cancellationToken = default) =>
            throw Unavailable("SetAttributes(...)");

        /// <summary>
        /// Always throws <see cref="ScriptSandboxException"/>; cross-site routing is unavailable in a Test Run.
        /// </summary>
        /// <param name="attributeName">Attribute name (included in the exception message).</param>
        /// <param name="targetValue">Unused target value.</param>
        /// <param name="timeout">Unused timeout.</param>
        /// <param name="cancellationToken">Unused token.</param>
        /// <returns>Never returns; always throws <see cref="ScriptSandboxException"/>.</returns>
        public Task<bool> WaitForAttribute(
            string attributeName,
            object? targetValue,
            TimeSpan timeout,
            CancellationToken cancellationToken = default) =>
            throw Unavailable($"WaitForAttribute(\"{attributeName}\")");

        private ScriptSandboxException Unavailable(string operation) =>
            new($"Route.To(\"{_instanceCode}\").{operation} is not available in Test Run — " +
                "cross-site routing needs a deployed site reachable over the cluster transport.");
    }
}
