namespace NexusShell.Services.Diagnostics;

public sealed class AppLaunchTracker
{
    private readonly DiagnosticsService _diagnostics;

    public AppLaunchTracker(DiagnosticsService diagnostics)
    {
        _diagnostics = diagnostics;
    }

    public void TrackInternalLaunch(string appId, string displayName, bool success, string? detail = null)
    {
        _diagnostics.LogLaunch(success ? "internal_launch" : "internal_launch_failed",
            success ? $"{displayName} åbnet i workspace" : $"{displayName} kunne ikke åbnes",
            new Dictionary<string, string>
            {
                ["appId"] = appId,
                ["hosting"] = "nexus-workspace",
                ["success"] = success.ToString(),
                ["detail"] = detail ?? string.Empty,
            });
    }

    public void TrackExternalLaunch(string appId, string displayName, int? processId, bool success, string? detail = null)
    {
        _diagnostics.LogLaunch(success ? "external_launch" : "external_launch_failed",
            success
                ? $"{displayName} startet som ekstern proces (POC-advarsel)"
                : $"{displayName} ekstern start mislykkedes",
            new Dictionary<string, string>
            {
                ["appId"] = appId,
                ["hosting"] = "external-hosted-app",
                ["processId"] = processId?.ToString() ?? string.Empty,
                ["success"] = success.ToString(),
                ["detail"] = detail ?? string.Empty,
                ["pocWarning"] = "Eksternt vindue er ikke fuldt indlejret i Nexus workspace.",
            });
    }

    public void TrackLauncherAction(string action, string? target = null)
    {
        _diagnostics.LogWorkspace("launcher_action", action,
            target is null ? null : new Dictionary<string, string> { ["target"] = target });
    }
}
