<?php
if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly
}

$credentials                   = SftcyWordformConnectGooglesheet::get_google_credentials();
$google_client_id              = isset( $credentials['clientID'] ) ? $credentials['clientID'] : '';
$google_client_secret          = isset( $credentials['clientSecret'] ) ? $credentials['clientSecret'] : '';
$google_auth_managed_available = SftcyWordformConnectGooglesheet::is_google_managed_oauth_available();
$google_auth_uri               = SftcyWordformConnectGooglesheet::get_google_authorization_uri();
$wordform_connect_is_local_env = SftcyWordformConnectBase::is_wordform_connect_local_environment();
?>
<!-- Start Google Sheet Configure Modal Form -->
<div id="GoogleSheetAPIModal" title="Google Sheet Configure" class="google-sheep-api-modal" style="display: none;">
			<div class="wordform-connect-modal-quickstart">
				<strong>Quick setup:</strong>
				<?php if ( $google_auth_managed_available ) : ?>
					Use <strong>Connect with Google</strong> to verify the account and finish setup in one flow.
				<?php else : ?>
					Enter the OAuth credentials, save them, then continue to Google to verify and approve access.
				<?php endif; ?>
			</div>

			<!-- Start Gooogle Credentials Create Help Instructions -->
			<?php
				$host = $_SERVER['SERVER_NAME'] ?? $_SERVER['HTTP_HOST'];
				$protocol = ( isset( $_SERVER['HTTPS'] ) && 'on' === $_SERVER['HTTPS'] ) ? 'https' : 'http';
				$full_domain_with_protocol = $protocol . '://' . $host;
				$google_auth_redirect_url = SftcyWordformConnectBase::get_wordform_connect_oauth_redirect_url();
			?>

			<h5>How to configure Google Sheet connection</h5>
			<p>
				<a href="https://developers.google.com/sheets/api/quickstart/js" target="_blank" rel="noopener">Google Sheets API Quickstart</a> |
				<a href="https://support.google.com/cloud/answer/10311615" target="_blank" rel="noopener">Google OAuth Consent Screen Help</a> |
				<a href="https://support.google.com/cloud/answer/6158849" target="_blank" rel="noopener">Google OAuth Client ID Help</a>
			</p>
			<div id="google-oauth-help" class="accordion">
			<?php if ( $google_auth_managed_available ) : ?>
			<h3>1. Click Connect with Google</h3>
			<div>
				<p>
					Use the <strong>Connect with Google</strong> button below. WordForm will open Google in a new step so you can sign in with the Google account that should own the spreadsheet connection.
				</p>
			</div>

			<h3>2. Approve spreadsheet access</h3>
			<div>
				<p>
					Approve the requested Google Sheets permission. After you return to WordForm, the app status will be saved as <strong>Configured</strong>.
				</p>
			</div>

			<h3>3. Create the form connection</h3>
			<div>
				<p>
					Choose a form, select <strong>Google Sheet</strong>, and click <strong>Create Connection</strong>. WordForm will create the spreadsheet and keep sending later submissions there.
				</p>
			</div>

			<h3>4. Keep the connection stable</h3>
			<div>
				<p>
					The connection keeps working long term through stored refresh access. If the Google account revokes access or authentication data is lost, use <strong>Reconfigure</strong> and connect again the same way.
				</p>
				<p>
					If you are using your own Google Cloud app instead of managed connect, keep the consent screen in production or published mode so Google does not force frequent re-authorization.
				</p>
			</div>
			<?php else : ?>

			<h3>1. Open Google Cloud Console</h3>
			<div>
				<p>
				Go to <a href="https://console.cloud.google.com/" target="_blank">Google Cloud Console</a>, sign in, and select an existing project or create a new project for WordForm.
				</p>
			</div>

			<h3>2. Configure OAuth consent screen</h3>
			<div>
				<p>
					Navigate to
					<a href="https://console.cloud.google.com/apis/credentials/consent" target="_blank">APIs &gt; OAuth consent screen</a>.
					Complete the app details so Google can authorize your WordForm connection.
				</p>

				<ul>
					<li><code>Set App name, support email, and authorized domain. Your domain is: <?php echo $full_domain_with_protocol; ?></code></li>
					<li><code>If publishing status is Testing, add your Google account under Test Users.</code></li>
					<li><code>Save and continue until the consent screen setup is complete.</code></li>
				</ul>
			</div>

			<h3>3. Enable Google Sheets API</h3>
			<div>
				<p>
				Open <a href="https://console.cloud.google.com/apis/library/sheets.googleapis.com" target="_blank">Google Sheets API</a> and click <strong>Enable</strong> for the selected project.
				</p>
			</div>

			<h3>4. Create OAuth client ID</h3>
			<div>
				<p>
				Go to
				<a href="https://console.cloud.google.com/apis/credentials" target="_blank">
				APIs &gt; Credentials</a>, click <em>Create Credentials</em>, then choose <strong>OAuth Client ID</strong>.
				</p>
				<p>Choose <strong>Web Application</strong> as the type and provide:</p>
				<ul>
				<li>
					<strong>Authorized JavaScript origin</strong>:
					<code>
						<span><?php echo $full_domain_with_protocol; ?></span>
						<a href="Javascript::void()" onclick="copyText(this)"><span title="Copy" class="dashicons dashicons-admin-page text-warning"></span></a>
						(<small>Copy-Paste this as authorized domain info</small>)
					</code>
				</li>
				<li><strong>Authorized Redirect URI</strong>:
					<code>
						<span><?php echo $google_auth_redirect_url; ?></span>
						<a href="Javascript::void()" onclick="copyText(this)"><span title="Copy" class="dashicons dashicons-admin-page text-warning"></span></a>
						(<small>Copy-Paste this as authorized redirect URI</small>)
					</code>
				</li>
				</ul>
				<p><strong>Important:</strong> Redirect URI must be an exact match. A mismatch in domain, protocol, port, or slash will break authentication.</p>
				<p><strong>Important:</strong> The redirect URL saved in Google Cloud must stay exactly the same as the redirect URL shown in this WordForm modal. If you change the redirect URL in one place, update the other place before authorize or re-authorize.</p>
				<?php if ( $wordform_connect_is_local_env ) : ?>
				<p><strong>Local development:</strong> WordForm now uses your direct local callback URL. Google supports localhost redirect URIs for web server apps. If you prefer a public HTTPS tunnel while testing, define <code>WORDFORM_CONNECT_OAUTH_REDIRECT_URL</code> in <code>wp-config.php</code> with that exact callback URL.</p>
				<?php else : ?>
				<p><strong>Production:</strong> WordForm uses your own site callback URL directly. No third-party redirect service is required.</p>
				<?php endif; ?>
			</div>

			<h3>5. Paste credentials into WordForm</h3>
			<div>
				<p>
				Copy the generated <strong>Client ID</strong> and <strong>Client Secret</strong>, paste them into the fields below, then click <strong>Save / Update</strong>.
				</p>
				<p>
				WordForm will redirect you to Google once so you can approve access to your sheet connection.
				</p>
			</div>
			<?php endif; ?>

			</div><!-- #google-oauth-help  -->
			<!-- End Google Credentials Create Help Instructions -->

	<div class="google-sheet-configure-input-elements-wrapper" style="display: flex; flex-direction:column;">
		<div class="wordform-google-sheet-credentials-feedback-msg"></div>
		<?php if ( $google_auth_managed_available ) : ?>
		<div class="wordform-managed-oauth-connect-action">
			<p>
				<strong>Quick Connect is enabled for this site.</strong> Click the button below, sign in to Google, and approve access.
			</p>
			<p class="wordform-connect-oauth-verification-note"><i class="fa-solid fa-shield-check"></i> Google verifies the connection during sign-in and consent approval.</p>
			<button
				type="button"
				class="button button-primary wordform-start-managed-oauth-authentication-btn"
				data-auth-url="<?php echo esc_url( $google_auth_uri ); ?>"
			><i class="fa-brands fa-google"></i> Connect with Google</button>
		</div>
		<?php else : ?>
		<div>
			<label for="googleAuthClientId"><i class="fa-solid fa-id-badge"></i> Google Authentication Client ID<a class="wordform-field-help-link" href="https://support.google.com/cloud/answer/6158849" target="_blank" rel="noopener" aria-label="How to get your Google OAuth Client ID"><span class="dashicons dashicons-editor-help" aria-hidden="true"></span></a></label><br/>
			<input type="text" id="googleAuthClientId" name="google-auth-client-id" class="google-auth-client-id" placeholder="Google Client ID" value="<?php echo esc_attr( $google_client_id ); ?>">
		</div>

		<div>
			<label for="googleAuthClientSecret"><i class="fa-solid fa-key"></i> Google Authentication Client Secret<a class="wordform-field-help-link" href="https://support.google.com/cloud/answer/6158849" target="_blank" rel="noopener" aria-label="How to get your Google OAuth Client Secret"><span class="dashicons dashicons-editor-help" aria-hidden="true"></span></a></label><br/>
			<input type="text" id="googleAuthClientSecret" name="google-auth-client-secret" class="google-auth-client-secret" placeholder="Google Client Secret" value="<?php echo esc_attr( $google_client_secret ); ?>">
		</div>

		<div>
			<label for="googleAuthRedirectUrl">
				<i class="fa-solid fa-link"></i> Google Authentication Redirect URL
				<span style="display: none;"><?php echo $google_auth_redirect_url; ?></span><a href="Javascript::void()" onclick="copyText(this)"><span title="Copy" class="dashicons dashicons-admin-page text-warning"></span></a>(<small>Copy-Paste this as authorized redirect URI</small>)
				<a class="wordform-field-help-link" href="https://support.google.com/cloud/answer/6158849" target="_blank" rel="noopener" aria-label="How to use the Google OAuth redirect URL">
					<span class="dashicons dashicons-editor-help" aria-hidden="true"></span>
				</a>
			</label><br/>
			<input type="text" id="googleAuthRedirectUrl" name="google-auth-redirect-url" class="google-auth-redirect-url" value="<?php echo $google_auth_redirect_url; ?>">
			<p class="wordform-connect-oauth-verification-note"><i class="fa-solid fa-circle-info"></i><span>Keep this redirect URL exactly the same in both WordForm and your Google OAuth client settings. If they do not match exactly, authorize or re-authorize will fail.</span></p>
			<?php if ( $wordform_connect_is_local_env ) : ?>
			<div class="wordform-connect-oauth-verification-note wordform-connect-oauth-verification-note-stack">
				<i class="fa-solid fa-flask"></i>
				<span class="wordform-connect-oauth-note-lines">
					<span>Localhost testing can use this exact callback.</span>
					<span>If you want to test through a public HTTPS tunnel instead, set the constant below in your WordPress config.</span>
					<code>WORDFORM_CONNECT_OAUTH_REDIRECT_URL</code>
					<code>wp-config.php</code>
				</span>
			</div>
			<?php endif; ?>
		</div>

		<div class="wordform-configure-google-credentials-save-and-authentication-btn-div-wrapper">
			<p class="wordform-connect-oauth-verification-note"><i class="fa-solid fa-shield-check"></i><span>WordForm saves these credentials first. Google verifies them during the next authorization step.</span></p>
			<button class="button button-primary wordform-configure-google-credentials-save-authentication-btn"><i class="fa-solid fa-gear"></i> Save / Update and Continue</button>
		</div>
		<?php endif; ?>
	</div>
</div>
<!-- End Google Sheet Configure Modal Form -->
