<?php

if (!defined('WPINC')) {
	die;
}

// Includes
if (!class_exists('PostsCategoriesController')) {
	require_once ASWP_ATHENA_CORE_INC . 'Controller/PostsCategoriesController.php';
}
if (!class_exists('PostsController')) {
	require_once ASWP_ATHENA_CORE_INC . 'Controller/PostsController.php';
}
// END Includes

class ApiReindexController {
	// Reindex Posts Categories
	public function getPostsCategories() {
		$postsCategoriesController = new PostsCategoriesController();
		$data = $postsCategoriesController->getCategories();

		// Return success response
		return [
			'wp_post_categories' => $data ?? []
		];
	}
	// END Reindex Posts Categories

	// Reindex Posts with Pagination
	public function getPosts(int $page) {
		$postsController = new PostsController();
		$allPosts = $postsController->getPosts();

		// If no posts at all, return empty
		if (empty($allPosts)) {
			return [
				'wp_posts' => []
			];
		}

		// Pagination logic
		$per_page = 500;
		$offset = ($page - 1) * $per_page;

		// Get posts for this page
		$postsForPage = array_slice($allPosts, $offset, $per_page);

		// Return posts for this page (empty array if page exceeds data)
		return [
			'wp_posts' => $postsForPage
		];
	}
	// END Reindex Posts with Pagination
}