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

use WP_PowerSuite\Core\OptionStoreModuleSettingsRepository;
use WP_PowerSuite\OAuth\Helpers\OAuthSettingsHelper;
use WP_PowerSuite\OAuth\Admin\SettingsUiHelper;
use WP_PowerSuite\OAuth\UI\ButtonStyleHelper;

if ( ! function_exists( 'wp_powersuite_is_license_valid' ) || ! wp_powersuite_is_license_valid() ) {
	$license_page_url = admin_url( 'admin.php?page=powersuite-license' );
	?>
	<div class="wp-powersuite-module-settings wpps-settings-panel" data-module-id="<?php echo esc_attr( $module_id ?? 'facebook-sign-in' ); ?>">
		<div class="notice notice-warning inline" style="margin:0;">
			<p>
				<?php esc_html_e( 'Facebook Sign In requires an active license. Activate your license to configure Facebook login.', 'wppowersuite-lite' ); ?>
				<a href="<?php echo esc_url( $license_page_url ); ?>"><?php esc_html_e( 'Activate license', 'wppowersuite-lite' ); ?></a>
			</p>
		</div>
	</div>
	<?php
	return;
}

if ( ! isset( $module_id ) || empty( $module_id ) ) {
	$module_id = 'facebook-sign-in';
}

$settings     = OptionStoreModuleSettingsRepository::forModule( $module_id )->all( [] );
if ( ! is_array( $settings ) ) {
	$settings = [];
}

