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

add_action('admin_menu', function () {
  add_menu_page(
    'WebAbility Accessibility Widget', // Page title
    'WebAbility', // Menu title
    'manage_options', // Capability
    'webability', // Menu slug
    'webability_acwg_render_admin_page', // Function
    'dashicons-universal-access', // Icon (accessibility icon)
    30 // Position (after Comments)
  );
});

// Hide default WP admin footer on our page
add_filter('admin_footer_text', function($text) {
  $screen = get_current_screen();
  return ($screen && $screen->id === 'toplevel_page_webability') ? '' : $text;
});
add_filter('update_footer', function($text) {
  $screen = get_current_screen();
  return ($screen && $screen->id === 'toplevel_page_webability') ? '' : $text;
}, 11);

add_action('admin_enqueue_scripts', function($hook){
  if ($hook !== 'toplevel_page_webability') return;
  wp_enqueue_script('webability-acwg-admin', plugins_url('../wa-admin.js', __FILE__), ['jquery'], WEBABILITY_WIDGET_VERSION, true);
  wp_localize_script('webability-acwg-admin', 'WEBABILITY_ACWG_VERIFY', [
    'ajax' => admin_url('admin-ajax.php'),
    'nonce' => wp_create_nonce('webability_acwg_verify_nonce'),
    'verified' => (int) get_option('webability_verified', 0),
    // Diagnostic reporting config (GlitchTip). Public ingest key only; no PII.
    'report' => function_exists('webability_acwg_reporter_config') ? webability_acwg_reporter_config() : ['enabled' => false],
    'site_host' => wp_parse_url(home_url(), PHP_URL_HOST),
    'plugin_version' => WEBABILITY_WIDGET_VERSION,
  ]);
  // Scanner JS disabled for now
  // wp_enqueue_script('webability-acwg-scanner', plugins_url('../wa-admin-scanner.js', __FILE__), [], WEBABILITY_WIDGET_VERSION, true);
  // wp_localize_script('webability-acwg-scanner', 'WEBABILITY_SCANNER', [
  //   'ajax'  => admin_url('admin-ajax.php'),
  //   'nonce' => wp_create_nonce('webability_acwg_scanner_nonce'),
  // ]);
  wp_enqueue_style('webability-acwg-scanner-css', plugins_url('../wa-admin-scanner.css', __FILE__), [], WEBABILITY_WIDGET_VERSION);
});

