// ============================================================================
// Environment configuration
// ============================================================================
// Two environments: Dev and Prod.
//
// Detection rules:
//   - hostname is `localhost` / `127.0.0.1` / `*.pages.dev` / contains "dev"
//       → DEV environment
//   - everything else (your prod custom domain) → PROD environment
//
// To override at runtime: append ?env=dev or ?env=prod to the URL.
//
// SECURITY NOTE: The Supabase anon key is designed to be public; Row-Level
// Security (RLS) policies in schema.sql control what each user can read/write.
// Never put the service-role key here.
// ============================================================================

window.EMPHASIS_ENV = (() => {
  const ENVS = {
    dev: {
      name: 'dev',
      supabaseUrl:     'https://jzwcaggjxlepqctbvoqs.supabase.co',
      supabaseAnonKey: 'sb_publishable_xMGsmaxrmbQkR7QHXH5_kg_ibH1htaJ',
      banner: true,                                                // show "DEV" pill in header
    },
    prod: {
      name: 'prod',
      supabaseUrl:     'https://wtzyxghmwyeokrpdkdhl.supabase.co',
      supabaseAnonKey: 'sb_publishable_c30Pc6xj9FFgR4FSo3qUAw_hzq-ZgPW',
      banner: false,
    },
  };

  // 1) ?env=dev|prod query override (handy on prod for quick QA)
  const url = new URL(window.location.href);
  const override = url.searchParams.get('env');
  if (override && ENVS[override]) return ENVS[override];

  // 2) Explicit hostname allowlist for PROD (everything else → DEV)
  // Add your custom prod domain here when you wire it up (e.g. 'app.emphasis.com')
  const PROD_HOSTS = [
    'emphasis-tracking.pages.dev',     // Cloudflare Pages production apex
  ];

  const host = window.location.hostname;
  const isProd = PROD_HOSTS.includes(host);
  return isProd ? ENVS.prod : ENVS.dev;
})();

// Surface a tiny indicator so you always know which env you're talking to
console.log(
  `%c EMPHASIS ${window.EMPHASIS_ENV.name.toUpperCase()} `,
  `background:${window.EMPHASIS_ENV.name === 'prod' ? '#10B981' : '#F59E0B'};color:white;padding:2px 8px;border-radius:4px;font-weight:bold;`
);
