<?php
// If uninstall not called from WordPress, exit.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
    exit;
}

update_option( 'paycal_connector_uninstall_diagnostics', array(
    'time' => current_time( 'mysql' ),
    'plugin' => defined( 'WP_UNINSTALL_PLUGIN' ) ? WP_UNINSTALL_PLUGIN : '',
    'removed_data' => get_option( 'paycal_remove_data_on_uninstall' ) === 'yes' ? 'yes' : 'no',
    'connector_settings_existed' => is_array( get_option( 'paycal_connector_settings', array() ) ) && ! empty( get_option( 'paycal_connector_settings', array() ) ) ? 'yes' : 'no',
    'license_data_existed' => is_array( get_option( 'paycal_license_data', array() ) ) && ! empty( get_option( 'paycal_license_data', array() ) ) ? 'yes' : 'no',
), false );

// Preserve merchant connection and invoice data by default.
// Some WordPress replacement flows can run uninstall when a plugin folder/basename changes.
// Removing these options would disconnect an existing PayCal merchant during an update.
if ( get_option( 'paycal_remove_data_on_uninstall' ) === 'yes' ) {
    delete_option( 'paycal_connector_settings' );
    delete_option( 'paycal_connector_settings_version' );
    delete_option( 'paycal_connector_plugin_basename' );
    delete_option( 'paycal_connector_update_diagnostics' );
    delete_option( 'paycal_connector_last_sync' );
    delete_option( 'paycal_license_data' );
    delete_option( 'paycal_bit_settings' );

    // Legacy invoices options
    delete_option( 'asp_invoice_number' );
    delete_option( 'paycal_invoice_settings' );
}

// Clear scheduled events
if ( function_exists( 'wp_clear_scheduled_hook' ) ) {
    wp_clear_scheduled_hook( 'paycal_license_revalidate' );
}
