<?php
/**
 * Adapter for "GSheetConnector for WooCommerce" (wc-gsheetconnector).
 *
 * Option shapes below were confirmed against the free plugin's source (SVN
 * trunk, plugins.svn.wordpress.org/wc-gsheetconnector/trunk):
 *  - `wcgsc_settings` holds the currently selected Google Spreadsheet ID as a
 *    plain string — it is saved verbatim from the "wcgsc-sheet-id" dropdown
 *    and used directly as the Sheets API spreadsheet id elsewhere in the
 *    plugin, so no unwrapping is needed.
 *  - `wcgsc_order_states` holds the WooCommerce order statuses the plugin is
 *    configured to push, as a flat array of status slugs. The free build only
 *    ever syncs Orders — Products/Coupons sync is a Pro-only feature with no
 *    corresponding option in free, so this adapter never invents
 *    `sync_products` / `sync_coupons` settings keys.
 *  - Google auth is brokered through GSheetConnector's own hosted service:
 *    `wcgsc_api_free_creds` (client_id_web/client_secret_web) is fetched from
 *    the vendor's remote API and exchanged against the vendor's own
 *    redirect_uri (oauth.gsheetconnector.com), never a client secret this site
 *    owns. It is therefore never surfaced as importable credentials.
 *
 * @package StoreSheet
 */

declare( strict_types=1 );

namespace StoreSheet\Importer\Adapters;

defined( 'ABSPATH' ) || exit;

use StoreSheet\Importer\ImportPlan;
use StoreSheet\Importer\SourceAdapter;

final class GSheetConnectorAdapter implements SourceAdapter {

	private const OPTION_SPREADSHEET  = 'wcgsc_settings';
	private const OPTION_ORDER_STATES = 'wcgsc_order_states';

	public function id(): string {
		return 'gsheetconnector';
	}

	public function label(): string {
		return __( 'GSheetConnector for WooCommerce', 'storesheet' );
	}

	public function plugin_files(): array {
		return array( 'wc-gsheetconnector/wc-gsheetconnector.php' );
	}

	public function is_present(): bool {
		if ( ! function_exists( 'is_plugin_active' ) ) {
			require_once ABSPATH . 'wp-admin/includes/plugin.php';
		}
		foreach ( $this->plugin_files() as $file ) {
			if ( is_plugin_active( $file ) ) {
				return true;
			}
		}
		return '' !== (string) get_option( self::OPTION_SPREADSHEET, '' );
	}

	public function read_config(): ImportPlan {
		$warnings = array();

		$spreadsheet    = null;
		$spreadsheet_id = (string) get_option( self::OPTION_SPREADSHEET, '' );
		if ( '' !== $spreadsheet_id ) {
			$spreadsheet = array(
				'id'  => $spreadsheet_id,
				'url' => 'https://docs.google.com/spreadsheets/d/' . $spreadsheet_id . '/edit',
			);
		}

		$settings = array_filter(
			array(
				'sync_orders' => $this->truthy( get_option( self::OPTION_ORDER_STATES, null ) ),
			),
			static function ( $v ) {
				return null !== $v;
			}
		);

		// GSheetConnector free never stores a Google client secret this site
		// owns — see class doc block above. Always null, always warn.
		$credentials = null;
		$warnings[]  = __( 'GSheetConnector authenticates through its own hosted Google connection rather than a locally stored client secret — connect your Google account on the next step.', 'storesheet' );

		return new ImportPlan( $this->id(), $this->label(), $credentials, $spreadsheet, $settings, array(), $warnings );
	}

	public function supports_sheet_adopt(): bool {
		return true;
	}

	/**
	 * Map a competitor value to bool, or null when the key is absent.
	 * Arrays (e.g. enabled-order-status lists) are truthy when non-empty.
	 *
	 * @param mixed $value Raw option value.
	 */
	private function truthy( $value ): ?bool {
		if ( null === $value ) {
			return null;
		}
		if ( is_array( $value ) ) {
			return array() !== $value;
		}
		return in_array( $value, array( 'yes', '1', 1, true, 'true', 'on' ), true );
	}
}
