// 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 backend stage in the OCR pipeline.
/// </summary>
public sealed record OcrPipelineStage
{
    /// <summary>
    /// Backend name: "tesseract", "paddleocr", "easyocr", or a custom registered name.
    /// </summary>
    [JsonPropertyName("backend")]
    public required string Backend { get; init; }

    /// <summary>
    /// Priority weight (higher = tried first). Stages are sorted by priority descending.
    /// </summary>
    [JsonPropertyName("priority")]
    public uint Priority { get; init; } = 0;

    /// <summary>
    /// Language override for this stage (null = use parent OcrConfig.language).
    /// </summary>
    [JsonPropertyName("language")]
    public string? Language { get; init; } = null;

    /// <summary>
    /// Tesseract-specific config override for this stage.
    /// </summary>
    [JsonPropertyName("tesseract_config")]
    public TesseractConfig? TesseractConfig { get; init; } = null;

    /// <summary>
    /// PaddleOCR-specific config for this stage.
    /// </summary>
    [JsonPropertyName("paddle_ocr_config")]
    public string? PaddleOcrConfig { get; init; } = null;

    /// <summary>
    /// VLM config override for this pipeline stage.
    /// </summary>
    [JsonPropertyName("vlm_config")]
    public LlmConfig? VlmConfig { get; init; } = null;

    /// <summary>
    /// Arbitrary per-call options passed through to the backend unchanged.
    ///
    /// Backends that support runtime tuning (mode switching, preprocessing
    /// flags, inference parameters, etc.) read this value and deserialize
    /// the keys they care about. Keys unknown to the backend are silently
    /// ignored, so options from different backends can coexist in the same
    /// config without conflict.
    ///
    /// Example (custom backend):
    /// ```json
    /// { "mode": "fast", "enable_layout": true }
    /// ```
    /// </summary>
    [JsonPropertyName("backend_options")]
    public string? BackendOptions { get; init; } = null;


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