// 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 styled HTML output.
///
/// When set on `ExtractionConfig.html_output` alongside
/// `output_format = OutputFormat.Html`, the pipeline builds a
/// `StyledHtmlRenderer`(crate.rendering.StyledHtmlRenderer) instead of
/// the plain comrak-based renderer.
/// </summary>
public sealed record HtmlOutputConfig
{
    /// <summary>
    /// Inline CSS string injected into the output after the theme stylesheet.
    /// Concatenated after `css_file` content when both are set.
    /// </summary>
    [JsonPropertyName("css")]
    public string? Css { get; init; } = null;

    /// <summary>
    /// Path to a CSS file loaded once at renderer construction time.
    /// Concatenated before `css` when both are set.
    /// </summary>
    [JsonPropertyName("css_file")]
    public string? CssFile { get; init; } = null;

    /// <summary>
    /// Built-in colour/typography theme. Default: `HtmlTheme.Unstyled`.
    /// </summary>
    [JsonPropertyName("theme")]
    public HtmlTheme Theme { get; init; } = HtmlTheme.Unstyled;

    /// <summary>
    /// CSS class prefix applied to every emitted class name.
    ///
    /// Default: `"kb-"`. Change this if your host application already uses
    /// classes that start with `kb-`.
    /// </summary>
    [JsonPropertyName("class_prefix")]
    public string ClassPrefix { get; init; } = "";

    /// <summary>
    /// When `true` (default), write the resolved CSS into a `&lt;style&gt;` block
    /// immediately after the opening `&lt;div class="{prefix}doc"&gt;`.
    ///
    /// Set to `false` to emit only the structural markup and wire up your
    /// own stylesheet targeting the `kb-*` class names.
    /// </summary>
    [JsonPropertyName("embed_css")]
    public bool EmbedCss { get; init; } = true;


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

    public static HtmlOutputConfig Default()
    {
        var nativeResult = NativeMethods.HtmlOutputConfigDefault();
        var jsonPtr = NativeMethods.HtmlOutputConfigToJson(nativeResult);
        var json = global::System.Runtime.InteropServices.Marshal.PtrToStringUTF8(jsonPtr);
        NativeMethods.FreeString(jsonPtr);
        NativeMethods.HtmlOutputConfigFree(nativeResult);
        return JsonSerializer.Deserialize<HtmlOutputConfig>(json ?? "null", JsonOptions)!;
    }
}
