<?php

$generate_redirect_uri_html = function ( $provider_id, $provider_label, $app_settings_field ) {
	$url = add_query_arg( array( 'provider' => $provider_id ), wp_login_url() );
	/* translators: 1: App Settings Field, 2: Provider Label */
	$instruction = sprintf( __( 'Paste this into the "%1$s" field in %2$s:', 'forumax' ), $app_settings_field, $provider_label );
	
	return $instruction .
		'<div style="display: flex; align-items: center; gap: 8px; margin-top: 10px;">' .
		'<input type="text" id="' . esc_attr( $provider_id ) . '-redirect-uri" value="' . esc_url( $url ) . '" readonly="readonly" style="width: 100%; max-width: 400px; padding: 4px 8px; background: #fff;" />' .
		'<button type="button" class="button button-secondary" style="display: flex; align-items: center; padding: 0 8px;" onclick="var copyText = document.getElementById(\'' . esc_attr( $provider_id ) . '-redirect-uri\'); copyText.select(); document.execCommand(\'copy\'); var icon = this.querySelector(\'.dashicons\'); icon.className = \'dashicons dashicons-yes\'; icon.style.color = \'green\'; setTimeout(function(){ icon.className = \'dashicons dashicons-clipboard\'; icon.style.color = \'\'; }, 2000);" title="' . esc_attr__( 'Copy', 'forumax' ) . '">' .
		'<span class="dashicons dashicons-clipboard"></span></button></div>';
};

