using System.Globalization;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
using CommandForge.Application;
using CommandForge.Application.Ports;
using CommandForge.Desktop.Resources;
using CommandForge.Desktop.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
namespace CommandForge.Desktop.ViewModels;
///
/// Drives the Report-a-bug dialog: shows auto-gathered environment, exports logs, and opens a
/// prefilled issue on the GitLab mirror. User-initiated only — nothing is sent automatically (Privacy).
///
public sealed partial class ReportBugViewModel : ObservableObject
{
// Browsers and servers cap URL length; the body beyond this is left for the user to paste.
private const int MaxBodyCharsInUrl = 4000;
/// The issue template the report follows: .gitlab/issue_templates/Bug.md on the GitLab mirror.
public const string BugReportTemplate = "Bug";
/// The checklist at the end of the Bug template, left for the reporter to tick.
public static readonly string[] Confirmations =
[
"- [ ] I'm using the latest release.",
"- [ ] This is not a security vulnerability (those go to " + ProductLinks.SecurityEmail + ").",
];
private readonly ILogMaintenance _maintenance;
private readonly IDialogService _dialogs;
private readonly IFileDialogService _files;
private readonly IShellService _shell;
private readonly string? _errorCode;
public ReportBugViewModel(
ILogMaintenance maintenance,
IDialogService dialogs,
IFileDialogService files,
IShellService shell,
string? errorCode = null,
string? errorDetails = null)
{
ArgumentNullException.ThrowIfNull(maintenance);
ArgumentNullException.ThrowIfNull(dialogs);
ArgumentNullException.ThrowIfNull(files);
ArgumentNullException.ThrowIfNull(shell);
_maintenance = maintenance;
_dialogs = dialogs;
_files = files;
_shell = shell;
_errorCode = errorCode;
EnvironmentText = BuildEnvironment(AppVersion(), RuntimeInformation.OSDescription, RuntimeInformation.OSArchitecture);
_description = errorDetails ?? string.Empty;
}
public string EnvironmentText { get; }
[ObservableProperty]
private string _description;
public event Action? CloseRequested;
[RelayCommand]
private async Task ExportLogAsync()
{
var title = Strings.Get("ReportBug_ExportLog");
var path = _files.PickSaveFile(title, Strings.Get("FileFilter_Zip"), $"commandforge-logs-{DateTime.Now:yyyyMMdd-HHmmss}.zip");
if (path is null)
{
return;
}
try
{
await _maintenance.ExportZipAsync(path);
}
catch (Exception ex) when (ex is IOException or UnauthorizedAccessException)
{
await _dialogs.ShowMessageAsync(title, string.Format(CultureInfo.CurrentCulture, Strings.Get("File_SaveFailed"), ex.Message), DialogKind.Error);
return;
}
await _dialogs.ShowMessageAsync(
Strings.Get("ReportBug_Title"),
string.Format(CultureInfo.CurrentCulture, Strings.Get("Log_ExportSuccess"), path)
+ Environment.NewLine + Strings.Get("ReportBug_ReviewReminder"));
}
[RelayCommand]
private void OpenIssue()
=> _shell.TryOpen(BuildIssueUrl(
BuildTitle(),
AppVersion(),
$"{RuntimeInformation.OSDescription} ({RuntimeInformation.OSArchitecture})",
Description,
_errorCode));
///
/// Pure: the address of a new issue on the GitLab mirror with its title and description filled
/// in. The description is the Bug template (.gitlab/issue_templates/Bug.md) with its sections
/// answered; GitLab shows it as is (a template picked in the URL would replace it).
///
public static string BuildIssueUrl(string title, string appVersion, string windowsVersion, string? userDescription, string? errorCode)
{
ArgumentNullException.ThrowIfNull(title);
ArgumentNullException.ThrowIfNull(appVersion);
ArgumentNullException.ThrowIfNull(windowsVersion);
(string Field, string Value)[] fields =
[
("issue[title]", title),
("issue[description]", BuildIssueBody(appVersion, windowsVersion, userDescription, errorCode)),
];
return ProductLinks.NewIssue + "?" + string.Join('&', fields.Select(field => Uri.EscapeDataString(field.Field) + "=" + Uri.EscapeDataString(field.Value)));
}
/// Pure: the Bug template's sections, in its order, with what the app knows filled in.
public static string BuildIssueBody(string appVersion, string windowsVersion, string? userDescription, string? errorCode)
{
ArgumentNullException.ThrowIfNull(appVersion);
ArgumentNullException.ThrowIfNull(windowsVersion);
string[] lines =
[
"## What happened?", string.Empty, TruncateForUrl(BuildWhatHappened(userDescription, errorCode)), string.Empty,
"## Steps to reproduce", string.Empty, "1. ", string.Empty,
"## CommandForge version", string.Empty, appVersion, string.Empty,
"## Windows version / build", string.Empty, windowsVersion, string.Empty,
"## Logs (optional)", string.Empty, "_Attach the exported .zip here. Review its contents first — it may contain command output._", string.Empty,
"## Confirmations", string.Empty, .. Confirmations,
];
return string.Join('\n', lines);
}
/// Pure: the form's "What happened?" text: the user's words (or a prompt) and a crash's error code.
public static string BuildWhatHappened(string? userDescription, string? errorCode)
{
var text = string.IsNullOrWhiteSpace(userDescription) ? "_Describe what happened._" : userDescription.Trim();
return string.IsNullOrWhiteSpace(errorCode)
? text
: text + Environment.NewLine + Environment.NewLine + "Error code: " + errorCode;
}
/// Pure: keeps a prefilled issue body within URL limits (a full stack trace can exceed them).
public static string TruncateForUrl(string body)
{
ArgumentNullException.ThrowIfNull(body);
return body.Length <= MaxBodyCharsInUrl
? body
: body[..MaxBodyCharsInUrl] + Environment.NewLine + Environment.NewLine + "_(truncated — please paste the rest of the details here)_";
}
[RelayCommand]
private void Close() => CloseRequested?.Invoke();
/// Pure: assembles the environment block (testable).
public static string BuildEnvironment(string appVersion, string osDescription, Architecture osArchitecture)
{
var builder = new StringBuilder();
builder.AppendLine($"App version: {appVersion}");
builder.AppendLine($"OS: {osDescription}");
builder.AppendLine($"Architecture: {osArchitecture}");
return builder.ToString();
}
private string BuildTitle() => _errorCode is null ? "[Bug] " : $"[Crash {_errorCode}] ";
private static string AppVersion()
{
var version = typeof(ReportBugViewModel).Assembly.GetName().Version;
return version is null ? "—" : $"{version.Major}.{version.Minor}.{version.Build}";
}
}