<?php

namespace WeDevs\AppBridge\Integrations\Dokan;

class DokanProLicenseVerification extends LicenseManager {
	/**
	 * Verification-only manager: skip admin notice registration so
	 * Dokan Pro license state does not surface a duplicate notice.
	 *
	 * Watches Dokan Pro's own `dokan_pro_license` option for changes so the
	 * cached mobile-app verification data is invalidated immediately when the
	 * license is deactivated or expires, instead of serving stale
	 * "license valid" responses until the transient expires. Hooking the option
	 * write keeps this self-contained inside App Bridge — no Dokan Pro change
	 * is required.
	 */
	public function register_hooks(): void {
		add_action( 'update_option_dokan_pro_license', [ $this, 'clear_dokan_pro_verification_cache' ], 10, 2 );
	}

	/**
	 * Clear the cached Dokan Pro license verification data when the license
	 * option changes (e.g. on deactivation).
	 *
	 * Fires on `update_option_dokan_pro_license`, which passes the previous and
	 * new option values. The previously stored key is used to compute the cache
	 * key because the verification transient was stored under the key that was
	 * active at the time — the new value may have an empty key after deactivation.
	 *
	 * @param mixed $old_value The option value before the update.
	 * @param mixed $new_value The option value after the update.
	 * @return void
	 */
	public function clear_dokan_pro_verification_cache( $old_value = [], $new_value = [] ): void {
		$old_key = is_array( $old_value ) ? (string) ( $old_value['key'] ?? '' ) : '';

		$this->clear_verification_cache_data( $this->get_mobile_app_name(), $old_key );
	}

	/**
	 * @inheritDoc
	 *
	 * @return string
	 */
	protected function get_mobile_app_name(): string {
		return 'dokan-pro';
	}

	/**
	 * Persist/read license data from Dokan Pro's own option so this manager's
	 * `get_license()`, `is_valid()` and `get_license_key()` reflect the real
	 * Dokan Pro license state — the same as every other license check — instead
	 * of an app-bridge-namespaced option that is never written.
	 *
	 * @return string
	 */
	protected function option_key(): string {
		return 'dokan_pro_license';
	}

	/**
	 * Verification-only: Dokan Pro owns its own license lifecycle. Block
	 * activation so this manager can never write the shared `dokan_pro_license`
	 * option (it reads that option for verification purposes only).
	 *
	 * @param string $license_key Unused.
	 * @return array{success: bool, message: string}
	 */
	public function activate( string $license_key ): array {
		return [
			'success' => false,
			'message' => __( 'The Dokan Pro license is managed by Dokan Pro.', 'app-bridge' ),
		];
	}

	/**
	 * Verification-only: see {@see self::activate()}. Block deactivation so this
	 * manager can never wipe the shared `dokan_pro_license` option.
	 *
	 * @return array{success: bool, message: string}
	 */
	public function deactivate(): array {
		return [
			'success' => false,
			'message' => __( 'The Dokan Pro license is managed by Dokan Pro.', 'app-bridge' ),
		];
	}
}
