<?php
/**
 * Main plugin controller (singleton)
 *
 * @package WooAttioSync
 */

declare( strict_types=1 );

defined( 'ABSPATH' ) || exit;

/**
 * Main plugin controller (singleton)
 */
final class WooAttioSync {

	/** @var WooAttioSync|null */
	private static ?WooAttioSync $instance = null;

	private WooAttioSync_Admin_Settings $admin_settings;
	private WooAttioSync_Sync_Queue $sync_queue;
	private WooAttioSync_Bulk_Sync $bulk_sync;

	private function __construct() {
		$this->init();
	}

	/**
	 * Return the singleton instance
	 */
	public static function get_instance(): WooAttioSync {
		if ( null === self::$instance ) {
			self::$instance = new self();
		}
		return self::$instance;
	}

	/**
	 * Initialise sub-components and register hooks
	 */
	private function init(): void {
		$this->admin_settings = new WooAttioSync_Admin_Settings();
		$this->sync_queue     = new WooAttioSync_Sync_Queue();
		$this->bulk_sync      = new WooAttioSync_Bulk_Sync();
		$this->bulk_sync->register();
		add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_assets' ) );

		if ( class_exists( 'WooAttioSync_Pro' ) ) {
			WooAttioSync_Pro::init();
		}
	}

	/**
	 * Enqueue CSS and JS only on this plugin's admin pages
	 *
	 * @param string $hook_suffix The current admin page hook suffix
	 */
	public function enqueue_admin_assets( string $hook_suffix ): void {
		if ( ! str_contains( $hook_suffix, 'wcattio' ) ) {
			return;
		}

		wp_enqueue_style(
			'harbor-attio-sync-admin',
			WCATTIO_PLUGIN_URL . 'assets/admin.css',
			array(),
			WCATTIO_VERSION
		);

		wp_enqueue_script(
			'harbor-attio-sync-admin',
			WCATTIO_PLUGIN_URL . 'assets/admin.js',
			array( 'jquery' ),
			WCATTIO_VERSION,
			true
		);

		wp_localize_script(
			'harbor-attio-sync-admin',
			'wcattio',
			array(
				'ajax_url'              => admin_url( 'admin-ajax.php' ),
				'nonce'                 => wp_create_nonce( 'wcattio_admin' ),
				'dismiss_nonce'         => wp_create_nonce( 'wcattio_dismiss_onboarding' ),
				'scheduled_actions_url' => admin_url( 'admin.php?page=wc-status&tab=action-scheduler&s=wcattio' ),
				// Plain __() — these are injected via JS textContent, so esc_html__ here
				// would double-encode entities (e.g. a translated "&") into mojibake.
				'i18n'                  => array(
					'validating'    => __( 'Validating…', 'harbor-sync-with-attio-for-woocommerce' ),
					'valid'         => __( 'API key is valid', 'harbor-sync-with-attio-for-woocommerce' ),
					'invalid'       => __( 'API key is invalid', 'harbor-sync-with-attio-for-woocommerce' ),
					'sync_started'  => __( 'Sync queued!', 'harbor-sync-with-attio-for-woocommerce' ),
					'error'         => __( 'An error occurred', 'harbor-sync-with-attio-for-woocommerce' ),
					'saving'        => __( 'Saving…', 'harbor-sync-with-attio-for-woocommerce' ),
					'save'          => __( 'Save Settings', 'harbor-sync-with-attio-for-woocommerce' ),
					'view_progress' => __( 'View progress in Scheduled Actions', 'harbor-sync-with-attio-for-woocommerce' ),
				),
			)
		);
	}

	/** Prevent cloning of the singleton */
	private function __clone() {}
}
