/**
 * Resolve nested path in object (e.g., 'contact.phone')
 */
function resolveNestedPath(obj, path) {
  if (!path || !obj) return undefined;

  const parts = path.split('.');
  let current = obj;

  for (const part of parts) {
    if (current && typeof current === 'object' && part in current) {
      current = current[part];
    } else {
      return undefined;
    }
  }

  return current;
}

/**
 * Resolve DBTextField value from reportData
 * @param {Object} reportData - The full report data
 * @param {string} objectKey - Top-level object key (e.g., 'owner', 'vessel')
 * @param {string} fieldPath - Dot-notation path to field (e.g., 'name', 'contact.phone')
 * @param {string} parentBandId - ID of parent band (if element is inside a band)
 * @param {Object} bandContext - Band context with currentBandData map
 * @returns {string} - Resolved value as string
 */
export function resolveDBTextValue(reportData, objectKey, fieldPath, parentBandId = null, bandContext = null) {
  // If inside band, use band's current data for automatic binding
  if (parentBandId && bandContext && bandContext.currentBandData && bandContext.currentBandData[parentBandId]) {
    const bandData = bandContext.currentBandData[parentBandId];

    // If objectKey is empty, resolve directly from band data
    if (!objectKey) {
      if (!fieldPath) return '';
      const value = resolveNestedPath(bandData, fieldPath);
      return value !== undefined ? String(value) : '';
    }

    // If objectKey is provided, resolve from objectKey in band data
    const objectData = bandData[objectKey];
    if (!objectData) return '';

    if (!fieldPath) {
      return typeof objectData === 'object' ? '' : String(objectData);
    }

    const value = resolveNestedPath(objectData, fieldPath);
    return value !== undefined ? String(value) : '';
  }

  // Normal resolution (not in band or no band context)
  if (!reportData || !objectKey) {
    return '';
  }

  const objectData = reportData[objectKey];
  if (!objectData) {
    return '';
  }

  if (!fieldPath) {
    // If no fieldPath, try to display the object itself (if primitive)
    return typeof objectData === 'object' ? '' : String(objectData);
  }

  const value = resolveNestedPath(objectData, fieldPath);
  return value !== undefined ? String(value) : '';
}

/**
 * Get available object keys from reportData
 */
export function getAvailableObjects(reportData) {
  if (!reportData || typeof reportData !== 'object') {
    return [];
  }
  return Object.keys(reportData);
}

/**
 * Get available field paths from an object (recursive)
 */
export function getAvailableFields(obj, prefix = '') {
  if (!obj || typeof obj !== 'object') {
    return [];
  }

  const fields = [];

  for (const key of Object.keys(obj)) {
    // Skip raw id/FK fields — only human-readable fields are useful for binding
    if (key === 'id' || (key.endsWith('_user') && !key.endsWith('_user_name'))) continue;

    const fullPath = prefix ? `${prefix}.${key}` : key;
    const value = obj[key];

    if (typeof value === 'object' && value !== null && !Array.isArray(value)) {
      // Nested object - recurse into children only (don't add the object itself)
      fields.push(...getAvailableFields(value, fullPath));
    } else {
      // Primitive or array - add this key
      fields.push(fullPath);
    }
  }

  return fields;
}
