#[serde_with::serde_as]
#[derive(Debug,serde::Deserialize)]
pub struct Torrent {
	#[serde(rename="num_complete")] pub seeders: u64,
	#[serde(rename="num_incomplete")] pub leechers: u64,
	#[serde(rename="size")] pub selected_size: u64,
	#[serde_as(as="serde_with::DurationSeconds")] pub seeding_time: std::time::Duration,
	#[serde_as(as="serde_with::TimestampSeconds")] pub added_on: std::time::SystemTime,
	#[serde_as(as="serde_with::TimestampSeconds")] pub last_activity: std::time::SystemTime,
	pub category: String,
	pub hash: crate::InfoHash,
	pub name: String,
	pub private: Option<bool>, // Can be null when downloading metadata.
	pub ratio: f32,
	pub state: crate::TorrentState,
	pub total_size: i64, // Can be -1 if the size is unknown.
	pub tracker: String,
}

impl Torrent {
	pub fn cmp_natural_key(&self) -> impl Ord {
		(&self.tracker, &self.hash)
	}

	pub fn tracker(&self) -> Result<url::Url, url::ParseError> {
		self.tracker.parse()
	}

	pub fn tracker_host(&self) -> Option<String> {
		Some(
			self.tracker().ok()?
				.host_str()?
				.into())
	}
}
