const CARGO_PKG_NAME: &str = env!("CARGO_PKG_NAME");
const CARGO_PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
const CARGO_PKG_REPOSITORY: &str = env!("CARGO_PKG_REPOSITORY");


fn init_log() {
	let mut builder = pretty_env_logger::formatted_builder();
	builder.target(pretty_env_logger::env_logger::Target::Stdout);
	builder.filter_level(log::LevelFilter::max());
	builder.is_test(true);

	if builder.try_init().is_ok() {
		log::debug!("Initialized logging!");
	}
}

fn make_client() -> reqwest::Client {
	let user_agent = format!("{CARGO_PKG_NAME}/{CARGO_PKG_VERSION} ({CARGO_PKG_REPOSITORY})");
	
	reqwest::Client::builder()
		.user_agent(user_agent)
		.build()
		.expect("reqwest client to build")
}


macro_rules! test_discover_hostmeta {
	($id:ident, $url:literal) => {
		test_discover_hostmeta!($id, $url,);
	};
	($id:ident, $url:literal, $($tag:meta),*) => {
		#[tokio::test]
		$(#[$tag])*
		async fn $id() {
			init_log();
			let client = make_client();

			let base: reqwest::Url = $url.parse()
				.expect("a valid URL");

			let doc = acrate_rd::any::ResourceDescriptor::discover_hostmeta(&client, base)
				.await
				.expect("host-meta discovery to succeed");

			log::info!("Parsed host-meta document: {doc:#?}");
		}
	};
}

macro_rules! test_de_ser_jrd {
	($id:ident, $file:literal) => {
		test_de_ser_jrd!($id, $file,);
	};
	($id:ident, $file:literal, $($tag:meta),*) => {
		const JRD_DOCUMENT: &'static str = include_str!($file);

		#[test]
		$(#[$tag])*
		fn $id() {
			init_log();
			
			log::info!("Starting document: {:#?}", JRD_DOCUMENT);
			
			let de: acrate_rd::jrd::ResourceDescriptorJRD = serde_json::from_str(JRD_DOCUMENT)
				.expect("document to be deserialized successfully");
			
			log::info!("Serialized document: {de:#?}");
			
			let ser = serde_json::to_string(&de)
				.expect("document to be serialized successfully");
			
			log::info!("Deserialized document: {ser:#?}");
		}
	};
}

macro_rules! test_de_ser_xrd {
	($id:ident, $file:literal) => {
		test_de_ser_xrd!($id, $file,);
	};
	($id:ident, $file:literal, $($tag:meta),*) => {
		const XRD_DOCUMENT: &'static str = include_str!($file);

		#[test]
		$(#[$tag])*
		fn $id() {
			init_log();
			
			log::info!("Starting document: {:#?}", XRD_DOCUMENT);
			
			let de: acrate_rd::xrd::ResourceDescriptorXRD = quick_xml::de::from_str(XRD_DOCUMENT)
				.expect("document to be deserialized successfully");
			
			log::info!("Serialized document: {de:#?}");
			
			let ser = quick_xml::se::to_string(&de)
				.expect("document to be serialized successfully");
			
			log::info!("Deserialized document: {ser:#?}");
		}
	};
}


test_discover_hostmeta!(test_discover_hostmeta_junimo_party, "https://junimo.party");
test_discover_hostmeta!(test_discover_hostmeta_mastodon_social, "https://mastodon.social");
test_discover_hostmeta!(test_discover_hostmeta_misskey_io, "https://misskey.io");
test_discover_hostmeta!(test_discover_hostmeta_meow_company, "https://meow.company");
test_discover_hostmeta!(test_discover_hostmeta_alpha_polymaths_social, "https://alpha.polymaths.social");
test_discover_hostmeta!(test_discover_hostmeta_fed_brid_gy, "https://fed.brid.gy");
test_discover_hostmeta!(test_discover_hostmeta_threads_net, "https://threads.net", ignore = "does not support host-meta");
test_discover_hostmeta!(test_discover_hostmeta_ngoa_giao_loan, "https://ngoa.giao.loan", ignore = "does not support host-meta");
test_discover_hostmeta!(test_discover_hostmeta_hollo_social, "https://hollo.social", ignore = "does not support host-meta");

test_de_ser_jrd!(test_de_ser_jrd_sample_junimo_party, "samples/junimo_party.nodeinfo.jrd.json");
test_de_ser_xrd!(test_de_ser_xrd_sample_junimo_party, "samples/junimo_party.host-meta.xrd.xml");