// This file is auto-generated by alef — DO NOT EDIT.
// alef:hash:4e15143f4af1ae8bafbdb1506ef057da924484c66a19483966333558ad437e75
// To regenerate: alef generate
// To verify freshness: alef verify --exit-code
// Issues & docs: https://github.com/kreuzberg-dev/alef
#nullable enable

using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Kreuzberg;

/// <summary>
/// The complete diff between two `ExtractionResult` values.
/// </summary>
public sealed record ExtractionDiff
{
    /// <summary>
    /// Unified-diff hunks for the `content` field.
    ///
    /// Empty when the content is identical.
    /// </summary>
    [JsonPropertyName("content_diff")]
    public List<DiffHunk> ContentDiff { get; init; } = [];

    /// <summary>
    /// Tables present in `b` but not in `a` (by index position, excess right-side tables).
    /// </summary>
    [JsonPropertyName("tables_added")]
    public List<Table> TablesAdded { get; init; } = [];

    /// <summary>
    /// Tables present in `a` but not in `b` (by index position, excess left-side tables).
    /// </summary>
    [JsonPropertyName("tables_removed")]
    public List<Table> TablesRemoved { get; init; } = [];

    /// <summary>
    /// Cell-level changes for table pairs that share the same index and dimensions.
    /// </summary>
    [JsonPropertyName("tables_changed")]
    public List<TableDiff> TablesChanged { get; init; } = [];

    /// <summary>
    /// Metadata difference, encoded as a JSON object with three top-level keys:
    /// `added` (keys present in `b` but not `a`), `removed` (keys present in `a`
    /// but not `b`), and `changed` (keys whose values differ — each entry is
    /// `{ "from": &lt;value-in-a&gt;, "to": &lt;value-in-b&gt; }`).
    ///
    /// This is NOT RFC 6902 JSON Patch — we deliberately chose a flatter shape
    /// to avoid pulling in a json-patch crate. If you need RFC 6902 semantics
    /// (with JSON Pointer paths) feed `a.metadata` and `b.metadata` to your
    /// preferred json-patch impl directly.
    /// </summary>
    [JsonPropertyName("metadata_changed")]
    public required string MetadataChanged { get; init; }

    /// <summary>
    /// Changes to embedded archive children.
    /// </summary>
    [JsonPropertyName("embedded_changes")]
    public required EmbeddedChanges EmbeddedChanges { get; init; }


    /// <summary>
    /// Parse a <see cref="ExtractionDiff"/> from a JSON string.
    /// </summary>
    /// <exception cref="KreuzbergException">When the JSON cannot be deserialised.</exception>
    public static ExtractionDiff FromJson(string json)
    {
        try
        {
            return JsonSerializer.Deserialize<ExtractionDiff>(json, JsonOptions)
                ?? throw new KreuzbergException($"Failed to parse ExtractionDiff from JSON: deserializer returned null");
        }
        catch (KreuzbergException)
        {
            throw;
        }
        catch (Exception e)
        {
            throw new KreuzbergException($"Failed to parse ExtractionDiff from JSON: {e.Message}", e);
        }
    }

    private static readonly JsonSerializerOptions JsonOptions = new()
    {
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingDefault,
        Converters = { new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseLower) },
    };

    /// <summary>Options for serializing config/input objects to FFI. Strips nulls
    /// (nullable C# fields default to null and would override required Rust fields with
    /// non-deserialisable nulls) but preserves explicit false/0 so caller intent is kept.</summary>
    private static readonly JsonSerializerOptions JsonSerializationOptions = new()
    {
        DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
        Converters = { new JsonStringEnumConverter(JsonNamingPolicy.SnakeCaseLower) },
    };
}
