// SPDX-FileCopyrightText: 2025 Slatian
//
// SPDX-License-Identifier: LGPL-3.0-only

/// Implementing this trait makes it possible to
/// use prebuilt number matching facilities.
///
/// Note that this isn't unit preserving!
///
/// Here is what the default implementations do:
/// * The default implementation for timestamps returns a Unix Timestamp in seconds
pub trait AsInteger {
	/// Converts whatever implements this trait into an i64 for comparing against some other number using a [NumberCriterium][crate::NumberCriterium].
	fn as_criterium_i64(&self) -> i64;
}

impl AsInteger for i64 {
	fn as_criterium_i64(&self) -> i64 {
		return *self;
	}
}

macro_rules! impl_primitive_as_integer {
	($e:ty) => {
		impl AsInteger for $e {
			fn as_criterium_i64(&self) -> i64 {
				return *self as i64;
			}
		}
	};
}

impl_primitive_as_integer!(i8);
impl_primitive_as_integer!(u8);
impl_primitive_as_integer!(i16);
impl_primitive_as_integer!(u16);
impl_primitive_as_integer!(i32);
impl_primitive_as_integer!(u32);

#[cfg(feature = "chrono")]
mod chrono {
	use super::AsInteger;
	use chrono::DateTime;
	use chrono::TimeZone;

	/// Converts a Chrono DateTime to a Unix UTC Timestamp
	/// using the [timestamp()][chrono::DateTime::timestamp()] method.
	impl<Tz: TimeZone> AsInteger for DateTime<Tz> {
		fn as_criterium_i64(&self) -> i64 {
			return self.timestamp();
		}
	}
}
