// ⚠️ When updating, also update vite.config.ts proxy headers (search for APP_VERSION)
export const APP_VERSION = '8.8.3';
export const OS_VERSION = '18.6.2';

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,
  'User-Agent': 'feeld-mobile',
  '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-vBzSZvWXvunGC0c3Sn5Sf4vxwSdb2z4VwT6rLC7LhGo_I4Wd759NCtKBBmIM994RPlEd3eHFT7v0CwkTFpu7uTT80uFa7EDR5DgYK-DiCm4S8nvcuoPMgPK1pa2xRYafqXnUmMEaZNIpWozvQrqmGZHa62pFySbX6EXxXd-S9vewsbWbSXUkZtZUMtOx3GR5nP1jP-z_bTGFktrJUNjV0tfeGa6C3Jc5MOg4dvjPGJ6YAMKGGMkKoDauNK1lHFKSvtnfMRFC6KrJYar1bsCljre-9aaYwq2gJ9YSZ53T6JIkV2_74c0',
  EVENT_ANALYTICS_ID: '53923e7d-39bb-42ac-99c6-99cdf6008d11',
};

// 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; },
};
