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

/**
 * R-LLM SEO Dashboard Page
 */
$r_llm_seo_api_key = (string)get_option(R_LLM_SEO_OPT_API_KEY, '');
$r_llm_seo_has_key = (trim($r_llm_seo_api_key) !== '');

$r_llm_seo_lp_url       = apply_filters('r_llm_seo_url_lp', 'https://r-llm.com/seo/?utm_source=wp-admin&utm_medium=referral&utm_campaign=intro');
$r_llm_seo_mypage_url   = apply_filters('r_llm_seo_url_mypage', 'https://r-llm.com/mypage/?utm_source=wp-admin&utm_medium=referral&utm_campaign=intro');
$r_llm_seo_register_url = apply_filters('r_llm_seo_url_register', 'https://r-llm.com/register/?utm_source=wp-admin&utm_medium=referral&utm_campaign=intro');
$r_llm_seo_docs_url     = apply_filters('r_llm_seo_url_docs', 'https://r-llm.com/docs/seo/?utm_source=wp-admin&utm_medium=referral&utm_campaign=intro');
$r_llm_seo_pricing_url  = apply_filters('r_llm_seo_url_pricing', 'https://r-llm.com/pricing/?utm_source=wp-admin&utm_medium=referral&utm_campaign=intro');
$r_llm_seo_contact_url  = apply_filters('r_llm_seo_url_contact', 'https://r-llm.com/contact/?utm_source=wp-admin&utm_medium=referral&utm_campaign=intro');

$r_llm_seo_settings_url = admin_url('admin.php?page=r-llm-seo-settings');

$r_llm_seo_site_url_fixed = rtrim((string)site_url(), '/');

$r_llm_seo_status_data = null;
if (function_exists('r_llm_seo_license_guard_get_status')) {
	$r_llm_seo_status_data = r_llm_seo_license_guard_get_status();
}

$r_llm_seo_status_label = esc_html__('System Status: Workspace ready', 'r-llm-seo');
$r_llm_seo_status_kind  = 'ok';

if (is_array($r_llm_seo_status_data)) {
	$r_llm_seo_status_ok      = !empty($r_llm_seo_status_data['ok']);
	$r_llm_seo_status_name    = (string)($r_llm_seo_status_data['status'] ?? '');
	$r_llm_seo_status_plan    = (string)($r_llm_seo_status_data['plan'] ?? '');
	$r_llm_seo_status_message = (string)($r_llm_seo_status_data['message'] ?? '');

	if ($r_llm_seo_status_ok) {
		$r_llm_seo_status_kind  = 'ok';
		$r_llm_seo_status_label = ($r_llm_seo_status_name === 'free_trial')
			? esc_html__('System Status: Trial active', 'r-llm-seo')
			: esc_html__('System Status: Active', 'r-llm-seo');

		if ($r_llm_seo_status_plan !== '') {
			$r_llm_seo_status_label .= ' (' . strtoupper($r_llm_seo_status_plan) . ')';
		}

		if ($r_llm_seo_status_message !== '') {
			$r_llm_seo_status_label .= ' - ' . $r_llm_seo_status_message;
		}
	} else {
		$r_llm_seo_status_kind  = 'warn';
		$r_llm_seo_status_label = esc_html__('System Status: Needs setup', 'r-llm-seo');

		if ($r_llm_seo_status_message !== '') {
			$r_llm_seo_status_label .= ' - ' . $r_llm_seo_status_message;
		}
	}
} else {
	if ($r_llm_seo_has_key) {
		$r_llm_seo_status_kind  = 'ok';
		$r_llm_seo_status_label = esc_html__('System Status: API key set', 'r-llm-seo');
	} else {
		$r_llm_seo_status_kind  = 'warn';
		$r_llm_seo_status_label = esc_html__('System Status: API key not set', 'r-llm-seo');
	}
}

$r_llm_seo_pill_class = 'rllm-status-ok';
if ($r_llm_seo_status_kind === 'warn') {
	$r_llm_seo_pill_class = 'rllm-status-warn';
}
if ($r_llm_seo_status_kind === 'ng') {
	$r_llm_seo_pill_class = 'rllm-status-ng';
}

