<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * Add a non-intrusive About/Upgrade page under Appearance.
 */
function glossy_blog_register_about_page() {
	add_theme_page(
		__( 'Upgrade to Pro', 'glossy-blog' ),
		__( 'Upgrade to Pro', 'glossy-blog' ),
		'manage_options',
		'glossy-blog-about',
		'glossy_blog_render_about_page'
	);
}
add_action( 'admin_menu', 'glossy_blog_register_about_page' );

/**
 * Load lightweight styles only on the About page.
 */
function glossy_blog_about_page_assets( $hook ) {
	if ( 'appearance_page_glossy-blog-about' !== $hook ) {
		return;
	}

	$css = '
		.wproyal-hero{margin-bottom:20px;padding:16px 18px;background:#fff;border:1px solid #e5e7eb;border-radius:12px}
		.wproyal-hero-header{display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:6px}
		.wproyal-hero h1{margin:0;font-size:22px}
		.wproyal-hero p{margin:0;color:#50575e}
		.wproyal-hero-actions{margin-top:10px;display:flex;gap:8px;flex-wrap:wrap}
		.wproyal-hero .wproyal-hero-cta{font-size:14px;font-weight:600;padding:6px 16px;display:flex;align-items:center}
		.wproyal-hero .wproyal-hero-cta .dashicons{margin-right:6px}
		.wproyal-steps-grid{display:grid;grid-template-columns:repeat(auto-fit,minmax(220px,1fr));gap:16px;margin:12px 0 24px}
		.wproyal-step-card{border:1px solid #e5e7eb;border-radius:12px;padding:14px;background:#fff;box-shadow:0 1px 0 rgba(0,0,0,.02)}
		.wproyal-step-title{display:flex;gap:8px;align-items:center;margin:0 0 10px;font-size:14px}
		.wproyal-step-badge{font-size:11px;padding:2px 8px;border-radius:999px;background:#f0f0f1;color:#50575e}
		.wproyal-step-list{margin:0;padding-left:0;list-style:none}
		.wproyal-step-item{display:flex;gap:8px;align-items:center;margin:6px 0;color:#3c434a}
		.wproyal-step-item.is-done{color:#1a7f37}
		.wproyal-step-icon{width:16px;height:16px;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;border:1px solid #c3c4c7;font-size:11px;line-height:1}
		.wproyal-step-item.is-done .wproyal-step-icon{background:#1a7f37;border-color:#1a7f37;color:#fff}
		.wproyal-unlock{display:inline-flex;align-items:center;gap:6px;margin-top:8px;font-weight:600;color:#2271b1}
		.wproyal-unlock-icon{width:18px;height:18px;border-radius:50%;display:inline-flex;align-items:center;justify-content:center;background:#2271b1;color:#fff;font-size:12px;line-height:1}
		.wproyal-columns{margin-top:16px}
		.wproyal-pricing{width:100%;border-collapse:separate;border-spacing:0;background:#fff;border:1px solid #e5e7eb;border-radius:12px;overflow:hidden}
		.wproyal-pricing th,.wproyal-pricing td{padding:12px 14px;border-bottom:1px solid #e5e7eb;text-align:left}
		.wproyal-pricing thead th{background:#f6f7f7;font-weight:600;color:#1d2327}
		.wproyal-pricing tbody tr:last-child td{border-bottom:none}
		.wproyal-pricing tbody tr:hover td{background:#f9fafb}
		.wproyal-pricing .is-pro{color:#1a7f37;font-weight:600}
		.wproyal-pricing .is-free{color:#50575e}
	';
	wp_register_style( 'glossy-blog-about', false );
	wp_enqueue_style( 'glossy-blog-about' );
	wp_add_inline_style( 'glossy-blog-about', $css );
}
add_action( 'admin_enqueue_scripts', 'glossy_blog_about_page_assets' );


/**
 * Render About page content.
 */
function glossy_blog_render_about_page() {
	$theme = wp_get_theme();
	$pro_url   = apply_filters( 'baithak_pro_url', 'https://wproyal.com/glossy-blog' );
	$docs_url  = apply_filters( 'baithak_docs_url', 'https://wproyal.com/docs/' );
	$support_url = apply_filters( 'baithak_support_url', 'https://wordpress.org/support/theme/glossy-blog/' );
	$changelog_url = apply_filters( 'baithak_changelog_url', 'https://wordpress.org/themes/glossy-blog/' );
	$sections = [
		[
			'label' => __( 'Setup Basics', 'glossy-blog' ),
			'items' => [
				[ 'label' => __( 'Full Site Editing', 'glossy-blog' ), 'done' => true ],
				[ 'label' => __( 'Colors customization', 'glossy-blog' ), 'done' => true ],
				[ 'label' => __( 'Typography', 'glossy-blog' ), 'done' => true ],
			],
		],
		[
			'label' => __( 'Content & Pages', 'glossy-blog' ),
			'items' => [
				[ 'label' => __( 'Pages created', 'glossy-blog' ), 'done' => true ],
				[ 'label' => __( 'Blog page setup', 'glossy-blog' ), 'done' => true ],
				[ 'label' => __( 'Header & Footer', 'glossy-blog' ), 'done' => true ],
			],
		],
		[
			'label'   => __( 'Design & Polish', 'glossy-blog' ),
			'premium' => true,
			'items'   => [
				[ 'label' => __( 'Additional blocks layout', 'glossy-blog' ) ],
				[ 'label' => __( 'Dark / Light mode', 'glossy-blog' ) ],
				[ 'label' => __( 'Popular posts widget', 'glossy-blog' ) ],
			],
		],
		[
			'label'   => __( 'Growth & Monetization', 'glossy-blog' ),
			'premium' => true,
			'items'   => [
				[ 'label' => __( 'Advertisement blocks', 'glossy-blog' ) ],
				[ 'label' => __( 'Social sharing', 'glossy-blog' ) ],
				[ 'label' => __( 'Like & reactions system', 'glossy-blog' ) ],
			],
		],
	];

	$total = $done = 0;
	foreach ( $sections as $section ) {
		if ( ! empty( $section['premium'] ) ) {
			continue;
		}
		foreach ( $section['items'] as $item ) {
			$total++;
			if ( ! empty( $item['done'] ) ) {
				$done++;
			}
		}
	}
	$progress = $total ? round( ( $done / $total ) * 100 ) : 0;
	?>

	<div class="wrap wproyal-about-wrap">
		<div class="wproyal-hero">
			<div class="wproyal-hero-header">
				<h1><?php echo esc_html( $theme->get( 'Name' ) ); ?></h1>
				<a class="button button-primary wproyal-hero-cta" href="<?php echo esc_url( $pro_url ); ?>" target="_blank" rel="noopener">
					<span class="dashicons dashicons-star-filled" aria-hidden="true"></span>
					<?php esc_html_e( 'Upgrade to Pro', 'glossy-blog' ); ?>
				</a>
			</div>
			<p><?php esc_html_e( 'Unlock premium design tools, layouts, and blocks built for professional sites.', 'glossy-blog' ); ?></p>
			<p><strong><?php esc_html_e( 'Current version:', 'glossy-blog' ); ?></strong> <?php echo esc_html( $theme->get( 'Version' ) ); ?></p>
			<div class="wproyal-hero-actions">
				<a class="button" href="<?php echo esc_url( $docs_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'View Docs', 'glossy-blog' ); ?></a>
				<a class="button" href="<?php echo esc_url( $support_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Get Support', 'glossy-blog' ); ?></a>
			</div>
		</div>

		<div class="wproyal-steps-grid">
			<?php foreach ( $sections as $section ) : ?>
				<div class="wproyal-step-card<?php echo ! empty( $section['premium'] ) ? ' is-premium' : ''; ?>">
					<h3 class="wproyal-step-title">
						<?php echo esc_html( $section['label'] ); ?>
						<?php if ( ! empty( $section['premium'] ) ) : ?>
							<span class="wproyal-step-badge"><?php esc_html_e( 'Premium', 'glossy-blog' ); ?></span>
						<?php endif; ?>
					</h3>
					<ul class="wproyal-step-list">
						<?php foreach ( $section['items'] as $item ) : ?>
							<?php $done_class = ! empty( $item['done'] ) ? ' is-done' : ''; ?>
							<li class="wproyal-step-item<?php echo esc_attr( $done_class ); ?>">
								<span class="wproyal-step-icon"><?php echo ! empty( $item['done'] ) ? '✓' : '○'; ?></span>
								<?php echo esc_html( $item['label'] ); ?>
							</li>
						<?php endforeach; ?>
					</ul>
					<?php if ( ! empty( $section['premium'] ) ) : ?>
						<p class="wproyal-unlock">
							<span class="wproyal-unlock-icon">★</span>
							<a href="<?php echo esc_url( $pro_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Unlock with Pro', 'glossy-blog' ); ?></a>
						</p>
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
		</div>

		<div class="wproyal-columns">
				<h2><?php esc_html_e( 'Free vs Pro', 'glossy-blog' ); ?></h2>
				<table class="wproyal-pricing">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Feature', 'glossy-blog' ); ?></th>
							<th><?php esc_html_e( 'Free', 'glossy-blog' ); ?></th>
							<th><?php esc_html_e( 'Pro', 'glossy-blog' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td><?php esc_html_e( 'Full Site Editing', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Colors customization', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Typography', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Pages created', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Blog page setup', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Header & Footer', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Additional blocks layout', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Dark / Light mode', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Popular posts widget', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Advertisement blocks', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Social sharing', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Like & reactions system', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Translation Ready', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Forum Support', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Edit Footer Copyright Text', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Priority Support', 'glossy-blog' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'glossy-blog' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'glossy-blog' ); ?></td>
						</tr>
						<tr>
							<td></td>
							<td></td>
							<td class="is-pro">
								<a class="button button-primary" href="<?php echo esc_url( $pro_url ); ?>" target="_blank" rel="noopener">
									<?php esc_html_e( 'Get Pro', 'glossy-blog' ); ?>
								</a>
							</td>
						</tr>
					</tbody>
				</table>
		</div>
	</div>
	<?php
}
