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

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

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


/**
 * Render About page content.
 */
function daisy_blog_render_about_page() {
	$theme = wp_get_theme();
	$pro_url   = apply_filters( 'baithak_pro_url', 'https://wproyal.com/daisy-blog' );
	$docs_url  = apply_filters( 'baithak_docs_url', 'https://wproyal.com/docs/' );
	$support_url = apply_filters( 'baithak_support_url', 'https://wordpress.org/support/theme/daisy-blog/' );
	$changelog_url = apply_filters( 'baithak_changelog_url', 'https://wordpress.org/themes/daisy-blog/' );
	$sections = [
		[
			'label' => __( 'Setup Basics', 'daisy-blog' ),
			'items' => [
				[ 'label' => __( 'Full Site Editing', 'daisy-blog' ), 'done' => true ],
				[ 'label' => __( 'Colors customization', 'daisy-blog' ), 'done' => true ],
				[ 'label' => __( 'Typography', 'daisy-blog' ), 'done' => true ],
			],
		],
		[
			'label' => __( 'Content & Pages', 'daisy-blog' ),
			'items' => [
				[ 'label' => __( 'Pages created', 'daisy-blog' ), 'done' => true ],
				[ 'label' => __( 'Blog page setup', 'daisy-blog' ), 'done' => true ],
				[ 'label' => __( 'Header & Footer', 'daisy-blog' ), 'done' => true ],
			],
		],
		[
			'label'   => __( 'Design & Polish', 'daisy-blog' ),
			'premium' => true,
			'items'   => [
				[ 'label' => __( 'Additional blocks layout', 'daisy-blog' ) ],
				[ 'label' => __( 'Dark / Light mode', 'daisy-blog' ) ],
				[ 'label' => __( 'Popular posts widget', 'daisy-blog' ) ],
			],
		],
		[
			'label'   => __( 'Growth & Monetization', 'daisy-blog' ),
			'premium' => true,
			'items'   => [
				[ 'label' => __( 'Advertisement blocks', 'daisy-blog' ) ],
				[ 'label' => __( 'Social sharing', 'daisy-blog' ) ],
				[ 'label' => __( 'Like & reactions system', 'daisy-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', 'daisy-blog' ); ?>
				</a>
			</div>
			<p><?php esc_html_e( 'Unlock premium design tools, layouts, and blocks built for professional sites.', 'daisy-blog' ); ?></p>
			<p><strong><?php esc_html_e( 'Current version:', 'daisy-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', 'daisy-blog' ); ?></a>
				<a class="button" href="<?php echo esc_url( $support_url ); ?>" target="_blank" rel="noopener"><?php esc_html_e( 'Get Support', 'daisy-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', 'daisy-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', 'daisy-blog' ); ?></a>
						</p>
					<?php endif; ?>
				</div>
			<?php endforeach; ?>
		</div>

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