using System.Text.Json.Serialization;

namespace ClawdDotNet.Core.Api.Models;

public sealed class ChatResponse
{
    [JsonPropertyName("id")]
    public string Id { get; set; } = "";

    [JsonPropertyName("choices")]
    public List<Choice> Choices { get; set; } = new();

    [JsonPropertyName("usage")]
    public Usage? Usage { get; set; }

    [JsonPropertyName("model")]
    public string? Model { get; set; }

    [JsonPropertyName("error")]
    public ApiError? Error { get; set; }
}

public sealed class Choice
{
    [JsonPropertyName("index")]
    public int Index { get; set; }

    [JsonPropertyName("message")]
    public ChatMessage Message { get; set; } = new();

    [JsonPropertyName("finish_reason")]
    public string? FinishReason { get; set; }
}

public sealed class Usage
{
    [JsonPropertyName("prompt_tokens")]
    public int PromptTokens { get; set; }

    [JsonPropertyName("completion_tokens")]
    public int CompletionTokens { get; set; }

    [JsonPropertyName("total_tokens")]
    public int TotalTokens { get; set; }

    [JsonPropertyName("prompt_tokens_details")]
    public PromptTokensDetails? PromptTokensDetails { get; set; }

    /// <summary>
    /// Anteil der Prompt-Tokens, der aus dem Cache gelesen wurde. Nur damit lässt sich
    /// belegen, ob das Prompt-Caching tatsächlich greift — ein still wirkungsloser
    /// Cache wäre sonst nicht zu bemerken.
    /// </summary>
    [JsonIgnore]
    public int CachedTokens => PromptTokensDetails?.CachedTokens ?? 0;
}

public sealed class PromptTokensDetails
{
    [JsonPropertyName("cached_tokens")]
    public int CachedTokens { get; set; }
}

public sealed class ApiError
{
    [JsonPropertyName("message")]
    public string Message { get; set; } = "";

    [JsonPropertyName("code")]
    public int? Code { get; set; }
}
