<?php
/**
 * Adapter for "Sync Master Sheet - Product Sync with Google Sheet for
 * WooCommerce" (CodeAstrology).
 *
 * Option shapes below were confirmed against the free plugin's source
 * (product-sync-master-sheet, v2.0.4 at audit time; see
 * .superpowers/sdd/competitor-productsyncmaster.md):
 *  - `pssg_settings` is a serialized associative array holding setup-wizard
 *    data: `sheet_url` (raw pasted URL), `spreadsheet_id` (bare ID, parsed
 *    server-side from the URL, app/handle/form-submit.php:148-166),
 *    `sheet_name` (worksheet name), plus wizard-progress flags. The plugin's
 *    own "setup complete" check requires all three of `sheet_url`,
 *    `spreadsheet_id`, `sheet_name` non-empty
 *    (admin/admin-loader.php:183-201), which this adapter mirrors for
 *    is_present().
 *  - This plugin syncs Products AND Variations only — confirmed no Orders,
 *    Coupons, or Customers resource exists anywhere (post_type filter is
 *    literally `['product', 'product_variation']`,
 *    app/service/products.php:227). There is no boolean toggle to disable
 *    either resource, so a present config always maps to both
 *    `sync_products` and `sync_variations`. StoreSheet only ships Variations
 *    sync in the Pro build, so the adapter adds an explicit warning.
 *  - `pssg_service_json_data` stores the merchant's own uploaded Google
 *    service-account JSON verbatim (private RSA key included), used for a
 *    JWT-bearer 2-legged auth flow — structurally incompatible with
 *    StoreSheet's user-consent OAuth `GoogleOAuthClient`. It is never read
 *    here; the user reconnects Google through StoreSheet's own flow instead.
 *
 * @package StoreSheet
 */

declare( strict_types=1 );

namespace StoreSheet\Importer\Adapters;

defined( 'ABSPATH' ) || exit;

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

final class ProductSyncMasterAdapter implements SourceAdapter {

	private const OPTION_SETTINGS = 'pssg_settings';

	public function id(): string {
		return 'product-sync-master';
	}

	public function label(): string {
		return __( 'Product Sync Master Sheet', 'storesheet' );
	}

	public function plugin_files(): array {
		return array( 'product-sync-master-sheet/product-sync-master-sheet.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;
			}
		}

		$settings = get_option( self::OPTION_SETTINGS, array() );
		if ( ! is_array( $settings ) ) {
			return false;
		}
		return '' !== (string) ( $settings['spreadsheet_id'] ?? '' ) && '' !== (string) ( $settings['sheet_name'] ?? '' );
	}

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

		$settings       = get_option( self::OPTION_SETTINGS, array() );
		$settings       = is_array( $settings ) ? $settings : array();
		$spreadsheet_id = (string) ( $settings['spreadsheet_id'] ?? '' );

		$spreadsheet = null;
		if ( '' !== $spreadsheet_id ) {
			$sheet_url   = (string) ( $settings['sheet_url'] ?? '' );
			$spreadsheet = array(
				'id'  => $spreadsheet_id,
				'url' => '' !== $sheet_url ? $sheet_url : 'https://docs.google.com/spreadsheets/d/' . $spreadsheet_id . '/edit',
			);
		}

		// No per-resource toggle exists — Products and Variations are always
		// synced together in this plugin. See class doc block above.
		$plan_settings = array_filter(
			array(
				'sync_products'   => true,
				'sync_variations' => true,
			),
			static function ( $v ) {
				return null !== $v;
			}
		);

		// Product Sync Master Sheet stores its own Google service-account key
		// for direct, JWT-signed Sheets API access — never a client secret
		// StoreSheet's OAuth flow could reuse.
		$credentials = null;
		$warnings[]  = __( 'Product Sync Master Sheet stores its own Google service-account key for direct Sheets API access — this can\'t be reused by StoreSheet\'s OAuth connection; connect your Google account on the next step.', 'storesheet' );
		$warnings[]  = __( 'Product Sync Master Sheet also syncs Variations — StoreSheet syncs Variations in the Pro build.', 'storesheet' );

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

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