<?php
/**
 * Template for key renewal process.
 *
 * @package Postomatic
 * @subpackage Admin
 */

namespace Postomatic\Admin;

use Postomatic\Core\Activator;

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

// manual session reset.
if (
	( isset( $_GET['session'] ) && 'reset' === $_GET['session'] ) || ( isset( $_GET['session'] ) && ! isset( $_GET['try'] ) )
) {
	$transient_key = 'postomatic_tries_' . get_current_user_id();
	delete_transient( $transient_key ); // Delete the transient to reset tries.

	?>
	<script>
	setTimeout(function() {
		window.location.href = "<?php echo '?page=' . esc_js( \Postomatic\Constants::PAGE_MAIN ) . '&key=refresh&try=0'; ?>";
	}, 5000);
	</script>
	<?php
}

// Delay page to receive data from api.
if ( isset( $_GET['key'] ) && 'refresh' === $_GET['key'] && isset( $_GET['_wpnonce'] ) && wp_verify_nonce( sanitize_key( $_GET['_wpnonce'] ), 'postomatic_admin_nonce' ) ) {

	Activator::activate();

	$current_url  = isset( $_SERVER['REQUEST_URI'] ) ? wp_unslash( sanitize_url( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : '';
	$query_params = array();
	wp_parse_url( $current_url, PHP_URL_QUERY, $query_params );
	unset( $query_params['key'] );
	$new_url = strtok( $current_url, '?' ) . ( count( $query_params ) ? '?' . http_build_query( $query_params ) : '' );

	$tries = 0;
	if ( ! session_id() ) {
		session_start();
	}

	// Use WordPress transients instead of sessions.
	$transient_key = 'postomatic_tries_' . get_current_user_id();
	$tries         = (int) get_transient( $transient_key );

	if ( 0 === $tries ) {
		$tries = 1;
	} else {
		++$tries;
	}

	set_transient( $transient_key, $tries, 2 * MINUTE_IN_SECONDS );

	if ( $tries > 5 ) {
		?>
		<div class="postomatic-error-message">
			<h3>API Key Renewal Error</h3>
			<p>We apologize, but there was an issue renewing your API key. Please try again later or contact our support
				team for assistance.</p>
			<p class="postomatic-support-link">
				<a href="admin.php?page=<?php echo esc_html( \Postomatic\Constants::PAGE_SUPPORT ); ?>" class="button button-primary">
					<span class="dashicons dashicons-admin-users"></span> Contact Support
				</a>
			</p>
		</div>
		<style>
			.postomatic-error-message {
				background: #fff;
				border-left: 4px solid #dc3232;
				box-shadow: 0 1px 1px rgba(0, 0, 0, .04);
				margin: 20px 0;
				padding: 12px 20px;
			}

			.postomatic-error-message h3 {
				margin: 0 0 10px;
				font-size: 16px;
				line-height: 1.4;
				color: #23282d;
			}

			.postomatic-error-message p {
				margin: 0 0 10px;
				color: #646970;
				font-size: 14px;
				line-height: 1.5;
			}

			.postomatic-support-link {
				margin-top: 15px !important;
			}

			.postomatic-support-link .button {
				display: inline-flex;
				align-items: center;
				gap: 5px;
			}
		</style>
		<?php
		exit();
	}
	?>


	<h2>Renewing Your <span class="postomatic-brand">Postomatic</span> API Key</h2>
	<p>Thank you for using <span class="postomatic-brand">Postomatic</span>. We're currently processing your key renewal
		request. This will only take a
		moment.</p>
	<div class="postomatic-loader"></div>
	<style>
		h2 {
			font-size: 28px;
			color: #23282d;
			font-weight: 600;
			margin-bottom: 1em;
			font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
		}

		p {
			font-size: 16px;
			color: #50575e;
			line-height: 1.5;
			margin-bottom: 2em;
		}

		.postomatic-brand {
			color: #0073aa;
			font-weight: 700;
			letter-spacing: 0.5px;
		}

		.postomatic-loader {
			border: 4px solid #f3f3f3;
			border-radius: 50%;
			border-top: 4px solid #0073aa;
			width: 40px;
			height: 40px;
			animation: spin 1s linear infinite;
		}

		@keyframes spin {
			0% {
				transform: rotate(0deg);
			}
			100% {
				transform: rotate(360deg);
			}
		}
	</style>
	<script>
		setTimeout(function() {
			window.location.href = "<?php echo esc_url( $new_url ) . '?page=' . esc_js( \Postomatic\Constants::PAGE_MAIN ) . '&try=' . esc_js( $tries ); ?>";
		}, 5000); // 3000ms (3 seconds) delay before redirect
	</script>
	<?php

	exit();
}
