// 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>
/// A single tracked change embedded in a document.
///
/// Populated by per-format extractors that understand change-tracking metadata
/// (DOCX `w:ins`/`w:del`/`w:rPrChange`, ODT `text:change-*`, …). Every
/// extractor defaults to `ExtractionResult.revisions = None` until a
/// format-specific implementation is added.
/// </summary>
public sealed record DocumentRevision
{
    /// <summary>
    /// Format-specific revision identifier.
    ///
    /// For DOCX this is the `w:id` attribute value on the change element
    /// (e.g. `"42"`). When the attribute is absent a synthetic fallback is
    /// generated (`"docx-ins-0"`, `"docx-del-3"`, …).
    /// </summary>
    [JsonPropertyName("revision_id")]
    public required string RevisionId { get; init; }

    /// <summary>
    /// Display name of the author who made this change, when available.
    /// </summary>
    [JsonPropertyName("author")]
    public string? Author { get; init; } = null;

    /// <summary>
    /// ISO-8601 timestamp of the change, when available.
    ///
    /// Stored as a plain string so this type remains FFI-friendly and
    /// unconditionally available without the `chrono` optional dep.
    /// DOCX populates this from the `w:date` attribute (e.g.
    /// `"2024-03-15T10:30:00Z"`).
    /// </summary>
    [JsonPropertyName("timestamp")]
    public string? Timestamp { get; init; } = null;

    /// <summary>
    /// Semantic kind of this revision.
    /// </summary>
    [JsonPropertyName("kind")]
    public required RevisionKind Kind { get; init; }

    /// <summary>
    /// Best-effort document location for this revision.
    ///
    /// Resolution is format-dependent and may be `None` when the location
    /// cannot be determined (e.g. changes inside table cells before
    /// table-cell anchor support is added).
    /// </summary>
    [JsonPropertyName("anchor")]
    public RevisionAnchor? Anchor { get; init; } = null;

    /// <summary>
    /// The content changes that make up this revision.
    /// </summary>
    [JsonPropertyName("delta")]
    public required RevisionDelta Delta { get; init; }


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