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

/**
 * R-LLM SEO F01 Page
 */
function r_llm_seo_f01_render_page(): void
{
	$paged = filter_input(INPUT_GET, 'paged', FILTER_VALIDATE_INT);
	$paged = is_int($paged) && $paged > 0 ? $paged : 1;

	$mode = filter_input(INPUT_GET, 'mode', FILTER_SANITIZE_SPECIAL_CHARS);
	$mode = is_string($mode) ? sanitize_key(wp_unslash($mode)) : 'pro';
	if (!in_array($mode, ['pro', 'keep'], true)) {
		$mode = 'pro';
	}

	$selected_post_types = get_option('r_llm_seo_post_types', ['post']);
	if (!is_array($selected_post_types)) {
		$selected_post_types = ['post'];
	}
	$selected_post_types = array_values(array_filter(array_map('sanitize_key', $selected_post_types)));

	if (empty($selected_post_types)) {
		echo '<div class="notice notice-warning"><p>' . esc_html__('設定画面で最適化対象の投稿タイプを1つ以上選択してください。', 'r-llm-seo') . '</p></div>';
		return;
	}

	$query = new WP_Query([
		'post_type'      => $selected_post_types,
		'posts_per_page' => 15,
		'paged'          => $paged,
		'orderby'        => 'ID',
		'order'          => 'DESC',
		'no_found_rows'  => false,
	]);

	$page_links = paginate_links([
		'base'      => add_query_arg([
			'paged' => '%#%',
			'mode'  => $mode,
		]),
		'format'    => '',
		'prev_text' => '&laquo;',
		'next_text' => '&raquo;',
		'total'     => (int) $query->max_num_pages,
		'current'   => $paged,
	]);
	?>
	<div class="strategy-box">
		<div class="mode-selector-wrap">
			<label for="analysis-mode" class="mode-label"><?php echo esc_html__('最適化方針', 'r-llm-seo'); ?></label>
			<select id="analysis-mode" class="mode-select">
				<option value="pro" <?php selected($mode, 'pro'); ?>><?php echo esc_html__('プロモード', 'r-llm-seo'); ?></option>
				<option value="keep" <?php selected($mode, 'keep'); ?>><?php echo esc_html__('キープモード', 'r-llm-seo'); ?></option>
			</select>
		</div>

		<small class="mode-description">
			<?php echo esc_html__('プロモード：構成や語順を根本から見直し、AIが最大限に最適化', 'r-llm-seo'); ?><br>
			<?php echo esc_html__('キープモード：元のニュアンスを活かしつつ、SEO観点でAIが最適化案を提案', 'r-llm-seo'); ?>
		</small>

		<div class="bulk-actions-wrap">
			<button id="run-bulk-analysis" class="button button-primary"><?php echo esc_html__('一括分析', 'r-llm-seo'); ?></button>
			<button id="reset-bulk-analysis" class="button delete-analysis"><?php echo esc_html__('一括消去', 'r-llm-seo'); ?></button>
		</div>

		<div class="ai-notice-wrap">
			<p class="ai-notice-text"><?php echo esc_html__('AI機能の実行時に、処理に必要なタイトル・本文などの内容がAIサーバーへ送信されます。機密情報や個人情報は入力しないでください。', 'r-llm-seo'); ?></p>
		</div>
	</div>

	<?php if ($page_links): ?>
		<div class="tablenav top">
			<div class="tablenav-pages">
				<span class="pagination-links custom-seo-pager"><?php echo wp_kses_post($page_links); ?></span>
			</div>
		</div>
	<?php endif; ?>

	<table class="wp-list-table widefat fixed striped">
		<thead>
			<tr>
				<th style="width:50px;"><?php echo esc_html__('ID', 'r-llm-seo'); ?></th>
				<th style="width:80px;"><?php echo esc_html__('種別', 'r-llm-seo'); ?></th>
				<th style="width:28%;"><?php echo esc_html__('現在のタイトル', 'r-llm-seo'); ?></th>
				<th><?php echo esc_html__('AIタイトル最適化案', 'r-llm-seo'); ?></th>
				<th style="width:90px; text-align:center;"><?php echo esc_html__('操作', 'r-llm-seo'); ?></th>
			</tr>
		</thead>
		<tbody id="seo-table-body">
			<?php foreach ((array) $query->posts as $post): ?>
				<tr id="post-row-<?php echo (int) $post->ID; ?>">
					<?php echo wp_kses_post(r_llm_seo_f01_render_row_inner($post)); ?>
				</tr>
			<?php endforeach; ?>
		</tbody>
	</table>

	<?php if ($page_links): ?>
		<div class="tablenav bottom">
			<div class="tablenav-pages">
				<span class="pagination-links custom-seo-pager"><?php echo wp_kses_post($page_links); ?></span>
			</div>
		</div>
	<?php endif; ?>
	<?php
}