<?php
/**
 * Global plugin-wide constants.
 *
 * Consolidates hard-coded strings (option names, statuses, cache keys, etc.)
 * so they can be referenced centrally and refactored safely.
 *
 * @package    MOMCS_Sync
 * @subpackage MOMCS_Sync\includes\common\constants
 */

declare( strict_types=1 );

namespace MOMCS_Sync\Includes\Common\Constants;

if ( ! defined( 'ABSPATH' ) ) {
	exit; // Exit if accessed directly.
}

/**
 * Plugin constants.
 */
class Plugin_Constants {

	public const PLUGIN_FILE = 'multi-channel-for-woocommerce.php';

	/**
	 * Plan name - determines which features are available.
	 * For premium builds, this should be 'premium'.
	 */
	public const PLAN_NAME = 'free';

	/**
	 * URL for upgrade / Add more payment (portal miniOrange).
	 */
	public const UPGRADE_PAYMENT_URL = 'https://portal.miniorange.com/initializePayment?requestOrigin=wp_rest_multi_channel_sync_premium_plan';

	/**
	 * DB Tables.
	 */

	public const TABLE_MULTICHANNELS   = 'momcs_multichannels';
	public const TABLE_AUTH_DETAILS    = 'momcs_authentication_details';
	public const TABLE_PRODUCT_MAPPING = 'momcs_product_mappings';

	/**
	 * DB Values.
	 */
	public const DEFAULT_MAPPING_NAME = 'default_mapping';

	/**
	 * Directories / Files.
	 */

	public const LOG_DIR_NAME = 'momcs-sync-logs';

	/**
	 * Nonces / Security.
	 */
	public const NONCE_MAIN       = 'momcs_sync_nonce';
	public const NONCE_SYNC_QUEUE = 'momcs_sync_queue_nonce';

	/**
	 * Cron / Action-Scheduler Hooks.
	 */
	public const CRON_SYNC_BULK                   = 'momcs_sync_process_bulk';
	public const CRON_CHECK_FEED_STATUS           = 'momcs_check_feed_status';
	public const CRON_UNLIST_BULK                 = 'momcs_unlist_process_bulk';
	public const CRON_FETCH_ORDERS                = 'momcs_fetch_orders_background';
	public const CRON_SCHEDULED_ORDER_SYNC        = 'momcs_scheduled_order_sync_execute';
	public const CRON_AMAZON_FULFILLMENT_DISPATCH = 'momcs_amazon_fulfillment_dispatch';
	public const CRON_AMAZON_FULFILLMENT_CHANNEL_JOB = 'momcs_amazon_fulfillment_channel_job';
	public const CRON_SYNC_SINGLE_MARKETPLACE     = 'momcs_sync_single_marketplace';
	public const CRON_UNLIST_SINGLE_MARKETPLACE   = 'momcs_unlist_single_marketplace';
	public const CRON_POLL_FEED_STATUS            = 'momcs_poll_feed_status';

	/**
	 * Batch Tracking.
	 */
	public const TRANSIENT_BATCH_PREFIX           = 'momcs_batch_';

	/**
	 * Transient Keys.
	 */
	/**
	 * Marks a feed ID that already has an upcoming cron event scheduled to poll its
	 * processing status. The feed ID is appended to ensure uniqueness per feed.
	 * Prevents accidental duplicate `wp_schedule_single_event()` calls when
	 * multiple sync operations run concurrently.
	 */
	public const TRANSIENT_FEED_STATUS_CRON_PREFIX = 'momcs_feed_status_scheduled_';
	public const TRANSIENT_ACCESS_TOKEN            = 'momcs_sync_access_token_';
	public const TRANSIENT_CURRENT_CHANNEL         = 'momcs_sync_current_channel';
	public const TRANSIENT_PRODUCT_TYPE_PREFIX     = 'momcs_sync_product_type_';
	public const TRANSIENT_PRODUCT_SCHEMA_PREFIX   = 'momcs_sync_product_schema_';

	/**
	 * Feed processing statuses (Amazon Feeds API).
	 */

	public const FEED_STATUS_SUBMITTED   = 'SUBMITTED';
	public const FEED_STATUS_IN_QUEUE    = 'IN_QUEUE';
	public const FEED_STATUS_IN_PROGRESS = 'IN_PROGRESS';
	public const FEED_STATUS_CANCELLED   = 'CANCELLED';
	public const FEED_STATUS_DONE        = 'DONE';
	public const FEED_STATUS_FATAL       = 'FATAL';

	/**
	 * Configuration.
	 */
	public const FEEDS_API_THRESHOLD = 2;

	/**
	 * Chunked Feed Building.
	 *
	 * When syncing large product sets (1000+), the feed building is split into
	 * smaller chunks processed by separate Action Scheduler actions. This prevents
	 * PHP timeouts and allows automatic resume if a chunk fails.
	 */

	/**
	 * Number of products to process per chunk during feed building.
	 */
	public const FEED_BUILD_CHUNK_SIZE = 100;

