/**
 * Shared configuration for QM operations
 */

import { getRandomUserAgent } from "@stock-bot/utils";

// QM Session IDs for different endpoints
export const QM_SESSION_IDS = {
  LOOKUP: 'dc8c9930437f65d30f6597768800957017bac203a0a50342932757c8dfa158d6', // lookup endpoint
  SYMBOL: '1e1d7cb1de1fd2fe52684abdea41a446919a5fe12776dfab88615ac1ce1ec2f6', // getProfiles
  PRICES: '5ad521e05faf5778d567f6d0012ec34d6cdbaeb2462f41568f66558bc7b4ced9', // getEnhancedChartData
  FINANCIALS: '4e4f1565fb7c9f2a8b4b32b9aa3137af684f3da8a2ce97799d3a7117b14f07be', // getFinancialsEnhancedBySymbol
  // FILINGS: 'a863d519e38f80e45d10e280fb1afc729816e23f0218db2f3e8b23005a9ad8dd', //  getCompanyFilings
  // INTRADAY: '', //  
  // '5ad521e05faf5778d567f6d0012ec34d6cdbaeb2462f41568f66558bc7b4ced9' // getEhnachedChartData
  // '5ad521e05faf5778d567f6d0012ec34d6cdbaeb2462f41568f66558bc7b4ced9': [], //4488d072b
  // cc1cbdaf040f76db8f4c94f7d156b9b9b716e1a7509ec9c74a48a47f6b6b9f87: [], //97ff00cf3 // getQuotes
  // '74963ff42f1db2320d051762b5d3950ff9eab23f9d5c5b592551b4ca0441d086': [], //32ca24e394b // getSplitsBySymbol getBrokerRatingsBySymbol getDividendsBySymbol getEarningsSurprisesBySymbol getEarningsEventsBySymbol
  // '1e1d7cb1de1fd2fe52684abdea41a446919a5fe12776dfab88615ac1ce1ec2f6': [], //fb5721812d2c // getEnhancedQuotes getProfiles
  // a900a06cc6b3e8036afb9eeb1bbf9783f0007698ed8f5cb1e373dc790e7be2e5: [], //cc882cd95f9 // getEnhancedQuotes
  // a863d519e38f80e45d10e280fb1afc729816e23f0218db2f3e8b23005a9ad8dd: [], //05a09a41225 // getCompanyFilings getEnhancedQuotes
  // b3cdb1873f3682c5aeeac097be6181529bfb755945e5a412a24f4b9316291427: [], //6a63f56a6 // getHeadlinesTickerStory
  // '97b24911d7b034620aafad9441afdb2bc906ee5c992d86933c5903254ca29709': [], //c56424868d // detailed-quotes
  // '8a394f09cb8540c8be8988780660a7ae5b583c331a1f6cb12834f051a0169a8f': [], //2a86d214e50e5 // getGlobalIndustrySectorPeers getKeyRatiosBySymbol getGlobalIndustrySectorCodeList
  // '2f059f75e2a839437095c9e7e4991d2365bafa7bbb086672a87ae0cf8d92eb01': [], // 48fa36d // getNethouseBySymbol
  // d7ae7e0091dd1d7011948c3dc4af09b5ec552285d92bb188be2618968bc78e3f: [], // 63548ee //getRecentTradesBySymbol getQuotes getLevel2Quote getRecentTradesBySymbol
  // d22d1db8f67fe6e420b4028e5129b289ca64862aa6cee8459193747b68c01de3: [], // 84e9e
  // '6e0b22a7cbc02ac3fa07d45e2880b7696aaebeb29574dce81789e570570c9002': [], //
  // Add other session IDs as needed
} as const;

// QM API Configuration
export const QM_CONFIG = {
  PROXY_URL: 'http://5.79.66.2:13010',
  BASE_URL: 'https://app.quotemedia.com',
  SESSION_PATH: '/auth/g/authenticate/dataTool/v0/500',
  LOOKUP_URL: 'https://app.quotemedia.com/datatool/lookup.json',
  SYMBOL_URL: 'https://app.quotemedia.com/datatool/getProfiles.json',
  PRICES_URL: 'https://app.quotemedia.com/datatool/getEnhancedChartData.json',
  INTRADAY_URL: 'https://app.quotemedia.com/datatool/getEnhancedChartData.json',
  EVENTS_URL: 'https://app.quotemedia.com/datatool/getIndicatorsBySymbol.json',
  FINANCIALS_URL: 'https://app.quotemedia.com/datatool/getFinancialsEnhancedBySymbol.json',
  FILING_URL: 'https://app.quotemedia.com/datatool/getCompanyFilings.json',
} as const;

// Session management settings
export const SESSION_CONFIG = {
  MAX_SESSIONS: 100,
  MAX_FAILED_CALLS: 5,
  SESSION_TIMEOUT: 5000, // 10 seconds
  API_TIMEOUT: 30000, // 15 seconds
} as const;

export function getQmHeaders(): Record<string, string> {
  // if(type?.toUpperCase() === 'FILINGS') {
  //   return {
  //     'User-Agent': getRandomUserAgent(),
  //     Accept: '*/*',
  //     'Accept-Language': 'en-US,en;q=0.5',
  //     'Sec-Fetch-Mode': 'cors',
  //     Origin: 'https://client.quotemedia.com',
  //     Referer: 'https://client.quotemedia.com/',
  //     'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8',
  //   };
  // }

  return {
    'User-Agent': getRandomUserAgent(),
    Accept: '*/*',
    'Accept-Language': 'en',
    'Sec-Fetch-Mode': 'cors',
    Origin: 'https://www.quotemedia.com',
    Referer: 'https://www.quotemedia.com/',
  };
}

function parseLocalDate(dateString: string): Date {
  const [year, month, day] = dateString.split('-').map(Number);
  return new Date(year || 0, (month || 0) - 1, day);
}

// Get start of week (Monday)
export function getWeekStart(dateInput: Date | string): Date {
  // Handle string input properly
  let date: Date;
  if (typeof dateInput === 'string') {
    date = parseLocalDate(dateInput);
  } else {
    // Create new date with local time components
    date = new Date(dateInput.getFullYear(), dateInput.getMonth(), dateInput.getDate());
  }

  const day = date.getDay();

  if (day !== 1) {
    const diff = date.getDate() - day + (day === 0 ? -6 : 1);
    date.setDate(diff);
  }

  date.setHours(0, 0, 0, 0);
  return date;
}

export function getLastWeek(dateInput: Date | string): Date {
  // Handle string input properly
  let date: Date;
  if (typeof dateInput === 'string') {
    date = parseLocalDate(dateInput);
  } else {
    // Create new date with local time components
    date = new Date(dateInput.getFullYear(), dateInput.getMonth(), dateInput.getDate());
  }

  // Subtract 7 days
  date.setDate(date.getDate() - 7);

  date.setHours(0, 0, 0, 0);
  return date;
}

// Get end of week (Sunday)
export function getWeekEnd(dateInput: Date | string): Date {
  let date: Date;

  // Handle string input properly
  if (typeof dateInput === 'string') {
    date = parseLocalDate(dateInput);
  } else {
    // Create new date with local time components
    date = new Date(dateInput.getFullYear(), dateInput.getMonth(), dateInput.getDate());
  }

  const day = date.getDay();

  // If not already Sunday, calculate days until Sunday
  if (day !== 0) {
    const daysToSunday = 7 - day;
    date.setDate(date.getDate() + daysToSunday);
  }

  // Set to end of day
  date.setHours(23, 59, 59, 999);
  return date;
}