<?php

// Shopify → HubSpot webhook integration (orders/paid).
// Prefer environment variables in production.

if (!defined('SHOPIFY_WEBHOOK_SECRET')) {
    define('SHOPIFY_WEBHOOK_SECRET', getenv('SHOPIFY_WEBHOOK_SECRET') ?: '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');
}

if (!defined('SHOPIFY_ADMIN_API_ACCESS_TOKEN')) {
    define('SHOPIFY_ADMIN_API_ACCESS_TOKEN', getenv('SHOPIFY_ADMIN_API_ACCESS_TOKEN') ?: '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');
}

if (!defined('SHOPIFY_SHOP_DOMAIN')) {
    // e.g. your-store.myshopify.com (no https://)
    define('SHOPIFY_SHOP_DOMAIN', getenv('SHOPIFY_SHOP_DOMAIN') ?: 'g1hj10-vk.myshopify.com');
}

if (!defined('SHOPIFY_ADMIN_API_VERSION')) {
    define('SHOPIFY_ADMIN_API_VERSION', getenv('SHOPIFY_ADMIN_API_VERSION') ?: '2024-10');
}

if (!defined('SHOPIFY_REQUIRED_PRODUCT_TYPE')) {
    // All line items' products must have this product_type (case-insensitive). "hardwear" → use hardware or set env.
    define('SHOPIFY_REQUIRED_PRODUCT_TYPE', getenv('SHOPIFY_REQUIRED_PRODUCT_TYPE') ?: 'Hardware');
}

if (!defined('SHOPIFY_HARDWARE_PAYMENT_GATEWAY_NAME')) {
    // If ALL items are hardware and the order used this payment method, set the special stage.
    // Example from Shopify UI: "Bank Deposit (ACH Payment)"
    define('SHOPIFY_HARDWARE_PAYMENT_GATEWAY_NAME', getenv('SHOPIFY_HARDWARE_PAYMENT_GATEWAY_NAME') ?: 'Bank Deposit (ACH Payment)');
}

if (!defined('SHOPIFY_ACH_PAYMENT_GATEWAY_NAME')) {
    // If the order used this gateway name, we'll create a Stripe checkout link for later payment collection.
    // Example from Shopify UI: "ACH Bank Deposit"
    // Fallback to the existing hardware ACH gateway name so older envs keep working.
    $v = getenv('SHOPIFY_ACH_PAYMENT_GATEWAY_NAME');
    if ($v === false || $v === '') {
        $v = defined('SHOPIFY_HARDWARE_PAYMENT_GATEWAY_NAME') ? SHOPIFY_HARDWARE_PAYMENT_GATEWAY_NAME : 'ACH Bank Deposit';
    }
    define('SHOPIFY_ACH_PAYMENT_GATEWAY_NAME', $v);
}

// Max seconds to wait for HubSpot Order to appear (Shopify→HubSpot sync can exceed 2 minutes).
// Override via env, e.g. SHOPIFY_HUBSPOT_ORDER_SYNC_MAX_WAIT_SECONDS=900
if (!defined('SHOPIFY_HUBSPOT_ORDER_SYNC_MAX_WAIT_SECONDS')) {
    $w = getenv('SHOPIFY_HUBSPOT_ORDER_SYNC_MAX_WAIT_SECONDS');
    $w = ($w !== false && $w !== '') ? (int) $w : 600;
    if ($w < 60) {
        $w = 60;
    }
    if ($w > 900) {
        $w = 900;
    }
    define('SHOPIFY_HUBSPOT_ORDER_SYNC_MAX_WAIT_SECONDS', $w);
}

if (!defined('SHOPIFY_HUBSPOT_ORDER_SYNC_SLEEP_SECONDS')) {
    $s = getenv('SHOPIFY_HUBSPOT_ORDER_SYNC_SLEEP_SECONDS');
    $s = ($s !== false && $s !== '') ? (int) $s : 2;
    if ($s < 1) {
        $s = 1;
    }
    if ($s > 10) {
        $s = 10;
    }
    define('SHOPIFY_HUBSPOT_ORDER_SYNC_SLEEP_SECONDS', $s);
}

