#[derive(Debug)]
pub struct Group {
	pub pinned: Option<crate::PinReason>,
	size: u64, // The logical group size.
	pub torrents: Vec<crate::Torrent>,
}

impl Group {
	pub fn new(size: u64) -> Self {
		Group {
			pinned: None,
			size,
			torrents: Vec::new(),
		}
	}
}

impl Group {
	pub fn last_activity(&self, now: std::time::SystemTime) -> std::time::Duration {
		self.torrents.iter()
			.map(|t| t.last_activity.duration_since(now).unwrap_or_default())
			.min()
			.unwrap()
	}

	pub fn min_age(&self, now: std::time::SystemTime) -> std::time::Duration {
		self.torrents.iter()
			.map(|t| t.added_on.duration_since(now).unwrap_or_default())
			.min()
			.unwrap()
	}

	pub fn rank(&self, global: &crate::Global) -> f64 {
		self.min_age(global.now).as_secs_f64() / 3600.0 / 24.0 * global.config.age_d_max_weight
		+ self.last_activity(global.now).as_secs_f64() / 3600.0 / 24.0 * global.config.last_activity_d_weight
		+ self.seeders() as f64 * global.config.seeder_count_weight
		+ self.content_size() as f64 * global.config.size_weight
	}

	pub fn seeders(&self) -> u64 {
		self.torrents.iter()
			.map(|t| t.seeders)
			.sum()
	}

	pub fn group_id(&self) -> u64 {
		self.size
	}

	/// Best guess of the amount of data that will be recovered by deleting this torrent.
	pub fn content_size(&self) -> u64 {
		// In theory we should try to identify distinct different files and count them. But in practice this will be close enough.
		self.torrents.iter()
			.map(|t| t.selected_size)
			.max()
			.unwrap()
	}
}
