// 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>
/// Configuration for LLM-based structured data extraction.
///
/// Sends extracted document content to a VLM with a JSON schema,
/// returning structured data that conforms to the schema.
/// </summary>
public sealed record StructuredExtractionConfig
{
    /// <summary>
    /// JSON Schema defining the desired output structure.
    /// </summary>
    [JsonPropertyName("schema")]
    public required string Schema { get; init; }

    /// <summary>
    /// Schema name passed to the LLM's structured output mode.
    /// </summary>
    [JsonPropertyName("schema_name")]
    public string SchemaName { get; init; } = "";

    /// <summary>
    /// Optional schema description for the LLM.
    /// </summary>
    [JsonPropertyName("schema_description")]
    public string? SchemaDescription { get; init; } = null;

    /// <summary>
    /// Enable strict mode — output must exactly match the schema.
    /// </summary>
    [JsonPropertyName("strict")]
    public bool Strict { get; init; } = false;

    /// <summary>
    /// Custom Jinja2 extraction prompt template. When `None`, a default template is used.
    ///
    /// Available template variables:
    /// - `{{ content }}` — The extracted document text.
    /// - `{{ schema }}` — The JSON schema as a formatted string.
    /// - `{{ schema_name }}` — The schema name.
    /// - `{{ schema_description }}` — The schema description (may be empty).
    /// </summary>
    [JsonPropertyName("prompt")]
    public string? Prompt { get; init; } = null;

    /// <summary>
    /// LLM configuration for the extraction.
    /// </summary>
    [JsonPropertyName("llm")]
    public required LlmConfig Llm { get; init; }


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