	/**
	 * Threshold for switching to chunked feed building (product count).
	 */
	public const CHUNKED_BUILD_THRESHOLD = 10;

	/**
	 * Action Scheduler hook for processing feed build chunks.
	 */
	public const CRON_BUILD_FEED_CHUNK = 'momcs_build_feed_chunk';

	/**
	 * Transient prefix for storing chunked feed build progress.
	 */
	public const TRANSIENT_FEED_BUILD_PREFIX = 'momcs_feed_build_';

	/**
	 * Maximum retry attempts for failed chunk build actions.
	 */
	public const FEED_BUILD_MAX_RETRIES = 3;

	/**
	 * Maximum retries for Amazon fulfillment channel jobs.
	 */
	public const AMAZON_FULFILLMENT_MAX_RETRIES = 3;

	/**
	 * Base retry delay (seconds) for Amazon fulfillment retries.
	 */
	public const AMAZON_FULFILLMENT_RETRY_DELAY = 60;

	/**
	 * Base delay in seconds for retry backoff (multiplied by retry count).
	 */
	public const FEED_BUILD_RETRY_DELAY = 60;

	/**
	 * OAuth / Broker.
	 */
	public const BROKER_BASE_URL       = 'https://multichannel-connect.xecurify.com';
	public const BROKER_AUTHORIZE_PATH = '/authorize';
	public const BROKER_OAUTH_CALLBACK = '/oauth-callback';
	public const BROKER_TOKEN_PATH     = '/token';

	/**
	 * Apps.
	 */

	public const AMAZON = 'amazon';

	/**
	 * Support / Contact.
	 */
	/**
	 * Remote endpoint for direct Contact Us submissions.
	 * The front-end posts JSON to this URL cross-domain.
	 */
	public const CONTACT_US_REMOTE_URL = 'https://login.xecurify.com/moas/rest/customer/contact-us';

	public const FEEDBACK_URL = 'https://login.xecurify.com/moas/api/notify/send';
	/**
	 * Default support email used for cc in Contact Us submissions.
	 */
	public const SUPPORT_EMAIL = 'apisupport@xecurify.com';

	/**
	 * Option name for tracking deactivation feedback submission.
	 */
	public const DEACTIVATION_FEEDBACK_SUBMITTED = 'momcs_deactivation_feedback_submitted';

	/**
	 * AJAX action name for deactivation feedback submission.
	 */
	public const AJAX_DEACTIVATION_FEEDBACK = 'momcs_submit_deactivation_feedback';

	/**
	 * Xecurify Account APIs.
	 */
	public const CUSTOMER_KEY_API = 'https://login.xecurify.com/moas/rest/customer/key';
	public const CUSTOMER_ADD_API = 'https://login.xecurify.com/moas/rest/customer/add';


	/**
	 * Messages.
	 */

	public const MESSAGE_AUTHORIZED                            = 'Authorized';
	public const MESSAGE_AUTHORIZATION_FAILED                  = 'Authorization failed';
	public const MESSAGE_AUTHORIZATION_SUCCESS                 = 'Authorization successful';
	public const MESSAGE_SECURITY_CHECK_FAILED                 = 'Security check failed.';
	public const MESSAGE_MISSING_REQUIRED_DATA                 = 'Missing required data for authorization.';
	public const MESSAGE_INVALID_OAUTH_CALLBACK                = 'Invalid OAuth callback parameters.';
	public const MESSAGE_AUTHORIZATION_CONTEXT_NOT_FOUND       = 'Authorization context not found. Please retry.';
	public const MESSAGE_AUTHORIZATION_FAILED_PERSIST          = 'Failed to persist authorization: ';
	public const MESSAGE_FAILED_TO_UPDATE_STEP                 = 'Failed to update step.';
	public const MESSAGE_STEP_UPDATED_SUCCESSFULLY             = 'Step updated successfully.';
	public const MESSAGE_FAILED_TO_SAVE_MAPPING                = 'Failed to save mapping data.';
	public const MESSAGE_MAPPING_SAVED_SUCCESSFULLY            = 'Mapping saved successfully.';
	public const MESSAGE_FAILED_TO_PERSIST_CHANNEL_DATA        = 'Failed to persist channel data.';
	public const MESSAGE_FAILED_TO_PERSIST_AUTHENTICATION_DATA = 'Failed to persist authentication data.';
	public const MESSAGE_TOKEN_EXCHANGE_FAILED                 = 'Token exchange failed: ';
	public const MESSAGE_TOKEN_EXCHANGE_ERROR                  = 'Token exchange error.';
	public const MESSAGE_MISSING_REFRESH_TOKEN                 = 'Missing refresh token in response.';
	public const MESSAGE_FAILED_TO_UPDATE_SELLER_ID            = 'Failed to update seller ID.';
	public const MESSAGE_CHANNEL_NOT_FOUND                     = 'Channel not found.';

	/**
	 * Order meta keys for outbound Amazon fulfillment.
	 */
	public const ORDER_META_AMAZON_FULFILLMENT = '_momcs_amazon_fulfillment';
}
