<?php
/**
 * Fixed edition metadata for the public WordPress.org Free package.
 *
 * Describes only the physically installed Free package and its public support
 * and comparison links. It contains no product-license endpoint, external
 * updater, commercial entitlement resolver or separately distributed runtime.
 */

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

// Legacy option migration: preserve existing settings across the custausu_ prefix transition.
if (false === get_option('custausu_global_settings', false)) {
  $custausu_legacy_global_settings = get_option('cas_global_settings', null);
  if (is_array($custausu_legacy_global_settings)) {
    update_option('custausu_global_settings', $custausu_legacy_global_settings, false);
  }
  unset($custausu_legacy_global_settings);
}

// Prefix migration C18: copy legacy persistent option keys to custausu_ keys once.
// Legacy values are intentionally retained for reversible rollback compatibility.
if (get_option('custausu_prefix_option_key_migration_c18_v1', '') !== '20260811_c18s1') {
    $custausu_prefix_missing = new stdClass();
    $custausu_prefix_alloptions = wp_load_alloptions();

    $custausu_prefix_option_map = [
        'cas_admin_classic_notice_dismissals' => 'custausu_admin_classic_notice_dismissals',
        'cas_doctor_assisted_support_preferences' => 'custausu_doctor_assisted_support_preferences',
        'cas_doctor_change_history' => 'custausu_doctor_change_history',
        'cas_doctor_last_snapshot' => 'custausu_doctor_last_snapshot',
        'cas_doctor_plugin_warning_acknowledgements' => 'custausu_doctor_plugin_warning_acknowledgements',
        'cas_email_verifications_existing_users_backfill' => 'custausu_email_verifications_existing_users_backfill',
        'cas_free_upgrade_card_dismissals' => 'custausu_free_upgrade_card_dismissals',
        'cas_license_instance_id' => 'custausu_license_instance_id',
        'cas_license_last_valid_state' => 'custausu_license_last_valid_state',
        'cas_remote_messages_catalog_version' => 'custausu_remote_messages_catalog_version',
        'cas_remote_messages_client_preferences' => 'custausu_remote_messages_client_preferences',
        'cas_remote_messages_dismissals' => 'custausu_remote_messages_dismissals',
        'cas_review_notice_started_at' => 'custausu_review_notice_started_at',
        'cas_setup_completed' => 'custausu_setup_completed',
        'cas_setup_completed_at' => 'custausu_setup_completed_at',
        'cas_support_diagnostics_client_revoke_queue' => 'custausu_support_diagnostics_client_revoke_queue',
        'cas_support_diagnostics_client_revoke_retry' => 'custausu_support_diagnostics_client_revoke_retry',
        'cas_support_diagnostics_local_revocations' => 'custausu_support_diagnostics_local_revocations',
        'cas_support_diagnostics_runtime_logs' => 'custausu_support_diagnostics_runtime_logs',
        'cas_wizard_completed' => 'custausu_wizard_completed',
        'cas_wizard_completed_at' => 'custausu_wizard_completed_at',
        'cas_wizard_state' => 'custausu_wizard_state',
    ];

    foreach ($custausu_prefix_option_map as $custausu_prefix_old => $custausu_prefix_new) {
        $custausu_prefix_new_value = get_option($custausu_prefix_new, $custausu_prefix_missing);

        if ($custausu_prefix_new_value !== $custausu_prefix_missing) {
            continue;
        }

        $custausu_prefix_old_value = get_option($custausu_prefix_old, $custausu_prefix_missing);

        if ($custausu_prefix_old_value === $custausu_prefix_missing) {
            continue;
        }

        $custausu_prefix_autoload = array_key_exists(
            $custausu_prefix_old,
            $custausu_prefix_alloptions
        ) ? 'yes' : 'no';

        add_option(
            $custausu_prefix_new,
            $custausu_prefix_old_value,
            '',
            $custausu_prefix_autoload
        );
    }

    $custausu_prefix_settings = get_option('custausu_global_settings', []);
    $custausu_prefix_settings_changed = false;

    if (is_array($custausu_prefix_settings)) {
        $custausu_prefix_subkey_map = [
        'cas_doctor_assisted_support_enabled' => 'custausu_doctor_assisted_support_enabled',
        'cas_rm_analytics_enabled' => 'custausu_rm_analytics_enabled',
        'cas_rm_messages_enabled' => 'custausu_rm_messages_enabled',
    ];

        foreach ($custausu_prefix_subkey_map as $custausu_prefix_old => $custausu_prefix_new) {
            if (
                array_key_exists($custausu_prefix_old, $custausu_prefix_settings)
                && !array_key_exists($custausu_prefix_new, $custausu_prefix_settings)
            ) {
                $custausu_prefix_settings[$custausu_prefix_new] =
                    $custausu_prefix_settings[$custausu_prefix_old];

                $custausu_prefix_settings_changed = true;
            }
        }

        if ($custausu_prefix_settings_changed) {
            update_option('custausu_global_settings', $custausu_prefix_settings, false);
        }
    }

    update_option('custausu_prefix_option_key_migration_c18_v1', '20260811_c18s1', false);

    unset(
        $custausu_prefix_missing,
        $custausu_prefix_alloptions,
        $custausu_prefix_option_map,
        $custausu_prefix_new_value,
        $custausu_prefix_old_value,
        $custausu_prefix_autoload,
        $custausu_prefix_settings,
        $custausu_prefix_settings_changed,
        $custausu_prefix_subkey_map,
        $custausu_prefix_old,
        $custausu_prefix_new
    );
}



