// 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>
/// Single Excel worksheet.
///
/// Represents one sheet from an Excel workbook with its content
/// converted to Markdown format and dimensional statistics.
/// </summary>
public sealed record ExcelSheet
{
    /// <summary>
    /// Sheet name as it appears in Excel
    /// </summary>
    [JsonPropertyName("name")]
    public required string Name { get; init; }

    /// <summary>
    /// Sheet content converted to Markdown tables
    /// </summary>
    [JsonPropertyName("markdown")]
    public required string Markdown { get; init; }

    /// <summary>
    /// Number of rows
    /// </summary>
    [JsonPropertyName("row_count")]
    public ulong RowCount { get; init; } = 0;

    /// <summary>
    /// Number of columns
    /// </summary>
    [JsonPropertyName("col_count")]
    public ulong ColCount { get; init; } = 0;

    /// <summary>
    /// Total number of non-empty cells
    /// </summary>
    [JsonPropertyName("cell_count")]
    public ulong CellCount { get; init; } = 0;

    /// <summary>
    /// Pre-extracted table cells (2D vector of cell values)
    /// Populated during markdown generation to avoid re-parsing markdown.
    /// null for empty sheets.
    /// </summary>
    [JsonPropertyName("table_cells")]
    public List<List<string>>? TableCells { get; init; } = null;


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