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

/**
 * Add a non-intrusive About/Upgrade page under Appearance.
 */
function pagoda_press_register_about_page() {
	add_theme_page(
		__( 'Upgrade to Pro', 'pagoda-press' ),
		__( 'Upgrade to Pro', 'pagoda-press' ),
		'manage_options',
		'pagoda-press-about',
		'pagoda_press_render_about_page'
	);
}
add_action( 'admin_menu', 'pagoda_press_register_about_page' );

/**
 * Load lightweight styles only on the About page.
 */
function pagoda_press_about_page_assets( $hook ) {
	if ( 'appearance_page_pagoda-press-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( 'pagoda-press-about', false );
	wp_enqueue_style( 'pagoda-press-about' );
	wp_add_inline_style( 'pagoda-press-about', $css );
}
add_action( 'admin_enqueue_scripts', 'pagoda_press_about_page_assets' );


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

	$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', 'pagoda-press' ); ?>
				</a>
			</div>
			<p><?php esc_html_e( 'Unlock premium design tools, layouts, and blocks built for professional sites.', 'pagoda-press' ); ?></p>
			<p><strong><?php esc_html_e( 'Current version:', 'pagoda-press' ); ?></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', 'pagoda-press' ); ?></a>
				<a class="button" href="<?php echo esc_url( $support_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Get Support', 'pagoda-press' ); ?></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', 'pagoda-press' ); ?></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', 'pagoda-press' ); ?></a>
						</p>
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
		</div>

		<div class="wproyal-columns">
				<h2><?php esc_html_e( 'Free vs Pro', 'pagoda-press' ); ?></h2>
				<table class="wproyal-pricing">
					<thead>
						<tr>
							<th><?php esc_html_e( 'Feature', 'pagoda-press' ); ?></th>
							<th><?php esc_html_e( 'Free', 'pagoda-press' ); ?></th>
							<th><?php esc_html_e( 'Pro', 'pagoda-press' ); ?></th>
						</tr>
					</thead>
					<tbody>
						<tr>
							<td><?php esc_html_e( 'Full Site Editing', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Colors customization', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Typography', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Pages created', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Blog page setup', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Header & Footer', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Additional blocks layout', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Dark / Light mode', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Popular posts widget', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Advertisement blocks', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Social sharing', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Like & reactions system', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Translation Ready', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Forum Support', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Edit Footer Copyright Text', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></td>
						</tr>
						<tr>
							<td><?php esc_html_e( 'Priority Support', 'pagoda-press' ); ?></td>
							<td class="is-free"><?php esc_html_e( '—', 'pagoda-press' ); ?></td>
							<td class="is-pro"><?php esc_html_e( 'Included', 'pagoda-press' ); ?></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', 'pagoda-press' ); ?>
								</a>
							</td>
						</tr>
					</tbody>
				</table>
		</div>
	</div>
	<?php
}
