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

/**
 * Render the main page
 */
function content_craft_ai_render_main_page()
{
  // Get auth status - for the main auth page, we don't need to check
  // The auth system will handle redirects if needed
  $user = null;

  // We always show the auth page container - the React app will handle the rest
  // Ensure the script is enqueued
  wp_enqueue_script('content-craft-ai-admin');

  // Add data as JSON in window object
  wp_localize_script(
    'content-craft-ai-admin',
    'contentCraftAIData',
    array_merge(
      (array) wp_scripts()->get_data('content-craft-ai-admin', 'data'),
      array(
        'page' => 'auth',
        'user' => null,
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading view parameter from URL for navigation, not processing form data
        'authView' => isset($_GET['view']) ? sanitize_text_field(wp_unslash($_GET['view'])) : 'login',
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading auth_expired parameter from URL for display purposes, not processing form data
        'authExpired' => isset($_GET['auth_expired']) ? true : false,
        // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Reading manual_logout parameter from URL for display purposes, not processing form data
        'manualLogout' => isset($_GET['manual_logout']) ? true : false,
        'wpApiUrl' => rest_url('content-craft-ai/v1'),
        'nonce' => wp_create_nonce('wp_rest'),
        'siteUrl' => get_site_url(),
        'adminUrl' => admin_url()
      )
    )
  );

  // Render the app container
  echo '<div id="content-craft-ai-app"></div>';
}
