// 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>
/// Keyword extraction configuration.
/// </summary>
public sealed record KeywordConfig
{
    /// <summary>
    /// Algorithm to use for extraction.
    /// </summary>
    [JsonPropertyName("algorithm")]
    public KeywordAlgorithm? Algorithm { get; init; } = null;

    /// <summary>
    /// Maximum number of keywords to extract (default: 10).
    /// </summary>
    [JsonPropertyName("max_keywords")]
    public ulong MaxKeywords { get; init; } = 10;

    /// <summary>
    /// Minimum score threshold (0.0-1.0, default: 0.0).
    ///
    /// Keywords with scores below this threshold are filtered out.
    /// Note: Score ranges differ between algorithms.
    /// </summary>
    [JsonPropertyName("min_score")]
    public float MinScore { get; init; } = 0.0f;

    /// <summary>
    /// N-gram range for keyword extraction (min, max).
    ///
    /// (1, 1) = unigrams only
    /// (1, 2) = unigrams and bigrams
    /// (1, 3) = unigrams, bigrams, and trigrams (default)
    /// </summary>
    [JsonPropertyName("ngram_range")]
    public List<ulong>? NgramRange { get; init; } = null;

    /// <summary>
    /// Language code for stopword filtering (e.g., "en", "de", "fr").
    ///
    /// If null, no stopword filtering is applied.
    /// </summary>
    [JsonPropertyName("language")]
    public string? Language { get; init; } = null;

    /// <summary>
    /// YAKE-specific tuning parameters.
    /// </summary>
    [JsonPropertyName("yake_params")]
    public YakeParams? YakeParams { get; init; } = null;

    /// <summary>
    /// RAKE-specific tuning parameters.
    /// </summary>
    [JsonPropertyName("rake_params")]
    public RakeParams? RakeParams { get; init; } = null;


    /// <summary>
    /// Parse a <see cref="KeywordConfig"/> from a JSON string.
    /// </summary>
    /// <exception cref="KreuzbergException">When the JSON cannot be deserialised.</exception>
    public static KeywordConfig FromJson(string json)
    {
        try
        {
            return JsonSerializer.Deserialize<KeywordConfig>(json, JsonOptions)
                ?? throw new KreuzbergException($"Failed to parse KeywordConfig from JSON: deserializer returned null");
        }
        catch (KreuzbergException)
        {
            throw;
        }
        catch (Exception e)
        {
            throw new KreuzbergException($"Failed to parse KeywordConfig 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 KeywordConfig Default()
    {
        var nativeResult = NativeMethods.KeywordConfigDefault();
        var jsonPtr = NativeMethods.KeywordConfigToJson(nativeResult);
        var json = global::System.Runtime.InteropServices.Marshal.PtrToStringUTF8(jsonPtr);
        NativeMethods.FreeString(jsonPtr);
        NativeMethods.KeywordConfigFree(nativeResult);
        return JsonSerializer.Deserialize<KeywordConfig>(json ?? "null", JsonOptions)!;
    }
}
