// 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>
/// HTML metadata extracted from HTML documents.
///
/// Includes document-level metadata, Open Graph data, Twitter Card metadata,
/// and extracted structural elements (headers, links, images, structured data).
/// </summary>
public sealed record HtmlMetadata
{
    /// <summary>
    /// Document title from `&lt;title&gt;` tag
    /// </summary>
    [JsonPropertyName("title")]
    public string? Title { get; init; } = null;

    /// <summary>
    /// Document description from `&lt;meta name="description"&gt;` tag
    /// </summary>
    [JsonPropertyName("description")]
    public string? Description { get; init; } = null;

    /// <summary>
    /// Document keywords from `&lt;meta name="keywords"&gt;` tag, split on commas
    /// </summary>
    [JsonPropertyName("keywords")]
    public List<string> Keywords { get; init; } = [];

    /// <summary>
    /// Document author from `&lt;meta name="author"&gt;` tag
    /// </summary>
    [JsonPropertyName("author")]
    public string? Author { get; init; } = null;

    /// <summary>
    /// Canonical URL from `&lt;link rel="canonical"&gt;` tag
    /// </summary>
    [JsonPropertyName("canonical_url")]
    public string? CanonicalUrl { get; init; } = null;

    /// <summary>
    /// Base URL from `&lt;base href=""&gt;` tag for resolving relative URLs
    /// </summary>
    [JsonPropertyName("base_href")]
    public string? BaseHref { get; init; } = null;

    /// <summary>
    /// Document language from `lang` attribute
    /// </summary>
    [JsonPropertyName("language")]
    public string? Language { get; init; } = null;

    /// <summary>
    /// Document text direction from `dir` attribute
    /// </summary>
    [JsonConverter(typeof(TextDirectionJsonConverter))]
    [JsonPropertyName("text_direction")]
    public TextDirection? TextDirection { get; init; } = null;

    /// <summary>
    /// Open Graph metadata (og:* properties) for social media
    /// Keys like "title", "description", "image", "url", etc.
    /// </summary>
    [JsonPropertyName("open_graph")]
    public Dictionary<string, string> OpenGraph { get; init; } = new Dictionary<string, string>();

    /// <summary>
    /// Twitter Card metadata (twitter:* properties)
    /// Keys like "card", "site", "creator", "title", "description", "image", etc.
    /// </summary>
    [JsonPropertyName("twitter_card")]
    public Dictionary<string, string> TwitterCard { get; init; } = new Dictionary<string, string>();

    /// <summary>
    /// Additional meta tags not covered by specific fields
    /// Keys are meta name/property attributes, values are content
    /// </summary>
    [JsonPropertyName("meta_tags")]
    public Dictionary<string, string> MetaTags { get; init; } = new Dictionary<string, string>();

    /// <summary>
    /// Extracted header elements with hierarchy
    /// </summary>
    [JsonPropertyName("headers")]
    public List<HeaderMetadata> Headers { get; init; } = [];

    /// <summary>
    /// Extracted hyperlinks with type classification
    /// </summary>
    [JsonPropertyName("links")]
    public List<LinkMetadata> Links { get; init; } = [];

    /// <summary>
    /// Extracted images with source and dimensions
    /// </summary>
    [JsonPropertyName("images")]
    public List<ImageMetadataType> Images { get; init; } = [];

    /// <summary>
    /// Extracted structured data blocks
    /// </summary>
    [JsonPropertyName("structured_data")]
    public List<StructuredData> StructuredData { get; init; } = [];


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