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

namespace QRRapidoApp.Models
{
    public class User
    {
        [BsonId]
        [BsonRepresentation(BsonType.ObjectId)]
        public string Id { get; set; } = string.Empty;

        [BsonElement("email")]
        public string Email { get; set; } = string.Empty;

        [BsonElement("name")]
        public string Name { get; set; } = string.Empty;

        [BsonElement("provider")]
        public string Provider { get; set; } = string.Empty; // Google, Microsoft

        [BsonElement("providerId")]
        public string ProviderId { get; set; } = string.Empty;

        [BsonElement("isPremium")]
        public bool IsPremium { get; set; } = false;

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

        [BsonElement("premiumExpiresAt")]
        public DateTime? PremiumExpiresAt { get; set; }

        [BsonElement("premiumCancelledAt")]
        public DateTime? PremiumCancelledAt { get; set; } // ADICIONADO: Data do cancelamento

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

        [BsonElement("qrHistoryIds")]
        public List<string> QRHistoryIds { get; set; } = new();

        [BsonElement("stripeCustomerId")]
        public string? StripeCustomerId { get; set; }

        [BsonElement("stripeSubscriptionId")]
        public string? StripeSubscriptionId { get; set; }

        [BsonElement("subscriptionStartedAt")]
        public DateTime? SubscriptionStartedAt { get; set; } // Data de início da assinatura atual

        [BsonElement("preferredLanguage")]
        public string PreferredLanguage { get; set; } = "pt-BR";

        [BsonElement("dailyQRCount")]
        public int DailyQRCount { get; set; } = 0;

        [BsonElement("lastQRDate")]
        public DateTime LastQRDate { get; set; } = DateTime.UtcNow.Date;

        [BsonElement("totalQRGenerated")]
        public int TotalQRGenerated { get; set; } = 0;

        // NEW: Credit System
        [BsonElement("credits")]
        public int Credits { get; set; } = 0;

        [BsonElement("freeQRsUsed")]
        public int FreeQRsUsed { get; set; } = 0; // Tracks usage of the 5 free QRs limit

        [BsonElement("historyHashes")]
        public List<string> HistoryHashes { get; set; } = new(); // Stores SHA256 hashes of generated content to prevent double charging

        [BsonElement("apiKeys")]
        public List<ApiKeyConfig> ApiKeys { get; set; } = new();

        /// <summary>API subscription plan (separate from QR credits).</summary>
        [BsonElement("apiSubscription")]
        public ApiSubscription ApiSubscription { get; set; } = new();
    }

    public class ApiKeyConfig
    {
        [BsonElement("keyHash")]
        public string KeyHash { get; set; } = string.Empty;

        [BsonElement("prefix")]
        public string Prefix { get; set; } = string.Empty; // Ex: qr_...

        [BsonElement("name")]
        public string Name { get; set; } = "Default";

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

        [BsonElement("isActive")]
        public bool IsActive { get; set; } = true;

        [BsonElement("lastUsedAt")]
        public DateTime? LastUsedAt { get; set; }

        [BsonElement("planTier")]
        public ApiPlanTier PlanTier { get; set; } = ApiPlanTier.Free;

        [BsonElement("totalRequests")]
        public long TotalRequests { get; set; } = 0;
    }
}