<?php
/**
 * File path: includes/npmp-setup-wizard.php
 *
 * Setup wizard for first-time Nonprofit Manager users
 */
defined( 'ABSPATH' ) || exit;

/**
 * Check if setup wizard should be displayed
 *
 * @return bool True if wizard should run
 */
function npmp_should_show_setup_wizard() {
	$setup_completed = get_option( 'npmp_setup_completed', false );
	$is_activation   = get_transient( 'npmp_activation_redirect' );

	return ! $setup_completed && $is_activation;
}

/**
 * Redirect to setup wizard on activation
 */
add_action(
	'admin_init',
	static function () {
		if ( get_transient( 'npmp_activation_redirect' ) ) {
			delete_transient( 'npmp_activation_redirect' );

			if ( ! npmp_should_show_setup_wizard() ) {
				return;
			}

			wp_safe_redirect( admin_url( 'admin.php?page=npmp_setup_wizard' ) );
			exit;
		}
	}
);

/**
 * Register setup wizard page
 */
add_action(
	'admin_menu',
	static function () {
		$hook = add_submenu_page(
			null, // Hidden from menu
			__( 'Nonprofit Manager Setup', 'nonprofit-manager' ),
			__( 'Setup', 'nonprofit-manager' ),
			'manage_options',
			'npmp_setup_wizard',
			'npmp_render_setup_wizard'
		);

		// A null-parent page never lands in the $submenu array, so core's
		// get_admin_page_title() leaves the global $title null and PHP 8.1+
		// warns on strip_tags( null ) in admin-header.php. Set it ourselves
		// before the header renders.
		if ( $hook ) {
			add_action(
				'load-' . $hook,
				static function () {
					$GLOBALS['title'] = __( 'Nonprofit Manager Setup', 'nonprofit-manager' );
				}
			);
		}
	}
);

/**
 * Handle setup wizard form submission
 */
add_action(
	'admin_init',
	static function () {
		if (
			isset( $_POST['npmp_setup_nonce'] ) &&
			wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['npmp_setup_nonce'] ) ), 'npmp_setup_wizard' ) &&
			current_user_can( 'manage_options' )
		) {
			$step = isset( $_POST['npmp_setup_step'] ) ? sanitize_text_field( wp_unslash( $_POST['npmp_setup_step'] ) ) : '';

			if ( 'complete' === $step ) {
				// Save selected features
				$enabled = array(
					'members'     => isset( $_POST['npmp_feature_members'] ),
					'newsletters' => isset( $_POST['npmp_feature_newsletters'] ),
					'donations'   => isset( $_POST['npmp_feature_donations'] ),
					'calendar'    => isset( $_POST['npmp_feature_calendar'] ),
					'social'      => isset( $_POST['npmp_feature_social'] ),
				);

				// Ensure newsletter dependency
				if ( ! $enabled['members'] ) {
					$enabled['newsletters'] = false;
				}

				update_option( 'npmp_enabled_features', $enabled );
				update_option( 'npmp_setup_completed', true );

				// Both opt-ins below are unchecked by default and only act on an
				// explicit, intentional checkbox the site owner ticked themselves
				// (WordPress.org requires attribution links and any data leaving
				// the site to be opt-in, not defaulted on).
				if ( isset( $_POST['npmp_setup_powered_by_optin'] ) ) {
					update_option( 'npmp_powered_by_optin', 1 );
				}

				if ( isset( $_POST['npmp_setup_email_optin'] ) && isset( $_POST['npmp_setup_email'] ) ) {
					$wizard_email = sanitize_email( wp_unslash( $_POST['npmp_setup_email'] ) );
					if ( is_email( $wizard_email ) && function_exists( 'npmp_subscribe_to_newsletter' ) ) {
						$current_user = wp_get_current_user();
						npmp_subscribe_to_newsletter( $wizard_email, $current_user ? $current_user->display_name : '' );
					}
				}

				wp_safe_redirect( admin_url( 'admin.php?page=npmp_main&setup_complete=1' ) );
				exit;
			}
		}
	}
);

/**
 * Render the setup wizard
 */
