<?php



add_action('rest_api_init', function () {

	/* ---------------------------
		SHIPPING ROUTES
 ---------------------------*/
	// Handle exclusion parameters like http://homegrade.local/wp-json/homegrade-datas/v1/questions-thematiques/29?excluded_ids=1314,1305,1301
	register_rest_route('homegrade-datas/v1', '/questions-thematiques/(?P<thematiqueId>\d+)', array(
		'methods' => 'GET',
		'callback' => 'get_questions_posts_per_thematique_id',
		'permission_callback' => '__return_true',
		'args' => array(
			'excluded_ids' => array(
				'sanitize_callback' => 'sanitize_text_field',
			),
		),
	));
	// * SHIP ALL NEWS
	register_rest_route('homegrade-datas/v1', '/news', array(
		'methods' => 'GET',
		'callback' => 'get_news',
		'permission_callback' => '__return_true',

	));
	// * SHIP NEWS  BY TYPE
	register_rest_route('homegrade-datas/v1', '/news/type/(?P<typeId>\d+)', array(
		'methods' => 'GET',
		'callback' => 'get_news_posts_per_type_id',
		'permission_callback' => '__return_true',

	));


	/* ----------------
		BUILDING ROUTES
 	-----------------*/
	// * BUILD ALL NEWS CARDS 
	register_rest_route('homegrade-datas/v1/build', '/news', array(
		'methods' => 'GET',
		'callback' => 'build_news_posts_feed_all',
		'permission_callback' => '__return_true',

	));
	// * BUILD NEWS CARDS BY NEWS TYPE
	register_rest_route('homegrade-datas/v1/build', '/news/type/(?P<typeId>\d+)', array(
		'methods' => 'GET',
		'callback' => 'build_news_posts_feed_per_type_id',
		'permission_callback' => '__return_true',

	));
	// * BUILD BROCHURES ARCHIVE ROWS
	register_rest_route('homegrade-datas/v1/build', '/brochures-archive-rows', array(
		'methods' => 'GET',
		'callback' => 'build_brochure_archive_rows',
		'permission_callback' => '__return_true',

	));
	// * BUILD FICHE INFOS ARCHIVE ROWS
	register_rest_route('homegrade-datas/v1/build', '/fiche-info-archive-rows', array(
		'methods' => 'GET',
		'callback' => 'build_fiches_info_archive_rows',
		'permission_callback' => '__return_true',

	));
	// * BUILD WEBINAIRE ARCHIVE ROWS
	// register_rest_route('homegrade-datas/v1/build', '/webinaire-archive-rows', array(
	// 	'methods' => 'GET',
	// 	'callback' => 'build_webinaire_archive_rows',
	// 	'permission_callback' => '__return_true',

	// ));
});



function get_news($request)
{
	$args = array(
		"status" => "publish",
		"post_type" => "questions",
		"posts_per_page" => -1,
	);
	$newsPosts = get_posts($args);
	$response = new WP_REST_Response($newsPosts);
	$response->set_status(200);

	return $response;
}
function get_news_posts_per_type_id($request)
{

	$typeId = $request['typeId'];
	$args = array(
		"post_type" => "post",
		"posts_per_page" => -1,
		"tax_query" => array(
			array(
				'taxonomy' => 'news_type',
				'field' => 'term_id',
				'terms' => $typeId
			)
		)
	);
	$newsPostsDatas = get_posts($args);
	$response = new WP_REST_Response($newsPostsDatas);
	$response->set_status(200);

	return $response;
}
function get_questions_posts_per_thematique_id($request)
{
	$excluded_ids = $request->get_param('excluded_ids');
	$excludedArray = explode(',', $excluded_ids);

	$thematiqueId = $request['thematiqueId'];
	$args = array(
		"post_type" => "questions",
		"posts_per_page" => -1,
		"post__not_in" => $excludedArray,
		'tax_query' => array(
			array(
				'taxonomy' => 'thematiques',
				'field' => 'term_id',
				'terms' => $thematiqueId
			)
		)
	);
	$postDatas = get_posts($args);
	$response = new WP_REST_Response($postDatas);
	$response->set_status(200);

	return $response;
}