$r_llm_seo_rel_blank = 'noopener noreferrer';
?>
<div id="r-llm-seo-admin" class="wrap">
	<div class="rllm-page">
		<div class="rllm-container">

			<header class="rllm-header">
				<div class="rllm-brand">
					<div class="rllm-logo">R</div>
					<div class="rllm-brandtext">
						<h1 class="rllm-title">R-LLM <span>SEO</span></h1>
						<p class="rllm-subtitle"><?php echo esc_html__('AI SEO Optimization Workspace', 'r-llm-seo'); ?></p>
					</div>
				</div>

				<div class="rllm-badgewrap">
					<div class="rllm-footer-status">
						<div class="rllm-status-pill <?php echo esc_attr($r_llm_seo_pill_class); ?>">
							<span class="rllm-dot"></span>
							<span><?php echo esc_html($r_llm_seo_status_label); ?></span>
						</div>
					</div>
				</div>
			</header>

			<div class="rllm-card rllm-hero">
				<div class="rllm-hero-inner">
					<h2 class="rllm-hero-title">
						<?php echo esc_html__('SEOの「迷い」をなくす。', 'r-llm-seo'); ?><br>
						<span class="gradient-text"><?php echo esc_html__('AIで、最短ルートへ。', 'r-llm-seo'); ?></span>
					</h2>

					<p class="rllm-hero-lead">
						<?php echo wp_kses_post(__('R-LLM SEO は、投稿のタイトル／ディスクリプションを<strong>AI</strong>が提案し、', 'r-llm-seo')); ?><br>
						<?php echo wp_kses_post(__('<strong>管理画面だけで</strong>更新まで完結できるプラグインです。', 'r-llm-seo')); ?><br class="rllm-only-md">
						<?php echo wp_kses_post(__('まずは<strong>7日間無料</strong>で APIキーを発行して、体感してください。', 'r-llm-seo')); ?>
					</p>

					<div class="rllm-safe">
						<div class="rllm-safe-icon">✓</div>
						<div>
							<p class="rllm-safe-title"><?php echo esc_html__('安心してお試しください', 'r-llm-seo'); ?></p>
							<ul class="rllm-safe-list">
								<li><?php echo esc_html__('クレジットカード登録は不要です', 'r-llm-seo'); ?></li>
								<li><?php echo esc_html__('無料期間終了後も自動契約・自動課金は行われません', 'r-llm-seo'); ?></li>
								<li><?php echo esc_html__('継続利用は、プラン選択後のみ有効になります', 'r-llm-seo'); ?></li>
							</ul>
						</div>
					</div>

					<div class="rllm-links">
						<a href="<?php echo esc_url($r_llm_seo_pricing_url); ?>" target="_blank" rel="<?php echo esc_attr($r_llm_seo_rel_blank); ?>"><?php echo esc_html__('料金プラン', 'r-llm-seo'); ?></a>
						<a href="<?php echo esc_url($r_llm_seo_lp_url); ?>" target="_blank" rel="<?php echo esc_attr($r_llm_seo_rel_blank); ?>"><?php echo esc_html__('プロダクトページ', 'r-llm-seo'); ?></a>
					</div>
				</div>
			</div>

			<div class="rllm-card rllm-steps">
				<div class="rllm-steps-head">
					<h3 class="rllm-steps-title"><?php echo esc_html__('3ステップで簡単スタート', 'r-llm-seo'); ?></h3>
					<span class="rllm-chip badge"><?php echo esc_html__('TRIAL: 7 DAYS FREE', 'r-llm-seo'); ?></span>
				</div>

				<div class="rllm-steps-grid">

					<div class="rllm-step">
						<div class="rllm-step-head">
							<span class="rllm-step-no">1</span>
							<p class="rllm-step-ttl"><?php echo esc_html__('サイトURL（固定）をコピー', 'r-llm-seo'); ?></p>
						</div>

						<p class="rllm-step-desc">
							<?php echo wp_kses_post(__('APIキー発行時に<strong>サイトURL（固定）の登録</strong>が必要です。下記URLをコピーしてください。', 'r-llm-seo')); ?>
						</p>

						<div class="rllm-urlbox">
							<p class="rllm-urlbox-label"><?php echo esc_html__('サイトURL（固定）', 'r-llm-seo'); ?></p>
							<p id="rllm-site-url" class="rllm-urlbox-value"><?php echo esc_html($r_llm_seo_site_url_fixed); ?></p>
						</div>

						<div class="rllm-copy-row">
							<button type="button" class="rllm-copy-btn" data-copy="#rllm-site-url">
								<?php echo esc_html__('コピー', 'r-llm-seo'); ?>
								<svg class="rllm-ico" fill="none" stroke="currentColor" viewBox="0 0 24 24">
									<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M8 16h8M8 12h8m-6 8h6a2 2 0 0 0 2-2V8l-6-6H8a2 2 0 0 0-2 2v4" />
								</svg>
							</button>
						</div>
					</div>

					<div class="rllm-step">
						<div class="rllm-step-head">
							<span class="rllm-step-no">2</span>
							<p class="rllm-step-ttl"><?php echo esc_html__('マイページでAPIキーを発行', 'r-llm-seo'); ?></p>
						</div>

						<p class="rllm-step-desc">
							<?php echo esc_html__('ユーザー登録後、Step1でコピーしたサイトURL（固定）を登録してAPIキーを発行します。', 'r-llm-seo'); ?>
						</p>

						<div class="rllm-step-links">
							<a href="<?php echo esc_url($r_llm_seo_mypage_url); ?>" target="_blank" rel="<?php echo esc_attr($r_llm_seo_rel_blank); ?>" class="rllm-link">
								<?php echo esc_html__('マイページを開く', 'r-llm-seo'); ?>
								<svg class="rllm-ico" fill="none" stroke="currentColor" viewBox="0 0 24 24">
									<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M14 5l7 7m0 0l-7 7m7-7H3"></path>
								</svg>
							</a>

							<div class="rllm-register">
								<a href="<?php echo esc_url($r_llm_seo_register_url); ?>" target="_blank" rel="<?php echo esc_attr($r_llm_seo_rel_blank); ?>" class="rllm-link-sub">
									<?php echo esc_html__('ユーザー登録がまだの方はこちら', 'r-llm-seo'); ?>
								</a>
							</div>
						</div>
					</div>

					<div class="rllm-step">
						<div class="rllm-step-head">
							<span class="rllm-step-no">3</span>
							<p class="rllm-step-ttl"><?php echo esc_html__('共通設定にペーストして開始', 'r-llm-seo'); ?></p>
						</div>

						<p class="rllm-step-desc">
							<?php echo wp_kses_post(__('発行したAPIキーを<strong>共通設定</strong>に貼り付けて保存します。保存後、機能メニューが有効になります。', 'r-llm-seo')); ?>
						</p>

						<div class="rllm-step-links">
							<a href="<?php echo esc_url($r_llm_seo_settings_url); ?>" class="rllm-link">
								<?php echo esc_html__('共通設定を開く', 'r-llm-seo'); ?>
								<svg class="rllm-ico" fill="none" stroke="currentColor" viewBox="0 0 24 24">
									<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M14 5l7 7m0 0l-7 7m7-7H3"></path>
								</svg>
							</a>
						</div>
					</div>

				</div>
			</div>

			<div class="rllm-card rllm-cta">
				<div class="rllm-cta-row">
					<?php if (!$r_llm_seo_has_key): ?>
						<a href="<?php echo esc_url($r_llm_seo_mypage_url); ?>" target="_blank" rel="<?php echo esc_attr($r_llm_seo_rel_blank); ?>" class="rllm-btn rllm-btn-primary">
							<span><?php echo esc_html__('APIキーを発行（7日間無料）', 'r-llm-seo'); ?></span>
							<svg class="rllm-ico-lg" fill="none" stroke="currentColor" viewBox="0 0 24 24">
								<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M14 5l7 7m0 0l-7 7m7-7H3"></path>
							</svg>
						</a>
						<a href="<?php echo esc_url($r_llm_seo_settings_url); ?>" class="rllm-btn rllm-btn-secondary">
							<span><?php echo esc_html__('共通設定を開く', 'r-llm-seo'); ?></span>
							<svg class="rllm-ico-lg" fill="none" stroke="currentColor" viewBox="0 0 24 24">
								<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M14 5l7 7m0 0l-7 7m7-7H3"></path>
							</svg>
						</a>
					<?php else: ?>
						<a href="<?php echo esc_url($r_llm_seo_settings_url); ?>" class="rllm-btn rllm-btn-primary">
							<span><?php echo esc_html__('共通設定へ（最適化を開始）', 'r-llm-seo'); ?></span>
							<svg class="rllm-ico-lg" fill="none" stroke="currentColor" viewBox="0 0 24 24">
								<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M14 5l7 7m0 0l-7 7m7-7H3"></path>
							</svg>
						</a>
						<a href="<?php echo esc_url($r_llm_seo_mypage_url); ?>" target="_blank" rel="<?php echo esc_attr($r_llm_seo_rel_blank); ?>" class="rllm-btn rllm-btn-secondary">
							<span><?php echo esc_html__('APIキー管理（再発行）', 'r-llm-seo'); ?></span>
							<svg class="rllm-ico-lg" fill="none" stroke="currentColor" viewBox="0 0 24 24">
								<path stroke-linecap="round" stroke-linejoin="round" stroke-width="2.5" d="M14 5l7 7m0 0l-7 7m7-7H3"></path>
							</svg>
						</a>
					<?php endif; ?>
				</div>
			</div>

			<div class="rllm-footer">
				<div class="rllm-footer-status">
					<div class="rllm-status-pill <?php echo esc_attr($r_llm_seo_pill_class); ?>">
						<span class="rllm-dot"></span>
						<span><?php echo esc_html($r_llm_seo_status_label); ?></span>
					</div>
				</div>

				<div class="rllm-footer-links">
					<a href="<?php echo esc_url($r_llm_seo_pricing_url); ?>" target="_blank" rel="<?php echo esc_attr($r_llm_seo_rel_blank); ?>"><?php echo esc_html__('料金プラン', 'r-llm-seo'); ?></a>
					<span>/</span>
					<a href="<?php echo esc_url($r_llm_seo_lp_url); ?>" target="_blank" rel="<?php echo esc_attr($r_llm_seo_rel_blank); ?>"><?php echo esc_html__('プロダクトページ', 'r-llm-seo'); ?></a>
					<span>/</span>
					<a href="<?php echo esc_url($r_llm_seo_contact_url); ?>" target="_blank" rel="<?php echo esc_attr($r_llm_seo_rel_blank); ?>"><?php echo esc_html__('お問い合わせ・フィードバック', 'r-llm-seo'); ?></a>
				</div>

				<p class="rllm-footer-copy">© <?php echo esc_html(gmdate('Y')); ?> R-LLM</p>
			</div>

		</div>
	</div>
</div>