function npmp_render_setup_wizard() {
	$version = npmp_get_version();
	?>
	<div class="wrap">
		<style>
			.npmp-setup-wizard {
				max-width: 800px;
				margin: 50px auto;
				background: #fff;
				padding: 40px;
				border-radius: 8px;
				box-shadow: 0 1px 3px rgba(0,0,0,0.1);
			}
			.npmp-setup-wizard h1 {
				text-align: center;
				margin-bottom: 10px;
			}
			.npmp-setup-wizard .subtitle {
				text-align: center;
				font-size: 16px;
				color: #666;
				margin-bottom: 40px;
			}
			.npmp-setup-wizard .feature-grid {
				display: grid;
				grid-template-columns: 1fr 1fr;
				gap: 20px;
				margin: 30px 0;
			}
			.npmp-setup-wizard .feature-card {
				border: 2px solid #ddd;
				padding: 20px;
				border-radius: 4px;
				transition: all 0.2s;
			}
			.npmp-setup-wizard .feature-card:hover {
				border-color: #2271b1;
			}
			.npmp-setup-wizard .feature-card.selected {
				border-color: #2271b1;
				background-color: #f0f6fc;
			}
			.npmp-setup-wizard .feature-card input[type="checkbox"] {
				margin-right: 10px;
			}
			.npmp-setup-wizard .feature-card h3 {
				margin: 0 0 10px 0;
				font-size: 18px;
			}
			.npmp-setup-wizard .feature-card p {
				margin: 0;
				color: #666;
			}
			.npmp-setup-wizard .button-primary {
				display: block;
				margin: 30px auto 0;
				padding: 12px 40px;
				height: auto;
				font-size: 16px;
			}
			.npmp-version-badge {
				display: inline-block;
				background: #2271b1;
				color: #fff;
				padding: 4px 12px;
				border-radius: 12px;
				font-size: 12px;
				font-weight: 600;
				text-transform: uppercase;
			}
		</style>

		<div class="npmp-setup-wizard">
			<h1>
				<?php esc_html_e( 'Welcome to Nonprofit Manager', 'nonprofit-manager' ); ?>
				<?php if ( 'pro' === $version ) : ?>
					<span class="npmp-version-badge"><?php esc_html_e( 'Pro', 'nonprofit-manager' ); ?></span>
				<?php endif; ?>
			</h1>
			<p class="subtitle">
				<?php
				if ( 'pro' === $version ) {
					esc_html_e( 'Thank you for upgrading to Pro! Let\'s set up your nonprofit management system.', 'nonprofit-manager' );
				} else {
					esc_html_e( 'Let\'s get started by choosing which features you need.', 'nonprofit-manager' );
				}
				?>
			</p>

			<form method="post" id="npmp-setup-form">
				<?php wp_nonce_field( 'npmp_setup_wizard', 'npmp_setup_nonce' ); ?>
				<input type="hidden" name="npmp_setup_step" value="complete">

				<h2><?php esc_html_e( 'Select Your Features', 'nonprofit-manager' ); ?></h2>
				<p><?php esc_html_e( 'Choose the features you want to activate. You can change these later.', 'nonprofit-manager' ); ?></p>

				<div class="feature-grid">
					<div class="feature-card selected">
						<label>
							<h3>
								<input type="checkbox" name="npmp_feature_members" checked onchange="npmpToggleFeatureCard(this)">
								<?php esc_html_e( 'Member Tracking', 'nonprofit-manager' ); ?>
							</h3>
							<p><?php esc_html_e( 'Track members, manage contacts, and capture signups through customizable forms.', 'nonprofit-manager' ); ?></p>
						</label>
					</div>

					<div class="feature-card">
						<label>
							<h3>
								<input type="checkbox" name="npmp_feature_newsletters" onchange="npmpToggleFeatureCard(this)">
								<?php esc_html_e( 'Email Newsletters', 'nonprofit-manager' ); ?>
							</h3>
							<p><?php esc_html_e( 'Send newsletters to your members. Requires Member Tracking.', 'nonprofit-manager' ); ?></p>
						</label>
					</div>

					<div class="feature-card selected">
						<label>
							<h3>
								<input type="checkbox" name="npmp_feature_donations" checked onchange="npmpToggleFeatureCard(this)">
								<?php esc_html_e( 'Donations', 'nonprofit-manager' ); ?>
							</h3>
							<p><?php esc_html_e( 'Accept and track donations with integrated payment processing.', 'nonprofit-manager' ); ?></p>
						</label>
					</div>

					<div class="feature-card">
						<label>
							<h3>
								<input type="checkbox" name="npmp_feature_calendar" onchange="npmpToggleFeatureCard(this)">
								<?php esc_html_e( 'Event Calendar', 'nonprofit-manager' ); ?>
							</h3>
							<p><?php esc_html_e( 'Manage events with a public calendar and iCal feed.', 'nonprofit-manager' ); ?></p>
						</label>
					</div>
				</div>

				<?php if ( 'free' === $version ) : ?>
					<div class="notice notice-info inline" style="margin: 30px 0;">
						<p>
							<strong><?php esc_html_e( 'Want more features?', 'nonprofit-manager' ); ?></strong>
							<?php esc_html_e( 'Upgrade to Nonprofit Manager Pro for advanced reporting, recurring donations, custom fields, and more.', 'nonprofit-manager' ); ?>
							<a href="<?php echo esc_url( npmp_get_upgrade_url() ); ?>" target="_blank">
								<?php esc_html_e( 'Learn more', 'nonprofit-manager' ); ?>
							</a>
						</p>
					</div>
				<?php endif; ?>

				<h2><?php esc_html_e( 'Optional', 'nonprofit-manager' ); ?></h2>
				<p class="npmp-setup-optin">
					<label>
						<input type="checkbox" name="npmp_setup_powered_by_optin" value="1">
						<?php esc_html_e( 'Show a "Powered by Nonprofit Manager" link on public donation forms and newsletter emails.', 'nonprofit-manager' ); ?>
					</label>
					<br>
					<span class="description"><?php esc_html_e( 'Off unless checked. Change this any time under Settings > General.', 'nonprofit-manager' ); ?></span>
				</p>
				<p class="npmp-setup-optin">
					<label>
						<input type="checkbox" name="npmp_setup_email_optin" value="1" onchange="document.getElementById('npmp-setup-email').disabled = !this.checked;">
						<?php esc_html_e( 'Email me setup tips and Nonprofit Manager product updates.', 'nonprofit-manager' ); ?>
					</label>
					<br>
					<input
						type="email"
						id="npmp-setup-email"
						name="npmp_setup_email"
						value="<?php echo esc_attr( wp_get_current_user()->user_email ); ?>"
						disabled
						style="margin-top: 6px; max-width: 320px;"
					>
					<br>
					<span class="description"><?php esc_html_e( 'Off unless checked. Double opt-in, unsubscribe any time.', 'nonprofit-manager' ); ?></span>
				</p>

				<?php submit_button( __( 'Complete Setup', 'nonprofit-manager' ), 'primary', 'submit', false ); ?>
			</form>
		</div>

		<script>
		function npmpToggleFeatureCard(checkbox) {
			const card = checkbox.closest('.feature-card');
			if (checkbox.checked) {
				card.classList.add('selected');
			} else {
				card.classList.remove('selected');
			}
		}
		</script>
	</div>
	<?php
}
