/**
 * Page Registry
 * 
 * Maps uiComponent codes from the Navigation API to React components and icons.
 * This is the single source of truth for component mapping in the frontend.
 * 
 * The backend provides uiComponent values like:
 * - "page.system.home"
 * - "page.admin.users"
 * - "page.feature.trustee.dashboard"
 * 
 * This registry maps them to:
 * - Icon components for navigation
 * - Page components for routing (lazy loaded)
 */

import React from 'react';
import {
  FaHome, FaCog, FaBriefcase, FaRobot, FaPlay, FaBuilding, FaUsers, FaUserTag,
  FaCubes, FaEnvelopeOpenText, FaKey, FaUsersCog, FaCube, FaShieldAlt,
  FaLightbulb, FaRegFileAlt, FaLink, FaComments, FaChartBar, FaMicrophone,
  FaListAlt, FaCogs, FaChartLine, FaFileAlt, FaUserShield, FaDatabase,
  FaProjectDiagram, FaMapMarkedAlt
} from 'react-icons/fa';

// =============================================================================
// ICON MAP
// =============================================================================

/**
 * Maps uiComponent codes to icon components.
 * Used by navigation to display icons next to menu items.
 */
export const PAGE_ICONS: Record<string, React.ReactNode> = {
  // System pages
  'page.system.home': <FaHome />,
  'page.system.settings': <FaCog />,
  'page.system.gdpr': <FaShieldAlt />,
  'page.system.playground': <FaPlay />,
  'page.system.chats': <FaListAlt />,
  'page.system.automations': <FaCogs />,
  'page.system.prompts': <FaLightbulb />,
  'page.system.files': <FaRegFileAlt />,
  'page.system.connections': <FaLink />,
  'page.system.chatbot': <FaComments />,
  'page.system.pek': <FaChartBar />,
  'page.system.speech': <FaMicrophone />,

  // Admin pages
  'page.admin.users': <FaUsers />,
  'page.admin.invitations': <FaEnvelopeOpenText />,
  'page.admin.mandates': <FaBuilding />,
  'page.admin.roles': <FaKey />,
  'page.admin.role-permissions': <FaShieldAlt />,
  'page.admin.user-mandates': <FaUserTag />,
  'page.admin.feature-roles': <FaCube />,
  'page.admin.feature-instances': <FaCubes />,
  'page.admin.feature-users': <FaUsersCog />,
  'page.admin.user-access-overview': <FaUserShield />,

  // Feature pages - Trustee
  'page.feature.trustee.dashboard': <FaChartLine />,
  'page.feature.trustee.positions': <FaDatabase />,
  'page.feature.trustee.documents': <FaFileAlt />,
  'page.feature.trustee.position-documents': <FaLink />,
  'page.feature.trustee.expense-import': <FaFileAlt />,
  'page.feature.trustee.instance-roles': <FaUserShield />,

  // Feature pages - Real Estate
  'page.feature.realestate.projects': <FaProjectDiagram />,
  'page.feature.realestate.parcels': <FaMapMarkedAlt />,
  
  // Feature icons (for feature grouping in navigation)
  'feature.trustee': <FaBriefcase />,
  'feature.realestate': <FaBuilding />,
  'feature.chatworkflow': <FaPlay />,
  'feature.chatbot': <FaRobot />,
};

// =============================================================================
// HELPER FUNCTIONS
// =============================================================================

/**
 * Get icon for a uiComponent code.
 * Falls back to FaCog if not found.
 */
export function getPageIcon(uiComponent: string): React.ReactNode {
  return PAGE_ICONS[uiComponent] || <FaCog />;
}

/**
 * Check if a uiComponent is a feature page (requires instance context).
 */
export function isFeaturePage(uiComponent: string): boolean {
  return uiComponent.startsWith('page.feature.');
}

/**
 * Check if a uiComponent is an admin page.
 */
export function isAdminPage(uiComponent: string): boolean {
  return uiComponent.startsWith('page.admin.');
}

/**
 * Extract feature code from uiComponent.
 * e.g., "page.feature.trustee.dashboard" -> "trustee"
 */
export function extractFeatureCode(uiComponent: string): string | null {
  if (!uiComponent.startsWith('page.feature.')) {
    return null;
  }
  const parts = uiComponent.split('.');
  return parts.length >= 3 ? parts[2] : null;
}

/**
 * Extract view code from uiComponent.
 * e.g., "page.feature.trustee.dashboard" -> "dashboard"
 */
export function extractViewCode(uiComponent: string): string | null {
  const parts = uiComponent.split('.');
  return parts.length >= 4 ? parts[3] : null;
}

/**
 * Build uiComponent from parts.
 */
export function buildUiComponent(type: 'system' | 'admin' | 'feature', ...parts: string[]): string {
  return `page.${type}.${parts.join('.')}`;
}

// =============================================================================
// EXPORTS
// =============================================================================

export default PAGE_ICONS;