$app_id              = trim( $settings['app_id'] ?? '' );
$app_secret_stored   = trim( $settings['app_secret'] ?? '' );
$app_secret          = OAuthSettingsHelper::secretFieldValue( $app_secret_stored );
$redirect_after      = trim( $settings['redirect_after_login'] ?? '' );
$allow_registration = ! empty( $settings['allow_registration'] );
$button_style        = ButtonStyleHelper::sanitize( (string) ( $settings['button_style'] ?? 'default' ) );
$button_display      = ButtonStyleHelper::sanitizeDisplay( (string) ( $settings['button_display'] ?? 'label' ) );
$button_label_inline = ButtonStyleHelper::sanitizeLabelInline( $settings['button_label_inline'] ?? false );
$woo_login           = ! empty( $settings['woo_login'] );
$callback_url        = add_query_arg( \WP_PowerSuite\OAuth\Helpers\UrlHelper::AUTH_QUERY_PARAM, 'facebook', home_url( '/' ) );
$uid                 = preg_replace( '/[^a-z0-9_-]/i', '', $module_id );
?>
<div class="wp-powersuite-module-settings wpps-settings-panel wpps-settings-panel--accordion-stack" data-module-id="<?php echo esc_attr( $module_id ); ?>">
	<form class="wp-powersuite-module-settings-form" data-module-id="<?php echo esc_attr( $module_id ); ?>">
		<?php wp_nonce_field( 'wp_powersuite_save_module_settings_' . $module_id, 'wp_powersuite_nonce' ); ?>

		<?php SettingsUiHelper::renderHttpsNotice(); ?>

		<div class="wpps-ts-accordions">
			<div class="wpps-ts-accordion is-open">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_credentials" aria-expanded="true" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_credentials">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Facebook App Credentials', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_credentials" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_credentials">
					<p class="description" style="margin-top:0;">
						<?php esc_html_e( 'Create a Facebook App at developers.facebook.com, add the "Facebook Login" product, and configure the settings below.', 'wppowersuite-lite' ); ?>
					</p>

					<table class="form-table" role="presentation">
						<tbody>
							<tr>
								<th scope="row">
									<label for="<?php echo esc_attr( $module_id ); ?>_app_id">
										<?php esc_html_e( 'App ID', 'wppowersuite-lite' ); ?>
									</label>
								</th>
								<td>
									<input
										type="text"
										id="<?php echo esc_attr( $module_id ); ?>_app_id"
										name="<?php echo esc_attr( $module_id ); ?>[app_id]"
										value="<?php echo esc_attr( $app_id ); ?>"
										class="regular-text wp-powersuite-advanced-setting"
										data-setting-key="app_id"
										placeholder="1234567890123456"
									/>
									<p class="description">
										<?php esc_html_e( 'Your Facebook App ID from the app dashboard.', 'wppowersuite-lite' ); ?>
									</p>
								</td>
							</tr>
							<tr>
								<th scope="row">
									<label for="<?php echo esc_attr( $module_id ); ?>_app_secret">
										<?php esc_html_e( 'App Secret', 'wppowersuite-lite' ); ?>
									</label>
								</th>
								<td>
									<input
										type="password"
										id="<?php echo esc_attr( $module_id ); ?>_app_secret"
										name="<?php echo esc_attr( $module_id ); ?>[app_secret]"
										value="<?php echo esc_attr( $app_secret ); ?>"
										class="regular-text wp-powersuite-advanced-setting"
										data-setting-key="app_secret"
										placeholder="••••••••••••••••••••••••••••••••"
										autocomplete="off"
									/>
									<p class="description">
										<?php esc_html_e( 'Your Facebook App Secret. Keep this private. Leave unchanged to keep the saved secret.', 'wppowersuite-lite' ); ?>
									</p>
								</td>
							</tr>
							<tr>
								<th scope="row">
									<label for="<?php echo esc_attr( $module_id ); ?>_button_style">
										<?php esc_html_e( 'Button style', 'wppowersuite-lite' ); ?>
									</label>
								</th>
								<td>
									<select
										id="<?php echo esc_attr( $module_id ); ?>_button_style"
										name="<?php echo esc_attr( $module_id ); ?>[button_style]"
										class="wp-powersuite-advanced-setting"
										data-setting-key="button_style"
									>
										<option value="default" <?php selected( $button_style, 'default' ); ?>><?php esc_html_e( 'Filled', 'wppowersuite-lite' ); ?></option>
										<option value="outline" <?php selected( $button_style, 'outline' ); ?>><?php esc_html_e( 'Outline', 'wppowersuite-lite' ); ?></option>
										<option value="minimal" <?php selected( $button_style, 'minimal' ); ?>><?php esc_html_e( 'Minimal', 'wppowersuite-lite' ); ?></option>
									</select>
									<p class="description">
										<?php esc_html_e( 'Icon-only: inline (auto). Text+icon: optional “Inline row” below, or use shortcode width/align.', 'wppowersuite-lite' ); ?>
									</p>
								</td>
							</tr>
							<tr>
								<th scope="row">
									<label for="<?php echo esc_attr( $module_id ); ?>_button_display"><?php esc_html_e( 'Label & icon', 'wppowersuite-lite' ); ?></label>
								</th>
								<td>
									<select id="<?php echo esc_attr( $module_id ); ?>_button_display" name="<?php echo esc_attr( $module_id ); ?>[button_display]" class="wp-powersuite-advanced-setting" data-setting-key="button_display">
										<option value="label" <?php selected( $button_display, 'label' ); ?>><?php esc_html_e( 'Text and icon', 'wppowersuite-lite' ); ?></option>
										<option value="icon" <?php selected( $button_display, 'icon' ); ?>><?php esc_html_e( 'Icon only', 'wppowersuite-lite' ); ?></option>
									</select>
								</td>
							</tr>
							<tr
								id="<?php echo esc_attr( $module_id ); ?>_label_inline_row"
								style="<?php echo ( $button_display === 'label' ) ? '' : 'display:none;'; ?>"
							>
								<th scope="row">
									<label for="<?php echo esc_attr( $module_id ); ?>_button_label_inline"><?php esc_html_e( 'Inline row (text and icon)', 'wppowersuite-lite' ); ?></label>
								</th>
								<td>
									<label>
										<input
											type="checkbox"
											id="<?php echo esc_attr( $module_id ); ?>_button_label_inline"
											name="<?php echo esc_attr( $module_id ); ?>[button_label_inline]"
											value="1"
											<?php checked( $button_label_inline ); ?>
											class="wp-powersuite-advanced-setting"
											data-setting-key="button_label_inline"
										/>
										<?php esc_html_e( 'Use auto width so multiple labeled buttons can sit in one row', 'wppowersuite-lite' ); ?>
									</label>
								</td>
							</tr>
						</tbody>
					</table>

					<div class="wp-powersuite-info-box" style="background: #f0f6fc; border-left: 4px solid #2271b1; padding: 12px; margin: 20px 0 0;">
						<p style="margin: 0 0 8px 0;">
							<strong><?php esc_html_e( 'Valid OAuth Redirect URIs', 'wppowersuite-lite' ); ?></strong>
						</p>
						<p style="margin: 0 0 8px 0;">
							<?php esc_html_e( 'Add this URL to your Facebook App → Facebook Login → Settings → Valid OAuth Redirect URIs:', 'wppowersuite-lite' ); ?>
						</p>
						<p style="margin: 0 0 8px 0;">
							<code style="word-break: break-all;"><?php echo esc_html( $callback_url ); ?></code>
						</p>
						<p style="margin: 8px 0 0 0;">
							<button type="button" class="button button-secondary wpps-copy-callback-url" data-url="<?php echo esc_attr( $callback_url ); ?>">
								<?php esc_html_e( 'Copy URL', 'wppowersuite-lite' ); ?>
							</button>
						</p>
					</div>

					<div class="wp-powersuite-info-box" style="background: #fcf0f1; border-left: 4px solid #d63638; padding: 12px; margin: 20px 0 0;">
						<p style="margin: 0 0 8px 0;">
							<strong><?php esc_html_e( 'Required Facebook App Permissions', 'wppowersuite-lite' ); ?></strong>
						</p>
						<p style="margin: 0;">
							<?php esc_html_e( 'Ensure your app requests "email" and "public_profile" permissions. These are included by default when adding the Facebook Login product.', 'wppowersuite-lite' ); ?>
						</p>
					</div>
				</div>
			</div>

			<div class="wpps-ts-accordion">
				<button type="button" class="wpps-ts-accordion-header" id="<?php echo esc_attr( $uid ); ?>_acc_head_registration" aria-expanded="false" aria-controls="<?php echo esc_attr( $uid ); ?>_acc_body_registration">
					<span class="wpps-ts-accordion-title"><?php esc_html_e( 'Registration & Users', 'wppowersuite-lite' ); ?></span>
					<span class="dashicons dashicons-arrow-down-alt2 wpps-ts-accordion-chevron" aria-hidden="true"></span>
				</button>
				<div class="wpps-ts-accordion-body" id="<?php echo esc_attr( $uid ); ?>_acc_body_registration" role="region" aria-labelledby="<?php echo esc_attr( $uid ); ?>_acc_head_registration">
					<table class="form-table" role="presentation">
						<tbody>
							<tr>
								<th scope="row">
									<label for="<?php echo esc_attr( $module_id ); ?>_allow_registration">
										<?php esc_html_e( 'Allow New User Registration', 'wppowersuite-lite' ); ?>
									</label>
								</th>
								<td>
									<label>
										<input
											type="checkbox"
											id="<?php echo esc_attr( $module_id ); ?>_allow_registration"
											name="<?php echo esc_attr( $module_id ); ?>[allow_registration]"
											value="1"
											<?php checked( $allow_registration ); ?>
											class="wp-powersuite-advanced-setting"
											data-setting-key="allow_registration"
										/>
										<?php esc_html_e( 'Create new WordPress accounts for first-time Facebook sign-ins', 'wppowersuite-lite' ); ?>
									</label>
									<p class="description">
										<?php esc_html_e( 'If disabled, only existing users (matched by email) can sign in with Facebook. New accounts are created only when Settings → General → Anyone can register is enabled, and they always receive the Subscriber role.', 'wppowersuite-lite' ); ?>
									</p>
								</td>
							</tr>
							<tr>
								<th scope="row">
									<label for="<?php echo esc_attr( $module_id ); ?>_redirect_after_login">
										<?php esc_html_e( 'Redirect After Login', 'wppowersuite-lite' ); ?>
									</label>
								</th>
								<td>
									<input
										type="url"
										id="<?php echo esc_attr( $module_id ); ?>_redirect_after_login"
										name="<?php echo esc_attr( $module_id ); ?>[redirect_after_login]"
										value="<?php echo esc_attr( $redirect_after ); ?>"
										class="regular-text wp-powersuite-advanced-setting"
										data-setting-key="redirect_after_login"
										placeholder="<?php echo esc_attr( home_url( '/' ) ); ?>"
									/>
									<p class="description">
										<?php esc_html_e( 'Where to send users after successful login. Leave blank for site homepage.', 'wppowersuite-lite' ); ?>
									</p>
								</td>
							</tr>
							<?php if ( class_exists( 'WooCommerce' ) ) : ?>
							<tr>
								<th scope="row">
									<label for="<?php echo esc_attr( $module_id ); ?>_woo_login">
										<?php esc_html_e( 'WooCommerce', 'wppowersuite-lite' ); ?>
									</label>
								</th>
								<td>
									<label>
										<input
											type="checkbox"
											id="<?php echo esc_attr( $module_id ); ?>_woo_login"
											name="<?php echo esc_attr( $module_id ); ?>[woo_login]"
											value="1"
											<?php checked( $woo_login ); ?>
											class="wp-powersuite-advanced-setting"
											data-setting-key="woo_login"
										/>
										<?php esc_html_e( 'Show Sign in with Facebook on WooCommerce login, registration, and checkout forms', 'wppowersuite-lite' ); ?>
									</label>
								</td>
							</tr>
							<?php endif; ?>
						</tbody>
					</table>
				</div>
			</div>

			<?php
			if ( class_exists( \WP_PowerSuite\OAuth\Admin\LinkingPolicySection::class ) ) {
				\WP_PowerSuite\OAuth\Admin\LinkingPolicySection::render(
					[
						'as_accordion' => true,
						'uid'          => $uid,
					]
				);
			}
			?>
		</div>

		<div class="wp-powersuite-info-box" style="background: #f0f6fc; border-left: 4px solid #2271b1; padding: 12px; margin: 20px 0 0;">
			<p style="margin: 0 0 8px 0;">
				<strong><?php esc_html_e( 'Shortcode', 'wppowersuite-lite' ); ?></strong>
			</p>
			<p style="margin: 0 0 8px 0;">
				<code>[social_login providers="facebook" type="button" style="outline"]</code>
			</p>
			<p style="margin: 0 0 8px 0;">
				<code>[facebook_sign_in]</code>
			</p>
			<p style="margin: 0 0 8px 0;">
				<?php esc_html_e( 'Optional: redirect, label, style (default, outline, minimal).', 'wppowersuite-lite' ); ?>
			</p>
			<p style="margin: 0;">
				<code>[facebook_sign_in style="outline"]</code>
			</p>
		</div>