function build_news_posts_feed_all($request)
{


	$args = array(
		"post_type" => "post",
		"posts_per_page" => -1,
	);
	$newsPostsDatas = new WP_Query($args);


	ob_start();
	foreach ($newsPostsDatas->posts as $key => $post) {
		$post_thumbnail = get_the_post_thumbnail($post->ID, 'full', array('class' => 'card-news__thumbnail card-post__thumbnail')) ?? null;
		$news_type = get_the_terms($post->ID, "news_type") ?? null;
		// write_log($news_type);
		get_template_part(
			'template-components/cards/card-news',
			null,
			array(
				'card_variant' => 'activite',
				'post_ID' => $post->ID,
				'post_title' => get_the_title($post->ID),
				'post_thumbnail' => $post_thumbnail,
				'news_type' => $news_type,
			)
		);
	}

	$html_template = ob_get_clean();

	$response_data = array(
		'html_template' => $html_template,
		'total_posts_found' => $newsPostsDatas->found_posts,
	);

	$response = new WP_REST_Response($response_data);
	$response->set_status(200);

	return $response;
}
function build_news_posts_feed_per_type_id($request)
{
	$typeId = esc_html($request['typeId']);
	if (!$typeId || (!is_numeric($typeId))) return;

	$args = array(
		"post_type" => "post",
		"posts_per_page" => -1,

		"tax_query" => array(
			array(
				'taxonomy' => 'news_type',
				'field' => 'term_id',
				'terms' => $typeId
			)
		)
	);
	$newsPostsDatas = new WP_Query($args);


	ob_start();
	foreach ($newsPostsDatas->posts as $key => $post) {
		$post_thumbnail = get_the_post_thumbnail($post->ID, 'full', array('class' => 'card-news__thumbnail card-post__thumbnail')) ?? null;
		$news_type = get_the_terms($post->ID, "news_type") ?? null;
		get_template_part(
			'template-components/cards/card-news',
			null,
			array(
				'card_variant' => 'activite',
				'post_ID' => $post->ID,
				'post_title' => get_the_title($post->ID),
				'post_thumbnail' => $post_thumbnail,
				'news_type' => $news_type,
			)
		);
	}

	$html_template = ob_get_clean();

	$response_data = array(
		'html_template' => $html_template,
		'total_posts_found' => $newsPostsDatas->found_posts,
	);


	$response = new WP_REST_Response($response_data);

	$response->set_status(200);

	return $response;
}

function build_brochure_archive_rows()
{
	$args = array(
		'post_type' => 'brochures',
		'posts_per_page' => -1,
		'post_status' => 'publish',
		'order' => 'DESC',
		'orderby' => 'date',
		'meta_key'      => 'brochure_pdf',
		'meta_value' => '',
		'meta_compare' => '!=',
		// ICI METAQUERY FOR LE PDF
	);
	$brochuresPosts = new WP_Query($args);

	ob_start();
	get_template_part(
		'template-components/archives/brochure-rows',
		null,
		array(
			'brochuresPosts' => $brochuresPosts,
		)
	);
	$html_template = ob_get_clean();



	$response_data = array(
		'posts' => $brochuresPosts->posts,
		'html_template' => $html_template,
		'total_posts_found' => count($brochuresPosts->posts),
	);

	$response = new WP_REST_Response($response_data);
	$response->set_status(200);

	return $response;
}
function build_fiches_info_archive_rows()
{
	$args = array(
		'post_type' => 'fiches-infos',
		'posts_per_page' => -1,
		'post_status' => 'publish',
		'order' => 'DESC',
		'orderby' => 'date',
		'meta_key'      => 'brochure_pdf',
		'meta_value' => '',
		'meta_compare' => '!=',

	);
	$ficheInfoPosts = new WP_Query($args);

	ob_start();
	get_template_part(
		'template-components/archives/fiche-info-rows',
		null,
		array(
			'fichesInfosPosts' => $ficheInfoPosts,
		)
	);
	$html_template = ob_get_clean();


	$response_data = array(
		'posts' => $ficheInfoPosts->posts,
		'html_template' => $html_template,
		'total_posts_found' => count($ficheInfoPosts->posts),
	);

	$response = new WP_REST_Response($response_data);
	$response->set_status(200);

	return $response;
}