// Create a section.
CSF::createSection(
	$prefix,
	[
		'title'  => __( 'Social Login', 'forumax' ),
		'icon'   => 'dashicons dashicons-share-alt',
		'fields' => [
			[
				'id'      => 'social_login_enabled',
				'type'    => 'switcher',
				'title'   => esc_html__( 'Enable Social Login', 'forumax' ),
				'desc'    => esc_html__( 'Allow your users to log in instantly using their social media accounts. This removes friction and boosts conversion rates.', 'forumax' ),
				'default' => false,
				'class'   => 'st-promax-notice'
			],

			[
				'id'         => 'social_login_notice',
				'type'       => 'notice',
				'style'      => 'info',
				'content'    => esc_html__( 'Configure your social login providers below using the built-in descriptions and setup notes.', 'forumax' ),
				'dependency' => [ 'social_login_enabled', '==', 'true' ],
			],

			// Google Provider
			[
				'id'         => 'social_login_google',
				'type'       => 'fieldset',
				'title'      => esc_html__( 'Google One-Tap Sign-in', 'forumax' ),
				'subtitle'   => esc_html__( 'Set up Google One-Tap by entering your Client ID and Client Secret, then copy the redirect URL below into Google Cloud Console exactly as shown.', 'forumax' ),
				'dependency' => [ 'social_login_enabled', '==', 'true' ],
				'class'      => 'st-promax-notice',
				'fields'     => [
					[
						'id'      => 'social_login_google_enabled',
						'type'    => 'switcher',
						'title'   => esc_html__( 'Enable Google Login', 'forumax' ),
						'default' => false,
					],
					[
						'id'         => 'social_login_google_client_id',
						'type'       => 'text',
						'title'      => esc_html__( 'Client ID', 'forumax' ),
						'subtitle'   => esc_html__( 'Copy the Client ID from Google Cloud Console. Use the exact value from your OAuth app.', 'forumax' ),
						'desc'       => sprintf(
							__( 'Get your Client ID from the <a href="%s" target="_blank">Google Cloud Console</a>.', 'forumax' ),
							'https://console.cloud.google.com/apis/credentials'
						),
						'dependency' => [ 'social_login_google_enabled', '==', 'true' ],
					],
					[
						'id'         => 'social_login_google_client_secret',
						'type'       => 'text',
						'title'      => esc_html__( 'Client Secret', 'forumax' ),
						'subtitle'   => esc_html__( 'Paste the Client Secret from the same Google OAuth app. Avoid extra spaces before or after the value.', 'forumax' ),
						'dependency' => [ 'social_login_google_enabled', '==', 'true' ],
					],
					[
						'id'         => 'social_login_google_redirect_uri',
						'type'       => 'notice',
						'style'      => 'info',
						'title'      => esc_html__( 'Authorized Redirect URI', 'forumax' ),
						'subtitle'   => esc_html__( 'Copy this exact URL into Google Cloud Console. A mismatch in protocol, path, or trailing slash will break login.', 'forumax' ),
						'content'    => $generate_redirect_uri_html( 'google', 'Google Console', 'Authorized redirect URIs' ),
						'dependency' => [ 'social_login_google_enabled', '==', 'true' ],
					],
				],
			],

			// Facebook Provider
			[
				'id'         => 'social_login_facebook',
				'type'       => 'fieldset',
				'title'      => esc_html__( 'Facebook Login', 'forumax' ),
				'subtitle'   => esc_html__( 'Add your Facebook App ID and App Secret here, then paste the redirect URL into Meta for Developers and switch the app to Live when ready.', 'forumax' ),
				'dependency' => [ 'social_login_enabled', '==', 'true' ],
				'class'      => 'st-promax-notice',
				'fields'     => [
					[
						'id'      => 'social_login_facebook_enabled',
						'type'    => 'switcher',
						'title'   => esc_html__( 'Enable Facebook Login', 'forumax' ),
						'default' => false,
					],
					[
						'id'         => 'social_login_facebook_app_id',
						'type'       => 'text',
						'title'      => esc_html__( 'App ID', 'forumax' ),
						'subtitle'   => esc_html__( 'Copy the App ID from Meta for Developers for the app you want to use for login.', 'forumax' ),
						'desc'       => sprintf(
							__( 'Get your App ID from <a href="%s" target="_blank">Meta for Developers</a>.', 'forumax' ),
							'https://developers.facebook.com/apps/'
						),
						'dependency' => [ 'social_login_facebook_enabled', '==', 'true' ],
					],
					[
						'id'         => 'social_login_facebook_app_secret',
						'type'       => 'text',
						'title'      => esc_html__( 'App Secret', 'forumax' ),
						'subtitle'   => esc_html__( 'Paste the App Secret from Meta exactly as provided. Hidden spaces or a wrong app secret will cause login failures.', 'forumax' ),
						'dependency' => [ 'social_login_facebook_enabled', '==', 'true' ],
					],
					[
						'id'         => 'social_login_facebook_redirect_uri',
						'type'       => 'notice',
						'style'      => 'info',
						'title'      => esc_html__( 'Authorized Redirect URI', 'forumax' ),
						'subtitle'   => esc_html__( 'Copy this exact URL into the Valid OAuth Redirect URIs field in Meta. The app must also be configured correctly in Facebook Login settings.', 'forumax' ),
						'content'    => $generate_redirect_uri_html( 'facebook', 'your Facebook App settings', 'Valid OAuth Redirect URIs' ),
						'dependency' => [ 'social_login_facebook_enabled', '==', 'true' ],
					],
				],
			],

			// X Provider
			[
				'id'         => 'social_login_twitter',
				'type'       => 'fieldset',
				'title'      => esc_html__( 'X (Twitter) Login', 'forumax' ),
				'subtitle'   => esc_html__( 'Enter your X app API Key and API Secret, then copy the callback URL below into your X app settings with OAuth enabled.', 'forumax' ),
				'dependency' => [ 'social_login_enabled', '==', 'true' ],
				'class'      => 'st-promax-notice',
				'fields'     => [
					[
						'id'      => 'social_login_twitter_enabled',
						'type'    => 'switcher',
						'title'   => esc_html__( 'Enable X (Twitter) Login', 'forumax' ),
						'default' => false,
					],
					[
						'id'         => 'social_login_twitter_api_key',
						'type'       => 'text',
						'title'      => esc_html__( 'API Key', 'forumax' ),
						'subtitle'   => esc_html__( 'Copy the API Key from your X developer app. Make sure you are using the correct app and environment.', 'forumax' ),
						'desc'       => sprintf(
							__( 'Get your API Key from the <a href="%s" target="_blank">X Developer Portal</a>.', 'forumax' ),
							'https://developer.twitter.com/en/portal/projects-and-apps'
						),
						'dependency' => [ 'social_login_twitter_enabled', '==', 'true' ],
					],
					[
						'id'         => 'social_login_twitter_api_secret',
						'type'       => 'text',
						'title'      => esc_html__( 'API Secret', 'forumax' ),
						'subtitle'   => esc_html__( 'Paste the API Secret exactly as provided by X. Confirm OAuth permissions are enabled for the app if login does not work.', 'forumax' ),
						'dependency' => [ 'social_login_twitter_enabled', '==', 'true' ],
					],
					[
						'id'         => 'social_login_twitter_redirect_uri',
						'type'       => 'notice',
						'style'      => 'info',
						'title'      => esc_html__( 'Authorized Callback URL', 'forumax' ),
						'subtitle'   => esc_html__( 'Copy this exact URL into your X app callback settings. A mismatch here is one of the most common login issues.', 'forumax' ),
						'content'    => $generate_redirect_uri_html( 'twitter', 'your X App settings', 'Callback URI / Redirect URL' ),
						'dependency' => [ 'social_login_twitter_enabled', '==', 'true' ],
					],
				],
			],
		],
	]
);
