using System;
using System.Collections.Generic;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace StellaOps.Feedser.Source.Cccs.Internal;

internal sealed class CccsFeedResponse
{
    [JsonPropertyName("ERROR")]
    public bool Error { get; init; }

    [JsonPropertyName("response")]
    public List<CccsFeedItem> Response { get; init; } = new();
}

internal sealed class CccsFeedItem
{
    [JsonPropertyName("nid")]
    public int Nid { get; init; }

    [JsonPropertyName("title")]
    public string? Title { get; init; }

    [JsonPropertyName("uuid")]
    public string? Uuid { get; init; }

    [JsonPropertyName("banner")]
    public string? Banner { get; init; }

    [JsonPropertyName("lang")]
    public string? Language { get; init; }

    [JsonPropertyName("date_modified")]
    public string? DateModified { get; init; }

    [JsonPropertyName("date_modified_ts")]
    public string? DateModifiedTimestamp { get; init; }

    [JsonPropertyName("date_created")]
    public string? DateCreated { get; init; }

    [JsonPropertyName("summary")]
    public string? Summary { get; init; }

    [JsonPropertyName("body")]
    public string[] Body { get; init; } = Array.Empty<string>();

    [JsonPropertyName("url")]
    public string? Url { get; init; }

    [JsonPropertyName("alert_type")]
    public JsonElement AlertType { get; init; }

    [JsonPropertyName("serial_number")]
    public string? SerialNumber { get; init; }

    [JsonPropertyName("subject")]
    public string? Subject { get; init; }

    [JsonPropertyName("moderation_state")]
    public string? ModerationState { get; init; }

    [JsonPropertyName("external_url")]
    public string? ExternalUrl { get; init; }
}

internal sealed class CccsTaxonomyResponse
{
    [JsonPropertyName("ERROR")]
    public bool Error { get; init; }

    [JsonPropertyName("response")]
    public List<CccsTaxonomyItem> Response { get; init; } = new();
}

internal sealed class CccsTaxonomyItem
{
    [JsonPropertyName("id")]
    public int Id { get; init; }

    [JsonPropertyName("title")]
    public string? Title { get; init; }
}

internal sealed record CccsFeedResult(
    IReadOnlyList<CccsFeedItem> Items,
    IReadOnlyDictionary<int, string> AlertTypes,
    DateTimeOffset? LastModifiedUtc)
{
    public static CccsFeedResult Empty { get; } = new(
        Array.Empty<CccsFeedItem>(),
        new Dictionary<int, string>(0),
        null);
}

internal static class CccsFeedResultExtensions
{
    public static CccsFeedResult ToResult(this IReadOnlyList<CccsFeedItem> items, DateTimeOffset? lastModified, IReadOnlyDictionary<int, string> alertTypes)
        => new(items, alertTypes, lastModified);
}