function webability_acwg_render_admin_page() {
  if (!current_user_can('manage_options')) return;
  $active_tab = isset($_GET['tab']) ? sanitize_key($_GET['tab']) : 'settings';
  ?>
  <div class="wrap">
    <h1>WebAbility Accessibility</h1>
    <nav class="nav-tab-wrapper">
      <a href="<?php echo esc_url(admin_url('admin.php?page=webability&tab=settings')); ?>" class="nav-tab <?php echo $active_tab === 'settings' ? 'nav-tab-active' : ''; ?>">
        <?php esc_html_e('Widget Settings', 'webability-accessibility-widget'); ?>
      </a>
      <a href="<?php echo esc_url(admin_url('admin.php?page=webability&tab=design')); ?>" class="nav-tab <?php echo $active_tab === 'design' ? 'nav-tab-active' : ''; ?>">
        <?php esc_html_e('Widget Design', 'webability-accessibility-widget'); ?>
      </a>
      <!-- Scanner tab disabled for now
      <a href="<?php echo esc_url(admin_url('admin.php?page=webability&tab=scanner')); ?>" class="nav-tab <?php echo $active_tab === 'scanner' ? 'nav-tab-active' : ''; ?>">
        <?php esc_html_e('Scanner', 'webability-accessibility-widget'); ?>
      </a>
      -->
      <a href="<?php echo esc_url(admin_url('admin.php?page=webability&tab=statement')); ?>" class="nav-tab <?php echo $active_tab === 'statement' ? 'nav-tab-active' : ''; ?>">
        <?php esc_html_e('Statement', 'webability-accessibility-widget'); ?>
      </a>
    </nav>

    <?php if ($active_tab === 'settings'): ?>
    <div class="wa-tab-content">
      <?php $o = webability_acwg_get_options(); ?>
      <div id="webability-settings" class="wa-settings-wrap">
        <form method="post" action="options.php">
          <?php settings_fields('webability_settings'); ?>
          <!-- Preserve design-tab fields so they don't get wiped on save -->
          <input type="hidden" name="<?php echo esc_attr(WEBABILITY_ACWG_OPT_KEY); ?>[language]" value="<?php echo esc_attr($o['language']); ?>">
          <input type="hidden" name="<?php echo esc_attr(WEBABILITY_ACWG_OPT_KEY); ?>[trigger_size]" value="<?php echo esc_attr($o['trigger_size']); ?>">
          <input type="hidden" name="<?php echo esc_attr(WEBABILITY_ACWG_OPT_KEY); ?>[widget_color]" value="<?php echo esc_attr($o['widget_color']); ?>">
          <input type="hidden" name="<?php echo esc_attr(WEBABILITY_ACWG_OPT_KEY); ?>[icon_type]" value="<?php echo esc_attr($o['icon_type']); ?>">

          <div class="wa-settings-grid">
            <!-- Left: Settings Cards -->
            <div class="wa-settings-cards">
              <!-- Preserve position/offset values -->
              <input type="hidden" name="<?php echo esc_attr(WEBABILITY_ACWG_OPT_KEY); ?>[position]" value="<?php echo esc_attr($o['position']); ?>">
              <input type="hidden" name="<?php echo esc_attr(WEBABILITY_ACWG_OPT_KEY); ?>[offset_x]" value="<?php echo esc_attr($o['offset_x']); ?>">
              <input type="hidden" name="<?php echo esc_attr(WEBABILITY_ACWG_OPT_KEY); ?>[offset_y]" value="<?php echo esc_attr($o['offset_y']); ?>">

              <!-- Widget Status -->
              <div class="wa-design-card">
                <h3 class="wa-design-card-title">Widget Status</h3>
                <label class="wa-toggle-row">
                  <span class="wa-toggle-switch">
                    <input type="checkbox" name="<?php echo esc_attr(WEBABILITY_ACWG_OPT_KEY); ?>[enabled]" value="1" <?php checked(1, $o['enabled']); ?>>
                    <span class="wa-toggle-slider"></span>
                  </span>
                  <span class="wa-toggle-text"><?php echo $o['enabled'] ? 'Widget is <strong>active</strong>' : 'Widget is <strong>disabled</strong>'; ?></span>
                </label>
              </div>

              <button type="submit" class="button button-primary wa-btn-save-settings">Save Settings</button>
            </div>

            <!-- Right: Verify Installation -->
            <div class="wa-settings-sidebar">
              <div class="wa-design-card wa-verify-card">
                <h3 class="wa-design-card-title">Verify Installation</h3>
                <p class="wa-design-card-desc">Confirm your domain is registered on WebAbility.io.</p>
                <?php $verified = get_option('webability_verified', 0); ?>
                <div class="wa-verify-status <?php echo $verified ? 'wa-verify-success' : ''; ?>" id="wa-verify-status">
                  <p class="wa-verify-msg"><?php echo $verified ? '✅ Domain verified' : 'Checking…'; ?></p>
                </div>
                <button type="button" id="webability-acwg-verify" class="button wa-btn-verify-link">
                  Re-check
                </button>
              </div>

              <div class="wa-design-card wa-quick-links">
                <h3 class="wa-design-card-title">Quick Links</h3>
                <ul class="wa-links-list">
                  <li><a href="https://app.webability.io" target="_blank" rel="noopener">WebAbility Dashboard</a></li>
                  <li><a href="https://webability.io/docs" target="_blank" rel="noopener">Documentation</a></li>
                  <li><a href="mailto:support@webability.io">Contact Support</a></li>
                </ul>
              </div>
            </div>
          </div>
        </form>
      </div>
    </div>
    <?php elseif ($active_tab === 'design'): ?>
    <div class="wa-tab-content">
      <?php webability_acwg_render_design_page(); ?>
    </div>
    <?php /* Scanner disabled for now
    elseif ($active_tab === 'scanner'): ?>
    <div class="wa-tab-content">
      <?php webability_acwg_render_scanner_page(); ?>
    </div>
    <?php */ ?>
    <?php elseif ($active_tab === 'statement'): ?>
    <div class="wa-tab-content">
      <?php webability_acwg_render_statement_page(); ?>
    </div>
    <?php endif; ?>
  </div>
  <?php
}
