// Add settings page
add_action('admin_menu', function () {
  add_options_page(
    'Menu Schema Cache',
    'Menu Schema Cache',
    'manage_options',
    'menu-schema-cache',
    'render_menu_schema_cache_page'
  );
});

// Register setting
add_action('admin_init', function () {
  register_setting('menu_schema_cache_group', 'menu_schema_cache_enabled');
});

// Render settings page
function render_menu_schema_cache_page() {
  $enabled = get_option('menu_schema_cache_enabled', '1');
  $message = '';

  // Handle clear cache action
  if (isset($_POST['clear_menu_schema_cache']) && check_admin_referer('clear_menu_schema_cache_action')) {
    delete_transient('bali_eco_deli_menu_schema');
    $message = 'Menu schema cache cleared successfully.';
  }
  ?>
  <div class="wrap">
    <h1>Menu Schema Cache</h1>

    <?php if ($message): ?>
      <div class="notice notice-success is-dismissible">
        <p><?php echo esc_html($message); ?></p>
      </div>
    <?php endif; ?>

    <form method="post" action="options.php" style="margin-bottom:20px;">
      <?php settings_fields('menu_schema_cache_group'); ?>
      <table class="form-table">
        <tr>
          <th scope="row">Enable Caching</th>
          <td>
            <label>
              <input type="checkbox" name="menu_schema_cache_enabled" value="1" <?php checked($enabled, '1'); ?>>
              Use cached menu schema (recommended for production)
            </label>
          </td>
        </tr>
      </table>
      <?php submit_button('Save Settings'); ?>
    </form>

    <form method="post">
      <?php wp_nonce_field('clear_menu_schema_cache_action'); ?>
      <p>
        <input type="submit" name="clear_menu_schema_cache" class="button button-secondary" value="Clear Cache Now">
      </p>
    </form>
  </div>
  <?php
}
