<?php

/**
 * ContentCraft AI - Redirect Helpers
 * 
 * Provides proper WordPress-compliant methods for handling redirects
 */

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

/**
 * Safely redirect to an external URL
 * 
 * @param string $url The URL to redirect to
 * @param int $status HTTP status code
 * @return void
 */
function content_craft_ai_safe_redirect($url, $status = 302)
{
  // Always sanitize URL before redirecting
  $safe_url = esc_url_raw($url);

  // Use WordPress core function for redirecting
  wp_redirect($safe_url, $status);
  exit;
}

/**
 * Create a page that safely redirects to an external URL
 * Uses http-equiv meta refresh as fallback when JavaScript is disabled
 * 
 * @param string $url The URL to redirect to
 * @param string $message Optional message to display during redirect
 * @return void
 */
function content_craft_ai_html_redirect_page($url, $message = 'Redirecting...')
{
  // Always sanitize URL before redirecting
  $safe_url = esc_url($url);
  $safe_message = esc_html($message);

  // Output buffer to prevent any other output
  ob_start();
?>
  <!DOCTYPE html>
  <html>

  <head>
    <meta charset="UTF-8">
    <meta http-equiv="refresh" content="1;url=<?php echo esc_attr($safe_url); ?>">
    <title><?php echo esc_html($safe_message); ?></title>
    <style>
      body {
        font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen-Sans, Ubuntu, Cantarell, "Helvetica Neue", sans-serif;
        background: #f0f0f1;
        margin: 0;
        padding: 0;
        display: flex;
        justify-content: center;
        align-items: center;
        height: 100vh;
      }

      .redirect-container {
        background: #fff;
        border: 1px solid #c3c4c7;
        border-radius: 4px;
        box-shadow: 0 1px 1px rgba(0, 0, 0, 0.04);
        max-width: 500px;
        padding: 30px;
        text-align: center;
      }

      .spinner {
        display: inline-block;
        width: 40px;
        height: 40px;
        border: 4px solid rgba(0, 0, 0, 0.1);
        border-radius: 50%;
        border-top-color: #3498db;
        animation: spin 1s ease-in-out infinite;
        margin-bottom: 20px;
      }

      @keyframes spin {
        to {
          transform: rotate(360deg);
        }
      }

      p {
        margin: 10px 0;
        color: #50575e;
        font-size: 16px;
      }

      a {
        color: #2271b1;
        text-decoration: none;
      }

      a:hover {
        color: #135e96;
        text-decoration: underline;
      }
    </style>
    <script>
      // Use JS redirect as primary method (safer than meta refresh)
      window.onload = function() {
        window.location.href = "<?php echo esc_js($safe_url); ?>";
      };
    </script>
  </head>

  <body>
    <div class="redirect-container">
      <div class="spinner"></div>
      <p><?php echo esc_html($safe_message); ?></p>
      <p><a href="<?php echo esc_url($safe_url); ?>">Click here if you are not redirected automatically</a></p>
    </div>
  </body>

  </html>
<?php
  // Output the redirect page and exit
  // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- All variables are properly escaped within the HTML template
  echo ob_get_clean();
  exit;
}

/**
 * Generate a redirect response for AJAX requests
 * 
 * @param string $url The URL to redirect to
 * @param string $message Optional message to display
 * @return array Response data for wp_send_json_success
 */
function content_craft_ai_ajax_redirect_response($url, $message = 'Redirecting...')
{
  return array(
    'success' => true,
    'redirect' => true,
    'url' => esc_url_raw($url),
    'message' => esc_html($message)
  );
}
