use axum::http::HeaderMap;

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RequestContext {
	pub client_info: ClientInfo,
	pub route: String,
}

//
//
//

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DeviceType {
	Mobile,
	Desktop,
}

impl Default for DeviceType {
	fn default() -> Self {
		Self::Desktop
	}
}

//
// MARK: clientinfo
//

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct ClientInfo {
	/// This is an estimate, but it's probably good enough.
	pub device_type: DeviceType,
}

impl ClientInfo {
	pub fn from_headers(headers: &HeaderMap) -> Self {
		let ua = headers
			.get("user-agent")
			.and_then(|x| x.to_str().ok())
			.unwrap_or("");

		let ch_mobile = headers
			.get("Sec-CH-UA-Mobile")
			.and_then(|x| x.to_str().ok())
			.unwrap_or("");

		let mut device_type = None;

		if device_type.is_none() && ch_mobile.contains("1") {
			device_type = Some(DeviceType::Mobile);
		}

		if device_type.is_none() && ua.contains("Mobile") {
			device_type = Some(DeviceType::Mobile);
		}

		Self {
			device_type: device_type.unwrap_or_default(),
		}
	}
}
