// 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>
/// Batch item for byte array extraction.
///
/// Used with `batch_extract_bytes` and `batch_extract_bytes_sync`
/// to represent a single item in a batch extraction job.
/// </summary>
public sealed record BatchBytesItem
{
    /// <summary>
    /// The content bytes to extract from
    /// </summary>
    [JsonConverter(typeof(ByteArrayToIntArrayConverter))]
    [JsonPropertyName("content")]
    public byte[] Content { get; init; } = [];

    /// <summary>
    /// MIME type of the content (e.g., "application/pdf", "text/html")
    /// </summary>
    [JsonPropertyName("mime_type")]
    public required string MimeType { get; init; }

    /// <summary>
    /// Per-item configuration overrides (null uses batch-level defaults)
    /// </summary>
    [JsonPropertyName("config")]
    public FileExtractionConfig? Config { get; init; } = null;


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