/**
 * Shopify Partner App & API Configuration.
 * Server-only secrets are marked and excluded from client-side bundles.
 */

export const SHOPIFY_CONFIG = {
  // Public Client Safe
  apiKey:
    process.env.NEXT_PUBLIC_SHOPIFY_API_KEY ||
    process.env.SHOPIFY_API_KEY ||
    'd6d73fa2d424638f91826f822d89c51f',
  get apiVersion(): string {
    const raw = (process.env.SHOPIFY_API_VERSION || '2024-07').trim();
    // Validate supported quarterly format (e.g. 2024-04, 2024-07, 2024-10, 2025-01) or unstable
    if (/^202[3-5]-(01|04|07|10)$/.test(raw) || raw === 'unstable') {
      return raw;
    }
    return '2024-07';
  },
  scopes: process.env.SHOPIFY_SCOPES || 'read_products,write_products,read_inventory,read_customer_events',
  get scopesArray(): string[] {
    return this.scopes.split(',').map((s) => s.trim()).filter(Boolean);
  },
  defaultShopDomain: process.env.DEFAULT_SHOP_DOMAIN || 'digi-horizon.myshopify.com',

  // Route paths
  paths: {
    auth: '/api/auth/shopify',
    authCallback: '/api/auth/shopify/callback',
    webhook: '/api/webhooks/shopify',
    gdprWebhook: '/api/webhooks/shopify/gdpr',
    appRedirect: '/pixel',
  },

  // Server-only access helpers
  getServerSecret: () => process.env.SHOPIFY_API_SECRET || '',
  getWebhookSecret: () => process.env.SHOPIFY_WEBHOOK_SECRET || process.env.SHOPIFY_API_SECRET || '',
} as const;

export function getShopifySecret(): string {
  return process.env.SHOPIFY_API_SECRET || '';
}
