<?php
/**
 * Pre-built demo site kits for agencies and sales demos.
 *
 * @package EasySite_Pro
 */

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

/**
 * List bundled demo kits.
 *
 * @return array
 */
function easysite_get_demo_kits() {
	$dir  = EASYSITE_PRO_DIR . '/demo-kits';
	$kits = array();
	if ( ! is_dir( $dir ) ) {
		return $kits;
	}
	foreach ( glob( $dir . '/*.json' ) as $file ) {
		$raw = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
		$data = json_decode( $raw, true );
		if ( ! is_array( $data ) || empty( $data['layout'] ) ) {
			continue;
		}
		$slug = basename( $file, '.json' );
		$kits[ $slug ] = array(
			'label'       => $data['label'] ?? ucfirst( str_replace( '-', ' ', $slug ) ),
			'description' => $data['description'] ?? '',
			'file'        => $file,
		);
	}
	return $kits;
}

/**
 * Import a bundled demo kit by slug.
 */
function easysite_import_demo_kit_handler() {
	if ( ! isset( $_GET['easysite_import_demo'] ) || ! current_user_can( 'manage_options' ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended
		return;
	}
	if ( ! isset( $_GET['_wpnonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_GET['_wpnonce'] ) ), 'easysite_import_demo' ) ) {
		wp_die( esc_html__( 'Security check failed.', 'easysite-pro' ) );
	}
	$slug = sanitize_key( wp_unslash( $_GET['easysite_import_demo'] ) );
	$kits = easysite_get_demo_kits();
	if ( ! isset( $kits[ $slug ] ) ) {
		wp_safe_redirect( admin_url( 'admin.php?page=easysite-site-kit&error=1' ) );
		exit;
	}
	$raw = file_get_contents( $kits[ $slug ]['file'] ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
	$kit = json_decode( $raw, true );
	$result = easysite_import_site_kit( is_array( $kit ) ? $kit : array() );
	$url = $result === true ? 'imported=1' : 'error=1';
	wp_safe_redirect( admin_url( 'admin.php?page=easysite-site-kit&' . $url ) );
	exit;
}
add_action( 'admin_init', 'easysite_import_demo_kit_handler' );

/**
 * Render demo kit import buttons on Site Kit page.
 */
function easysite_render_demo_kits_section() {
	$kits = easysite_get_demo_kits();
	if ( empty( $kits ) ) {
		return;
	}
	?>
	<h2><?php esc_html_e( 'Demo site kits', 'easysite-pro' ); ?></h2>
	<p><?php esc_html_e( 'One-click starter layouts for common industries. Imports homepage layout and brand colors.', 'easysite-pro' ); ?></p>
	<div class="easysite-demo-kits">
		<?php foreach ( $kits as $slug => $kit ) : ?>
			<div class="easysite-demo-kit-card">
				<h3><?php echo esc_html( $kit['label'] ); ?></h3>
				<?php if ( $kit['description'] ) : ?>
					<p><?php echo esc_html( $kit['description'] ); ?></p>
				<?php endif; ?>
				<a class="button" href="<?php echo esc_url( wp_nonce_url( admin_url( 'admin.php?easysite_import_demo=' . $slug ), 'easysite_import_demo' ) ); ?>" onclick="return confirm('<?php esc_attr_e( 'Replace your homepage layout with this demo?', 'easysite-pro' ); ?>');"><?php esc_html_e( 'Import demo', 'easysite-pro' ); ?></a>
			</div>
		<?php endforeach; ?>
	</div>
	<?php
}
