// 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>
/// OCR extraction result.
///
/// Result of performing OCR on an image or scanned document,
/// including recognized text and detected tables.
/// </summary>
public sealed record OcrExtractionResult
{
    /// <summary>
    /// Recognized text content
    /// </summary>
    [JsonPropertyName("content")]
    public required string Content { get; init; }

    /// <summary>
    /// Original MIME type of the processed image
    /// </summary>
    [JsonPropertyName("mime_type")]
    public required string MimeType { get; init; }

    /// <summary>
    /// OCR processing metadata (confidence scores, language, etc.)
    /// </summary>
    [JsonPropertyName("metadata")]
    public Dictionary<string, string> Metadata { get; init; } = default!;

    /// <summary>
    /// Tables detected and extracted via OCR
    /// </summary>
    [JsonPropertyName("tables")]
    public List<OcrTable> Tables { get; init; } = [];

    /// <summary>
    /// Structured OCR elements with bounding boxes and confidence scores.
    /// Available when TSV output is requested or table detection is enabled.
    /// </summary>
    [JsonPropertyName("ocr_elements")]
    public List<OcrElement>? OcrElements { get; init; } = null;

    /// <summary>
    /// Structured document produced from hOCR parsing.
    /// Carries paragraph structure, bounding boxes, and confidence scores
    /// that the flattened `content` string discards.
    /// </summary>
    [JsonPropertyName("internal_document")]
    public string? InternalDocument { get; init; } = null;


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