<?php
defined('ABSPATH') || exit;

/**
 * R-LLM SEO Menu
 */
function r_llm_seo_features(): array
{
	$features = [];

	if (function_exists('r_llm_seo_feature_F01')) {
		$features[] = r_llm_seo_feature_F01();
	}

	if (function_exists('r_llm_seo_feature_F02')) {
		$features[] = r_llm_seo_feature_F02();
	}

	if (function_exists('r_llm_seo_feature_F03')) {
		$features[] = r_llm_seo_feature_F03();
	}

	return $features;
}

function r_llm_seo_find_feature_by_slug(string $slug): ?array
{
	foreach (r_llm_seo_features() as $f) {
		if (!empty($f['menu_slug']) && $f['menu_slug'] === $slug) {
			return $f;
		}
	}

	return null;
}

function r_llm_seo_get_current_admin_page(): string
{
	$page = filter_input(INPUT_GET, 'page', FILTER_SANITIZE_SPECIAL_CHARS);
	$page = is_string($page) ? sanitize_key(wp_unslash($page)) : '';

	return $page;
}

add_filter('admin_title', function ($admin_title, $title) {
	$screen = function_exists('get_current_screen') ? get_current_screen() : null;
	if (!$screen || strpos((string)$screen->id, 'r-llm-seo') === false) {
		return $admin_title;
	}

	$page      = r_llm_seo_get_current_admin_page();
	$blog_name = get_bloginfo('name');

	if ($page === 'r-llm-seo') {
		return sprintf(
			/* translators: %s: site name */
			esc_html__('R-LLM SEO - ダッシュボード ‹ %s ― WordPress', 'r-llm-seo'),
			$blog_name
		);
	}

	if ($page === 'r-llm-seo-settings') {
		return sprintf(
			/* translators: %s: site name */
			esc_html__('R-LLM SEO - 共通設定 ‹ %s ― WordPress', 'r-llm-seo'),
			$blog_name
		);
	}

	$f = r_llm_seo_find_feature_by_slug($page);
	if ($f) {
		return sprintf(
			/* translators: 1: feature page title, 2: site name */
			esc_html__('R-LLM SEO - %1$s ‹ %2$s ― WordPress', 'r-llm-seo'),
			(string)$f['page_title'],
			$blog_name
		);
	}

	return $admin_title;
}, 10, 2);

add_action('admin_menu', function () {
	$cap = function_exists('r_llm_seo_get_min_capability') ? r_llm_seo_get_min_capability() : 'manage_options';

	add_menu_page(
		esc_html__('R-LLM SEO', 'r-llm-seo'),
		esc_html__('R-LLM SEO', 'r-llm-seo'),
		$cap,
		'r-llm-seo',
		'r_llm_seo_router',
		'dashicons-chart-line',
		26
	);

	add_submenu_page(
		'r-llm-seo',
		esc_html__('ダッシュボード', 'r-llm-seo'),
		esc_html__('ダッシュボード', 'r-llm-seo'),
		$cap,
		'r-llm-seo',
		'r_llm_seo_router'
	);

	if (function_exists('r_llm_seo_features')) {
		foreach (r_llm_seo_features() as $f) {
			add_submenu_page(
				'r-llm-seo',
				(string)$f['page_title'],
				(string)$f['menu_title'],
				(string)$f['capability'],
				(string)$f['menu_slug'],
				'r_llm_seo_router'
			);
		}
	}

	add_submenu_page(
		'r-llm-seo',
		esc_html__('共通設定', 'r-llm-seo'),
		esc_html__('共通設定', 'r-llm-seo'),
		'manage_options',
		'r-llm-seo-settings',
		'r_llm_seo_router'
	);
});

function r_llm_seo_router(): void
{
	$page = r_llm_seo_get_current_admin_page();

	if ($page === 'r-llm-seo-settings' && !current_user_can('manage_options')) {
		wp_die(esc_html__('You do not have permission to access this page.', 'r-llm-seo'));
	}

	if ($page === 'r-llm-seo') {
		echo '<div class="wrap"><h1>' . esc_html__('R-LLM SEO', 'r-llm-seo') . ' <small style="font-size:0.5em; opacity:0.6; font-weight:normal;">- ' . esc_html__('ダッシュボード', 'r-llm-seo') . '</small></h1>';

		$file = plugin_dir_path(__FILE__) . '../pages/dashboard-page.php';
		if (is_file($file)) {
			require $file;
		} else {
			echo '<p>' . esc_html__('dashboard-page.php が見つかりません。', 'r-llm-seo') . '</p>';
		}

		echo '</div>';
		return;
	}

	if ($page === 'r-llm-seo-settings') {
		if (!current_user_can('manage_options')) {
			wp_safe_redirect(admin_url('admin.php?page=r-llm-seo'));
			exit;
		}

		echo '<div class="wrap"><h1>' . esc_html__('R-LLM SEO', 'r-llm-seo') . ' <small style="font-size:0.5em; opacity:0.6; font-weight:normal;">- ' . esc_html__('共通設定', 'r-llm-seo') . '</small></h1>';

		if (function_exists('r_llm_seo_render_settings_page')) {
			r_llm_seo_render_settings_page();
		}

		echo '</div>';
		return;
	}

	$f = r_llm_seo_find_feature_by_slug($page);
	if ($f) {
		echo '<div class="wrap"><h1>' . esc_html__('R-LLM SEO', 'r-llm-seo') . ' <small style="font-size:0.5em; opacity:0.6; font-weight:normal;">- ' . esc_html((string)$f['page_title']) . '</small></h1>';

		$render = $f['render_cb'] ?? null;
		if (is_callable($render)) {
			call_user_func($render);
		}

		echo '</div>';
		return;
	}

	wp_safe_redirect(admin_url('admin.php?page=r-llm-seo'));
	exit;
}