// ⚠️ When updating, also update vite.config.ts proxy headers and server/index.js APP_VERSION
export const APP_VERSION = '9.4.3';
export const OS_VERSION = '26.2.1';
// iOS app identifiers captured from a real iPhone — used by the proxy + server to mimic the iOS app.
export const IOS_BUNDLE_ID = 'com.3nder.threender';
export const IOS_DEVICE_MODEL = 'iPhone14_4';
export const FIREBASE_SDK_VERSION = '11.5.0';
export const FIREBASE_GMP_ID = '1:594152761603:ios:f52cf15efff827861b2136';
// Sent by the iOS app when on-device AppCheck fails — Firebase still accepts this fallback.
export const FIREBASE_APPCHECK_FALLBACK = 'eyJlcnJvciI6IlVOS05PV05fRVJST1IifQ==';

export const API_CONFIG = {
  // Use Vite proxy to bypass CORS
  GRAPHQL_ENDPOINT: '/api/graphql',
  FIREBASE_TOKEN_URL: '/api/firebase/v1/token',
  FIREBASE_API_KEY: 'AIzaSyD9o9mzulN50-hqOwF6ww9pxUNUxwVOCXA',
  // Backend server for data persistence
  BACKEND_URL: 'http://localhost:3001',
};

// Exact headers from Proxyman capture
export const REQUEST_HEADERS = {
  'x-device-os': 'ios',
  'x-app-version': APP_VERSION,
  'x-os-version': OS_VERSION,
  // Realistic iOS Alamofire UA — matches what the real Feeld app sends.
  'User-Agent': `Feeld/${APP_VERSION} (${IOS_BUNDLE_ID}; build:1; iOS ${OS_VERSION}) Alamofire/5.9.1`,
  'Accept': '*/*',
  'Accept-Language': 'en-US,en;q=0.9',
  'Accept-Encoding': 'gzip, deflate, br',
  'Content-Type': 'application/json',
  'Connection': 'keep-alive',
};

// Default credentials - can be overridden via localStorage
const DEFAULT_CREDENTIALS = {
  PROFILE_ID: 'profile#c5f40eca-b972-41b8-ba74-24db33a8f244',
  REFRESH_TOKEN: 'AMf-vBwqZWAS46Y76TtMlbyE7eSaPA952sVuMyxRwIUKbuqwEmHxn6Mk4Zy0vbOVKPjAckKjT42LkBIzFEUV9PdlhDpQehTmZ45VbqYLOPj7Vh4cQCYDsZP47APdLThHYoWGT4verYMEz7I7o0_6TC_V0B9kgvL6xdtcumeTY_UAG-5LZxUNYQ1EdAXwv4tCT7CpGIKsdoMpyyNwWNBKRV1LIP7c2H2OfZVgGF107XM3mfzc97kKOqT7JSZPg5tOuuFXFykTiiS_JjFMgvWzs2tk-V394sa1Jz3DrgWkXYhphUsa7S-ls9A',
  EVENT_ANALYTICS_ID: 'e207fccf-b258-428f-bb6a-02e14616915c',
};

// Auth credentials manager with localStorage persistence
export const getCredentials = () => {
  if (typeof window === 'undefined') return DEFAULT_CREDENTIALS;
  return {
    PROFILE_ID: localStorage.getItem('feeld_profile_id') || DEFAULT_CREDENTIALS.PROFILE_ID,
    REFRESH_TOKEN: localStorage.getItem('feeld_refresh_token') || DEFAULT_CREDENTIALS.REFRESH_TOKEN,
    EVENT_ANALYTICS_ID: localStorage.getItem('feeld_analytics_id') || DEFAULT_CREDENTIALS.EVENT_ANALYTICS_ID,
  };
};

export const setCredentials = (creds: { profileId?: string; refreshToken?: string; analyticsId?: string }) => {
  if (creds.profileId) localStorage.setItem('feeld_profile_id', creds.profileId);
  if (creds.refreshToken) localStorage.setItem('feeld_refresh_token', creds.refreshToken);
  if (creds.analyticsId) localStorage.setItem('feeld_analytics_id', creds.analyticsId);
};

export const clearCredentials = () => {
  localStorage.removeItem('feeld_profile_id');
  localStorage.removeItem('feeld_refresh_token');
  localStorage.removeItem('feeld_analytics_id');
};

// For backward compatibility - returns current credentials
export const TEST_CREDENTIALS = {
  get PROFILE_ID() { return getCredentials().PROFILE_ID; },
  get REFRESH_TOKEN() { return getCredentials().REFRESH_TOKEN; },
  get EVENT_ANALYTICS_ID() { return getCredentials().EVENT_ANALYTICS_ID; },
};
