use libp2p::{Multiaddr, PeerId};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;
use chrono::{DateTime, Utc};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Peer {
    pub peer_id: PeerId,
    pub addresses: HashSet<Multiaddr>,
    pub public_ip: Option<String>,
    pub last_seen: DateTime<Utc>,
    pub listen_ports: HashSet<u16>,
}

impl Peer {
    pub fn new(peer_id: PeerId) -> Self {
        Self {
            peer_id,
            addresses: Default::default(),
            public_ip: None,
            last_seen: Utc::now(),
            listen_ports: Default::default(),
        }
    }

    pub fn add_address(&mut self, addr: Multiaddr) {
        self.addresses.insert(addr);
        self.last_seen = Utc::now();
    }

    pub fn set_public_ip(&mut self, ip: String) {
        self.public_ip = Some(ip);
        self.last_seen = Utc::now();
    }

    pub fn add_listen_port(&mut self, port: u16) {
        self.listen_ports.insert(port);
        self.last_seen = Utc::now();
    }

    pub fn update_last_seen(&mut self) {
        self.last_seen = Utc::now();
    }
}