using System.Text.Json;

using RolemasterDb.ImportTool.Parsing;

namespace RolemasterDb.ImportTool;

public sealed class ImportArtifactWriter
{
    private static readonly JsonSerializerOptions JsonOptions = new()
    {
        WriteIndented = true
    };

    public async Task WriteAsync(ImportArtifactPaths artifactPaths, CriticalTableParseResult parseResult, CancellationToken cancellationToken = default)
    {
        Directory.CreateDirectory(artifactPaths.DirectoryPath);

        await File.WriteAllTextAsync(
            artifactPaths.FragmentsJsonPath,
            JsonSerializer.Serialize(parseResult.Fragments, JsonOptions),
            cancellationToken);

        await File.WriteAllTextAsync(
            artifactPaths.ParsedCellsJsonPath,
            JsonSerializer.Serialize(parseResult.Cells, JsonOptions),
            cancellationToken);

        await File.WriteAllTextAsync(
            artifactPaths.ValidationReportPath,
            JsonSerializer.Serialize(parseResult.ValidationReport, JsonOptions),
            cancellationToken);
    }
}
