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

/**
 * R-LLM SEO F02 View
 */
function r_llm_seo_f02_render_row(array $item, string $post_type = 'post'): string
{
	$id         = (string)($item['id'] ?? '');
	$title      = (string)($item['title'] ?? '');
	$intent     = trim((string)($item['intent'] ?? ''));
	$score      = (int)($item['score'] ?? 0);
	$reason     = trim((string)($item['reason'] ?? ''));
	$is_fav     = !empty($item['is_fav']);
	$is_created = !empty($item['is_created']);
	$post_id    = (int)($item['post_id'] ?? 0);
	$created_at = trim((string)($item['created_at'] ?? ''));

	if ($id === '') return '';

	$title_len = function_exists('mb_strlen') ? mb_strlen($title, 'UTF-8') : strlen($title);

	$badge_text = $is_created
		? esc_html__('作成済み', 'r-llm-seo')
		: ($is_fav ? esc_html__('お気に入り', 'r-llm-seo') : esc_html__('未作成', 'r-llm-seo'));

	$badge_cls = $is_created ? 'is-created' : ($is_fav ? 'is-fav' : 'is-pending');

	$score_cls =
		($score >= 85) ? 'is-high' :
		(($score >= 70) ? 'is-mid' : 'is-low');

	$edit_url = $post_id > 0 ? admin_url('post.php?post=' . $post_id . '&action=edit') : '';
	$fav_text = $is_fav ? esc_html__('お気に入り解除', 'r-llm-seo') : esc_html__('お気に入り', 'r-llm-seo');

	$warn_cls = $title_len > 32 ? 'warn' : '';
	$reason_text = ($reason !== '') ? $reason : esc_html__('—', 'r-llm-seo');

	ob_start();
	?>
	<tr data-id="<?php echo esc_attr($id); ?>">
		<td class="f02-col-state">
			<span class="f02-status-badge <?php echo esc_attr($badge_cls); ?>">
				<?php echo esc_html($badge_text); ?>
			</span>
		</td>

		<td class="f02-col-title">
			<div class="f02-title-flex">
				<span class="f02-char-badge <?php echo esc_attr($warn_cls); ?>">
					<?php
					printf(
						/* translators: %d: title character count */
						esc_html__('%d文字', 'r-llm-seo'),
						(int)$title_len
					);
					?>
				</span>
				<textarea class="f02-title" rows="2"><?php echo esc_textarea($title); ?></textarea>
			</div>
		</td>

		<td class="f02-col-meta">
			<div class="f02-metaCard">

				<div class="f02-metaTop">
					<?php if ($intent !== ''): ?>
						<span class="f02-chip f02-chip-intent">
							<?php echo esc_html__('意図：', 'r-llm-seo') . esc_html($intent); ?>
						</span>
					<?php endif; ?>

					<?php if ($score > 0): ?>
						<span class="f02-chip f02-chip-score <?php echo esc_attr($score_cls); ?>">
							<?php
							printf(
								/* translators: %d: SEO score */
								esc_html__('評価：%d点', 'r-llm-seo'),
								(int)$score
							);
							?>
						</span>
					<?php endif; ?>

					<?php if ($post_id > 0): ?>
						<span class="f02-chip f02-chip-draft">
							<?php
							printf(
								/* translators: %d: draft post ID */
								esc_html__('下書きID：%d', 'r-llm-seo'),
								(int)$post_id
							);
							?>
						</span>
					<?php endif; ?>
				</div>

				<div class="f02-metaReason <?php echo esc_attr($reason !== '' ? '' : 'is-empty'); ?>">
					<?php echo esc_html($reason_text); ?>
				</div>

				<div class="f02-metaBottom">
					<span class="f02-metaDate">
						<?php
						echo esc_html__('生成日：', 'r-llm-seo');
						echo $created_at !== '' ? esc_html($created_at) : esc_html__('-', 'r-llm-seo');
						?>
					</span>

					<?php if ($edit_url): ?>
						<a class="f02-editlink" href="<?php echo esc_url($edit_url); ?>" target="_blank" rel="noopener">
							<?php echo esc_html__('編集画面を開く', 'r-llm-seo'); ?>
						</a>
					<?php endif; ?>
				</div>

			</div>
		</td>

		<td class="f02-col-actions">
			<div class="f02-action-row">
				<?php if (!$is_created): ?>
					<button type="button" class="button f02-draft"
						data-id="<?php echo esc_attr($id); ?>"
						data-post-type="<?php echo esc_attr($post_type ?: 'post'); ?>">
						<?php echo esc_html__('下書き作成', 'r-llm-seo'); ?>
					</button>
				<?php elseif ($edit_url): ?>
					<a class="button button-primary"
						href="<?php echo esc_url($edit_url); ?>"
						target="_blank"
						rel="noopener">
						<?php echo esc_html__('編集', 'r-llm-seo'); ?>
					</a>
				<?php endif; ?>

				<button type="button" class="button f02-fav"
					data-id="<?php echo esc_attr($id); ?>"
					data-is-fav="<?php echo $is_fav ? '1' : '0'; ?>">
					<?php echo esc_html($fav_text); ?>
				</button>

				<button type="button" class="button f02-del" data-id="<?php echo esc_attr($id); ?>">
					<?php echo esc_html__('削除', 'r-llm-seo'); ?>
				</button>
			</div>
		</td>
	</tr>
	<?php
	return (string)ob_get_clean();
}