using MongoDB.Bson;
using MongoDB.Bson.Serialization.Attributes;
using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace Insight.Infrastructure.Entities
{
    [BsonIgnoreExtraElements]
    public class AgentEntity
    {
        [BsonId, BsonRepresentation(BsonType.ObjectId), JsonPropertyName("id")]
        public string? Id { get; set; }

        [BsonElement("insert")]
        public DateTime? Insert { get; set; }

        [BsonElement("update")]
        public DateTime? Update { get; set; }

        [BsonElement("serial"), Required]
        public string? Serial { get; set; }

        [BsonElement("hostname")]
        public string? Hostname { get; set; }

        [BsonElement("version"), BsonRepresentation(BsonType.String)]
        public Version? Version { get; set; }

        [BsonElement("endpoint"), BsonRepresentation(BsonType.String)]
        public string? Endpoint { get; set; }

        [BsonElement("connected")]
        public DateTime? Connected { get; set; }

        [BsonElement("activity")]
        public DateTime? Activity { get; set; }

        [BsonElement("bytes_sent")]
        public long SentBytes { get; set; }

        [BsonElement("bytes_received")]
        public long ReceivedBytes { get; set; }

        [BsonElement("packets_sent")]
        public long SentPackets { get; set; }

        [BsonElement("packets_received")]
        public long ReceivedPackets { get; set; }

        //[BsonElement("latency")]
        //public TimeSpan? Latency { get; set; }

        [BsonIgnoreIfNull, JsonIgnore]
        public List<HostEntity>? Hosts { get; set; }

        public bool GetOnlineState()
        {
            if (Activity is null) return false;
            return Activity.Value.ToLocalTime().Add(TimeSpan.FromSeconds(60)).Subtract(DateTime.Now).Seconds > 0;
        }
    }
}