using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;

namespace Nalu.Web.Data.Models;

/// <summary>
/// One document per (api_key + year_month). Primary record for credit-based rate limiting.
/// All counters updated atomically via $inc. Upserted on first call of each month.
/// </summary>
public class UsageMonthly
{
    [BsonId]
    [BsonRepresentation(BsonType.ObjectId)]
    public string Id { get; set; } = ObjectId.GenerateNewId().ToString();

    [BsonElement("api_key")]
    public required string ApiKey { get; set; }

    [BsonElement("plan")]
    public required string Plan { get; set; }

    /// "YYYY-MM"
    [BsonElement("year_month")]
    public required string YearMonth { get; set; }

    [BsonElement("total_credits_used")]
    public int TotalCreditsUsed { get; set; }

    [BsonElement("total_requests")]
    public int TotalRequests { get; set; }

    /// validator_id → credits consumed
    [BsonElement("credits_by_validator")]
    public Dictionary<string, int> CreditsByValidator { get; set; } = new();

    /// validator_id → request count
    [BsonElement("requests_by_validator")]
    public Dictionary<string, int> RequestsByValidator { get; set; } = new();

    [BsonElement("updated_at")]
    public DateTime UpdatedAt { get; set; } = DateTime.UtcNow;
}
