/**
 * Parse a SQLite timestamp (which is stored as UTC) into a JavaScript Date object.
 * SQLite CURRENT_TIMESTAMP returns: "YYYY-MM-DD HH:MM:SS" in UTC
 * But without a 'Z' suffix, JavaScript treats it as local time.
 * This function ensures it's interpreted as UTC.
 * 
 * @param {string} sqliteTimestamp - Timestamp from SQLite (e.g., "2025-10-30 14:30:00")
 * @returns {Date} - JavaScript Date object
 */
export function parseUTCTimestamp(sqliteTimestamp) {
  if (!sqliteTimestamp) return new Date();
  
  // If timestamp already has 'Z' or timezone info, use it directly
  if (sqliteTimestamp.includes('Z') || sqliteTimestamp.includes('+') || sqliteTimestamp.includes('T')) {
    return new Date(sqliteTimestamp);
  }
  
  // SQLite format: "YYYY-MM-DD HH:MM:SS"
  // Add 'Z' to explicitly mark it as UTC
  return new Date(sqliteTimestamp + 'Z');
}

/**
 * Format a SQLite timestamp as a localized date string
 * @param {string} sqliteTimestamp 
 * @returns {string} - Localized date (e.g., "10/30/2025")
 */
export function formatLocalDate(sqliteTimestamp) {
  return parseUTCTimestamp(sqliteTimestamp).toLocaleDateString();
}

/**
 * Format a SQLite timestamp as a localized time string
 * @param {string} sqliteTimestamp 
 * @returns {string} - Localized time (e.g., "2:30:00 PM")
 */
export function formatLocalTime(sqliteTimestamp) {
  return parseUTCTimestamp(sqliteTimestamp).toLocaleTimeString();
}

/**
 * Format a SQLite timestamp as a localized date and time string
 * @param {string} sqliteTimestamp 
 * @returns {string} - Localized date and time (e.g., "10/30/2025, 2:30:00 PM")
 */
export function formatLocalDateTime(sqliteTimestamp) {
  return parseUTCTimestamp(sqliteTimestamp).toLocaleString();
}

/**
 * Check if a SQLite timestamp falls on a Sunday (in local timezone)
 * @param {string} sqliteTimestamp
 * @returns {boolean}
 */
export function isSunday(sqliteTimestamp) {
  return parseUTCTimestamp(sqliteTimestamp).getDay() === 0;
}

/**
 * Get a locale-independent date key for grouping sessions by local calendar day
 * @param {string} sqliteTimestamp
 * @returns {string} - e.g., "2026-03-23"
 */
export function getLocalDateKey(sqliteTimestamp) {
  const d = parseUTCTimestamp(sqliteTimestamp);
  const year = d.getFullYear();
  const month = String(d.getMonth() + 1).padStart(2, '0');
  const day = String(d.getDate()).padStart(2, '0');
  return `${year}-${month}-${day}`;
}

/**
 * Format a SQLite timestamp as a day header string (e.g., "Sunday, Mar 23, 2026")
 * @param {string} sqliteTimestamp
 * @returns {string}
 */
export function formatDayHeader(sqliteTimestamp) {
  const d = parseUTCTimestamp(sqliteTimestamp);
  return d.toLocaleDateString(undefined, {
    weekday: 'long',
    year: 'numeric',
    month: 'short',
    day: 'numeric',
  });
}

/**
 * Format a SQLite timestamp as a time-only string (e.g., "7:30 PM")
 * @param {string} sqliteTimestamp
 * @returns {string}
 */
export function formatTimeOnly(sqliteTimestamp) {
  const d = parseUTCTimestamp(sqliteTimestamp);
  return d.toLocaleTimeString(undefined, {
    hour: 'numeric',
    minute: '2-digit',
  });
}

