using System.Text.Json;

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

/// <summary>
/// Request from the UI to execute a script in the central sandbox.
/// Parameters arrive as JSON values and are converted to .NET primitives
/// before being placed in the Parameters dictionary supplied to the script.
/// <see cref="Kind"/> selects which globals surface the script is compiled
/// and run against — template/shared scripts see <see cref="SandboxScriptHost"/>,
/// inbound API method scripts see <see cref="SandboxInboundScriptHost"/>.
/// <see cref="BindInstanceUniqueName"/>, when set, binds the run to a deployed
/// instance so <c>Instance</c>/<c>Attributes</c> access routes to it cross-site
/// instead of throwing. Ignored for inbound API scripts.
/// </summary>
public record SandboxRunRequest(
    string Code,
    Dictionary<string, JsonElement>? Parameters,
    int? TimeoutSeconds,
    ScriptKind Kind = ScriptKind.Template,
    string? BindInstanceUniqueName = null);

public enum SandboxErrorKind
{
    None,
    CompileError,
    SandboxLimitation,
    RuntimeError,
    Timeout
}

/// <summary>
/// Result of a Test Run. <see cref="Markers"/> carries Roslyn diagnostics
/// when <see cref="ErrorKind"/> is CompileError so the UI can display them
/// the same way it does for the editor's live problems panel.
/// </summary>
public record SandboxRunResult(
    bool Success,
    string? ReturnValueJson,
    string? ReturnTypeName,
    string ConsoleOutput,
    string? Error,
    SandboxErrorKind ErrorKind,
    long DurationMs,
    IReadOnlyList<DiagnosticMarker>? Markers);