if (!function_exists('custausu_edition_allowed_values')) {
  function custausu_edition_allowed_values(): array {
    return ['freemium_wporg'];
  }
}

if (!function_exists('custausu_edition_default')) {
  function custausu_edition_default(): string {
    return 'freemium_wporg';
  }
}

if (!function_exists('custausu_edition_sanitize')) {
  function custausu_edition_sanitize($edition): string {
    unset($edition);
    return 'freemium_wporg';
  }
}

if (!function_exists('custausu_edition')) {
  function custausu_edition(): string {
    return 'freemium_wporg';
  }
}

if (!function_exists('custausu_build_channel')) {
  function custausu_build_channel(): string {
    return 'wordpress_org_free';
  }
}

if (!function_exists('custausu_is_public_build')) {
  function custausu_is_public_build(): bool {
    return true;
  }
}

if (!function_exists('custausu_is_commercial_build')) {
  function custausu_is_commercial_build(): bool {
    return false;
  }
}

if (!function_exists('custausu_is_special_build')) {
  function custausu_is_special_build(): bool {
    return false;
  }
}

if (!function_exists('custausu_edition_label')) {
  function custausu_edition_label(?string $edition = null): string {
    unset($edition);
    return 'Free';
  }
}

if (!function_exists('custausu_edition_manifest')) {
  function custausu_edition_manifest(): array {
    return [
      'edition' => 'freemium_wporg',
      'package_plan' => 'freemium',
      'label' => 'Free',
      'support_url' => function_exists('custausu_build_url')
        ? custausu_build_url('CUSTAUSU_BUILD_SUPPORT_URL')
        : '',
      'upgrade_url' => function_exists('custausu_build_url')
        ? custausu_build_url('CUSTAUSU_BUILD_UPGRADE_URL')
        : '',
      'external_support_service' => true,
    ];
  }
}

if (!function_exists('custausu_edition_meta')) {
  function custausu_edition_meta(string $key, $default = null) {
    $manifest = custausu_edition_manifest();
    return array_key_exists($key, $manifest) ? $manifest[$key] : $default;
  }
}

if (!function_exists('custausu_edition_support_url')) {
  function custausu_edition_support_url(): string {
    return (string)custausu_edition_meta('support_url', '');
  }
}

if (!function_exists('custausu_edition_upgrade_url')) {
  function custausu_edition_upgrade_url(): string {
    return (string)custausu_edition_meta('upgrade_url', '');
  }
}

if (!function_exists('custausu_edition_bootstrap')) {
  function custausu_edition_bootstrap(): void {
    // The public Free package has no external edition bootstrap.
  }
}

if (!function_exists('custausu_edition_debug_info')) {
  function custausu_edition_debug_info(): array {
    return [
      'edition' => 'freemium_wporg',
      'label' => 'Free',
      'package_plan' => 'freemium',
      'build_channel' => 'wordpress_org_free',
      'external_support_service' => true,
    ];
  }
}
