// 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>
/// PowerPoint (PPTX) extraction result.
///
/// Contains extracted slide content, metadata, and embedded images/tables.
/// </summary>
public sealed record PptxExtractionResult
{
    /// <summary>
    /// Extracted text content from all slides
    /// </summary>
    [JsonPropertyName("content")]
    public required string Content { get; init; }

    /// <summary>
    /// Presentation metadata
    /// </summary>
    [JsonPropertyName("metadata")]
    public required PptxMetadata Metadata { get; init; }

    /// <summary>
    /// Total number of slides
    /// </summary>
    [JsonPropertyName("slide_count")]
    public ulong SlideCount { get; init; } = 0;

    /// <summary>
    /// Total number of embedded images
    /// </summary>
    [JsonPropertyName("image_count")]
    public ulong ImageCount { get; init; } = 0;

    /// <summary>
    /// Total number of tables
    /// </summary>
    [JsonPropertyName("table_count")]
    public ulong TableCount { get; init; } = 0;

    /// <summary>
    /// Extracted images from the presentation
    /// </summary>
    [JsonPropertyName("images")]
    public List<ExtractedImage> Images { get; init; } = [];

    /// <summary>
    /// Slide structure with boundaries (when page tracking is enabled)
    /// </summary>
    [JsonPropertyName("page_structure")]
    public PageStructure? PageStructure { get; init; } = null;

    /// <summary>
    /// Per-slide content (when page tracking is enabled)
    /// </summary>
    [JsonPropertyName("page_contents")]
    public List<PageContent>? PageContents { get; init; } = null;

    /// <summary>
    /// Structured document representation
    /// </summary>
    [JsonPropertyName("document")]
    public DocumentStructure? Document { get; init; } = null;

    /// <summary>
    /// Hyperlinks discovered in slides as (url, optional_label) pairs.
    /// </summary>
    [JsonPropertyName("hyperlinks")]
    public List<string>? Hyperlinks { get; init; } = null;

    /// <summary>
    /// Office metadata extracted from docProps/core.xml and docProps/app.xml.
    ///
    /// Contains keys like "title", "author", "created_by", "subject", "keywords",
    /// "modified_by", "created_at", "modified_at", etc.
    /// </summary>
    [JsonPropertyName("office_metadata")]
    public Dictionary<string, string> OfficeMetadata { get; init; } = new Dictionary<string, string>();

    /// <summary>
    /// Slide comments as revisions.
    ///
    /// Each `&lt;p:cm&gt;` element in `ppt/comments/comment{N}.xml` becomes a
    /// `DocumentRevision { kind: Comment }` with author (resolved from
    /// `ppt/commentAuthors.xml`), ISO-8601 timestamp, and
    /// `RevisionAnchor.Slide { index }`. `None` when no comment XML parts exist.
    /// </summary>
    [JsonPropertyName("revisions")]
    public List<DocumentRevision>? Revisions { get; init; } = null;


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