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

/**
 * R-LLM SEO F01 View
 */
function r_llm_seo_f01_render_row_inner($post): string
{
	if (!($post instanceof WP_Post)) {
		return '';
	}

	$analysis = r_llm_seo_f01_get_saved_analysis((int) $post->ID);
	$suggestions = $analysis['suggestions'] ?? [];
	$intent = (string) ($analysis['intent'] ?? '');
	$diag = (array) ($analysis['current_title'] ?? ['score' => 0, 'reason' => '']);
	$is_parse_error = !empty($analysis['is_parse_error']);
	$raw = (string) ($analysis['raw'] ?? '');

	$title_len = mb_strlen((string) $post->post_title);

	$type_obj  = get_post_type_object($post->post_type);
	$type_name = ($type_obj && isset($type_obj->labels->singular_name))
		? (string) $type_obj->labels->singular_name
		: (string) $post->post_type;

	ob_start();
	?>
	<td class="col-id"><code><?php echo (int) $post->ID; ?></code></td>

	<td class="col-type">
		<span class="type-badge">
			<?php echo esc_html($type_name); ?>
		</span>
	</td>

	<td class="col-current">
		<div class="current-title-flex">
			<span class="char-count-badge <?php echo ($title_len > 32) ? 'warn' : ''; ?>">
				<?php
				printf(
					/* translators: %d: title character count */
					esc_html__('%d文字', 'r-llm-seo'),
					(int) $title_len
				);
				?>
			</span>
			<div class="title-text"><?php echo esc_html((string) $post->post_title); ?></div>
		</div>
	</td>

	<td class="col-suggestions">
		<?php if (!empty($suggestions)): ?>
			<div class="suggestion-list">
				<?php foreach ($suggestions as $item): ?>
					<?php
					$n = (int) ($item['index'] ?? 0);
					$score = (int) ($item['score'] ?? 0);
					$cls = ($score >= 85) ? 'score-high' : (($score >= 70) ? 'score-mid' : 'score-low');
					$title = (string) ($item['title'] ?? '');
					$s_len = mb_strlen($title);
					$unique_id = 'ai-edit-' . (int) $post->ID . '-' . $n;
					$button_id = 'btn-' . $unique_id;
					?>
					<div class="ai-suggestion-card">
						<div class="ai-card-content">
							<div class="ai-card-meta">
								<span class="ai-num-badge">
									<?php
									printf(
										/* translators: %d: suggestion number */
										esc_html__('案%d', 'r-llm-seo'),
										(int) $n
									);
									?>
								</span>
								<span class="ai-score-label <?php echo esc_attr($cls); ?>">
									<?php
									printf(
										/* translators: %d: SEO score */
										esc_html__('%d点', 'r-llm-seo'),
										(int) $score
									);
									?>
								</span>
								<span class="char-count-badge sm-badge <?php echo ($s_len > 32) ? 'warn' : ''; ?>">
									<?php
									printf(
										/* translators: %d: suggestion title character count */
										esc_html__('%d文字', 'r-llm-seo'),
										(int) $s_len
									);
									?>
								</span>
							</div>

							<div class="ai-title-display">
								<textarea
									id="<?php echo esc_attr($unique_id); ?>"
									class="ai-title-textarea"
									rows="2"
									data-target-button="#<?php echo esc_attr($button_id); ?>"
								><?php echo esc_textarea($title); ?></textarea>
							</div>
						</div>

						<button
							id="<?php echo esc_attr($button_id); ?>"
							class="apply-btn"
							data-id="<?php echo (int) $post->ID; ?>"
							data-title="<?php echo esc_attr($title); ?>"
						><?php echo esc_html__('採用', 'r-llm-seo'); ?></button>
					</div>
				<?php endforeach; ?>
			</div>

		<?php elseif ($is_parse_error): ?>
			<div class="is-pending-msg parse-error-bg"><?php echo esc_html__('解析失敗。再度「分析」を選択してください。', 'r-llm-seo'); ?></div>

		<?php else: ?>
			<div class="is-pending-msg"><?php echo esc_html__('未分析', 'r-llm-seo'); ?></div>
		<?php endif; ?>
	</td>

	<td class="col-actions">
		<div class="action-stack">
			<button class="button run-single <?php echo !empty($suggestions) || $is_parse_error || $raw !== '' ? '' : 'button-primary is-pending'; ?>" data-id="<?php echo (int) $post->ID; ?>">
				<?php echo esc_html__('分析', 'r-llm-seo'); ?>
			</button>

			<?php if ($raw !== ''): ?>
				<a href="#TB_inline?width=700&height=650&inlineId=modal-<?php echo (int) $post->ID; ?>" class="thickbox button">
					<?php echo esc_html__('思考', 'r-llm-seo'); ?>
				</a>

				<button class="button delete-analysis" data-id="<?php echo (int) $post->ID; ?>">
					<?php echo esc_html__('消去', 'r-llm-seo'); ?>
				</button>

				<div id="modal-<?php echo (int) $post->ID; ?>" class="hidden-modal-content">
					<div class="modal-wrap">
						<h2 class="modal-header">
							<?php echo esc_html__('SEO分析レポート', 'r-llm-seo'); ?>
							<small class="modal-header-id">(ID: <?php echo (int) $post->ID; ?>)</small>
						</h2>

						<div class="modal-grid-top">
							<div class="modal-section modal-intent">
								<span class="modal-label"><?php echo esc_html__('意図 (Intent)', 'r-llm-seo'); ?></span>
								<strong><?php echo esc_html($intent); ?></strong>
							</div>

							<div class="modal-section modal-diag">
								<span class="modal-label">
									<?php
									printf(
										/* translators: %d: current SEO score */
										esc_html__('現在のタイトル評価 (%d点)', 'r-llm-seo'),
										(int) ($diag['score'] ?? 0)
									);
									?>
								</span>
								<?php echo nl2br(esc_html((string) ($diag['reason'] ?? ''))); ?>
							</div>
						</div>

						<div class="modal-section">
							<span class="modal-label"><?php echo esc_html__('提案詳細', 'r-llm-seo'); ?></span>

							<table class="modal-table-fixed">
								<?php foreach ($suggestions as $item): ?>
									<tr>
										<th class="modal-th-num">
											<?php
											printf(
												/* translators: %d: suggestion number */
												esc_html__('案%d', 'r-llm-seo'),
												(int) ($item['index'] ?? 0)
											);
											?>
										</th>
										<td>
											<div class="modal-suggest-title"><?php echo esc_html((string) ($item['title'] ?? '')); ?></div>
											<div class="modal-suggest-reason"><?php echo esc_html((string) ($item['reason'] ?? '')); ?></div>
										</td>
									</tr>
								<?php endforeach; ?>
							</table>
						</div>

						<div class="modal-section modal-raw">
							<span class="modal-label"><?php echo esc_html__('RAW DATA (JSON)', 'r-llm-seo'); ?></span>
							<pre class="raw-pre"><?php echo esc_html($raw); ?></pre>
						</div>
					</div>
				</div>
			<?php endif; ?>
		</div>
	</td>
	<?php

	return (string) ob_get_clean();
}