<?php
/**
 * Uninstall - clean up database and notify STW platform
 *
 * @package Scanshow
 */

// If uninstall.php is not called by WordPress, die.
if ( ! defined( 'WP_UNINSTALL_PLUGIN' ) ) {
	die;
}

// Load required classes.
require_once plugin_dir_path( __FILE__ ) . 'includes/class-database.php';
require_once plugin_dir_path( __FILE__ ) . 'includes/class-api.php';

// Notify STW platform about uninstall (removes shop link, downgrades subscription).
// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Variable is properly prefixed with scanshow_.
$scanshow_result = Scan_Show_API::remove_shop_connection();
if ( ! isset( $scanshow_result['success'] ) || ! $scanshow_result['success'] ) {
	// Only log errors when WP_DEBUG is enabled.
	if ( defined( 'WP_DEBUG' ) && WP_DEBUG ) {
		// phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedVariableFound -- Variable is properly prefixed with scanshow_.
		$scanshow_error = isset( $scanshow_result['error'] ) ? $scanshow_result['error'] : 'Unknown error';
		// phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- Debug logging only when WP_DEBUG is true.
		error_log( '3D Scan & Show: Failed to remove shop connection during uninstall. Error: ' . $scanshow_error );
	}
}

// Drop the STW connection table.
Scan_Show_Database::drop_table();
