Editor/Core/ImportCompletion.cs
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Nodes;

namespace ImportUnityPackage;

/// <summary>Short completion text, with detailed warnings and errors retained in the report.</summary>
public sealed record ImportCompletion( int AssetCount, string[] Warnings, string[] Errors, bool Cancelled )
{
	public string Title => Cancelled ? "Import preparation cancelled" : Errors.Length > 0 ? "Import completed with errors" : "Import complete";
	public string Counts => $"Warnings: {Warnings.Length} · Errors: {Errors.Length}";
	public string Message => Cancelled ? $"Imported files kept. Resource preparation stopped.\n{Counts}" : $"{AssetCount} assets imported.\n{Counts}";
	public string Status => $"{(Errors.Length == 0 && !Cancelled ? "✓ " : "")}{Title}\n{Message}";

	public static ImportCompletion Create( ImportResult result, IEnumerable<string> preparationWarnings, IEnumerable<string> preparationErrors, bool cancelled )
	{
		var errors = result.Unresolved.Where( i => i.Severity == "Error" ).Select( i => $"{i.Asset}: {i.Message}" )
			.Concat( preparationErrors ?? Array.Empty<string>() ).Distinct().ToArray();
		var warnings = result.Warnings.Concat( result.Unresolved.Where( i => i.Severity == "Warning" ).Select( i => $"{i.Asset}: {i.Message}" ) )
			.Concat( preparationWarnings ?? Array.Empty<string>() ).Except( errors ).Distinct().ToArray();
		return new( result.AssetCount, warnings, errors, cancelled );
	}

	public void WriteReport( string path )
	{
		if ( string.IsNullOrWhiteSpace( path ) ) return;
		var report = JsonNode.Parse( File.ReadAllText( path ) );
		report["Completion"] = JsonSerializer.SerializeToNode( new { Title, AssetCount, Cancelled, Warnings, Errors } );
		File.WriteAllText( path, report.ToJsonString( new JsonSerializerOptions { WriteIndented = true } ) );
	}
}