</form>
</div>

<?php ob_start(); ?>
(function() {
	var cb = document.getElementById('<?php echo esc_js( $module_id ); ?>_allow_registration');
	var rows = document.querySelectorAll('.wpps-facebook-signin-auto-register-options');
	if (cb && rows.length) {
		function toggle() {
			rows.forEach(function(r) { r.style.display = cb.checked ? '' : 'none'; });
		}
		cb.addEventListener('change', toggle);
		toggle();
	}
	document.addEventListener('click', function(e) {
		var btn = e.target.closest('.wpps-copy-callback-url');
		if (!btn) return;
		var url = btn.getAttribute('data-url');
		if (!url) return;
		if (navigator.clipboard && navigator.clipboard.writeText) {
			navigator.clipboard.writeText(url).then(function() {
				var orig = btn.textContent;
				btn.textContent = '<?php echo esc_js( __( 'Copied!', 'wppowersuite-lite' ) ); ?>';
				setTimeout(function() { btn.textContent = orig; }, 2000);
			});
		} else {
			var ta = document.createElement('textarea');
			ta.value = url;
			document.body.appendChild(ta);
			ta.select();
			document.execCommand('copy');
			document.body.removeChild(ta);
			var orig = btn.textContent;
			btn.textContent = '<?php echo esc_js( __( 'Copied!', 'wppowersuite-lite' ) ); ?>';
			setTimeout(function() { btn.textContent = orig; }, 2000);
		}
	});
	var d = document.getElementById('<?php echo esc_js( $module_id ); ?>_button_display');
	var row = document.getElementById('<?php echo esc_js( $module_id ); ?>_label_inline_row');
	var chk = document.getElementById('<?php echo esc_js( $module_id ); ?>_button_label_inline');
	if (d && row) {
		function t() {
			var on = d.value === 'label';
			row.style.display = on ? '' : 'none';
			if (!on && chk) { chk.checked = false; }
		}
		d.addEventListener('change', t);
		t();
	}
})();
<?php \WP_PowerSuite\Core\InlineAssets::script( 'wpps-ia-modules-facebook-sign-in-admin-php-script-1', (string) ob_get_clean() ); ?>
