// 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>
/// Metadata for individual page/slide/sheet.
///
/// Captures per-page information including dimensions, content counts,
/// and visibility state (for presentations).
/// </summary>
public sealed record PageInfo
{
    /// <summary>
    /// Page number (1-indexed)
    /// </summary>
    [JsonPropertyName("number")]
    public uint Number { get; init; } = 0;

    /// <summary>
    /// Page title (usually for presentations)
    /// </summary>
    [JsonPropertyName("title")]
    public string? Title { get; init; } = null;

    /// <summary>
    /// Dimensions in points (PDF) or pixels (images): (width, height)
    /// </summary>
    [JsonPropertyName("dimensions")]
    public List<double>? Dimensions { get; init; } = null;

    /// <summary>
    /// Number of images on this page
    /// </summary>
    [JsonPropertyName("image_count")]
    public uint? ImageCount { get; init; } = null;

    /// <summary>
    /// Number of tables on this page
    /// </summary>
    [JsonPropertyName("table_count")]
    public uint? TableCount { get; init; } = null;

    /// <summary>
    /// Whether this page is hidden (e.g., in presentations)
    /// </summary>
    [JsonPropertyName("hidden")]
    public bool? Hidden { get; init; } = null;

    /// <summary>
    /// Whether this page is blank (no meaningful text, no images, no tables)
    ///
    /// A page is considered blank if it has fewer than 3 non-whitespace characters
    /// and contains no tables or images. This is useful for filtering out empty pages
    /// in scanned documents or PDFs with blank separator pages.
    /// </summary>
    [JsonPropertyName("is_blank")]
    public bool? IsBlank { get; init; } = null;

    /// <summary>
    /// Whether this page contains non-trivial vector graphics (paths, shapes, curves)
    ///
    /// Indicates the presence of vector-drawn content such as charts, diagrams,
    /// or geometric shapes (e.g., from Adobe InDesign, LaTeX TikZ). These are
    /// invisible to `ExtractionResult.images` since they are not embedded as raster
    /// XObjects. Set to `true` when path count exceeds a heuristic threshold,
    /// signaling that downstream consumers may want to rasterize the page to
    /// capture this content.
    ///
    /// Only populated for PDFs; `None` for other document types.
    /// </summary>
    [JsonPropertyName("has_vector_graphics")]
    public bool HasVectorGraphics { get; init; } = false;


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