/**
 * 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, FaPlay, FaBuilding, FaUsers, FaUserTag,
  FaCubes, FaEnvelopeOpenText, FaKey, FaUsersCog, FaCube, FaShieldAlt,
  FaLightbulb, FaRegFileAlt, FaLink, FaComments,
  FaListAlt, FaChartLine, FaChartBar, FaFileAlt, FaUserShield, FaDatabase,
  FaProjectDiagram, FaMapMarkedAlt, FaWallet, FaMoneyBillAlt, FaClock,
  FaHeadset, FaVideo, FaHatWizard, FaStore, FaUserTie, FaClipboardList,
  FaFileContract, FaRobot, FaGlobe, FaClipboardCheck,
} 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.integrations': <FaProjectDiagram />,
  'page.system.settings': <FaCog />,
  'page.system.store': <FaStore />,
  'page.system.gdpr': <FaShieldAlt />,

  // Basedata pages (system-level)
  'page.system.prompts': <FaLightbulb />,
  'page.system.files': <FaRegFileAlt />,
  'page.system.connections': <FaLink />,

  // System pages - Overviews
  'page.system.complianceAudit': <FaShieldAlt />,

  // System pages - Usage
  'page.system.billingAdmin': <FaMoneyBillAlt />,
  'page.system.statistics': <FaChartBar />,
  'page.system.automations': <FaRobot />,

  // Billing pages (legacy compat)
  'page.billing.dashboard': <FaWallet />,
  'page.billing.transactions': <FaListAlt />,

  // Admin pages (kebab-case + camelCase variants for backend compatibility)
  'page.admin.access': <FaBuilding />,
  'page.admin.users': <FaUsers />,
  'page.admin.invitations': <FaEnvelopeOpenText />,
  'page.admin.mandates': <FaBuilding />,
  'page.admin.roles': <FaKey />,
  'page.admin.role-permissions': <FaShieldAlt />,
  'page.admin.mandateRolePermissions': <FaShieldAlt />,
  'page.admin.user-mandates': <FaUserTag />,
  'page.admin.userMandates': <FaUserTag />,
  'page.admin.feature-roles': <FaCube />,
  'page.admin.featureRoles': <FaCube />,
  'page.admin.feature-instances': <FaCubes />,
  'page.admin.featureInstances': <FaCubes />,
  'page.admin.feature-users': <FaUsersCog />,
  'page.admin.user-access-overview': <FaUserShield />,
  'page.admin.userAccessOverview': <FaUserShield />,
  'page.admin.billing': <FaMoneyBillAlt />,
  'page.admin.subscriptions': <FaFileContract />,
  'page.admin.automationEvents': <FaClock />,
  'page.admin.automation-events': <FaClock />,
  'page.admin.automationLogs': <FaClipboardList />,
  'page.admin.automation-logs': <FaClipboardList />,
  'page.admin.logs': <FaFileAlt />,
  'page.admin.languages': <FaGlobe />,
  'page.admin.databaseHealth': <FaDatabase />,
  'page.admin.database-health': <FaDatabase />,
  'page.admin.demoConfig': <FaCubes />,
  'page.admin.demo-config': <FaCubes />,
  'page.admin.mandate-wizard': <FaHatWizard />,
  'page.admin.mandateWizard': <FaHatWizard />,
  'page.admin.invitation-wizard': <FaEnvelopeOpenText />,
  'page.admin.invitationWizard': <FaEnvelopeOpenText />,

  // Feature pages - Trustee
  'page.feature.trustee.dashboard': <FaChartLine />,
  'page.feature.trustee.positions': <FaDatabase />,
  'page.feature.trustee.documents': <FaFileAlt />,
  'page.feature.trustee.expense-import': <FaFileAlt />,
  'page.feature.trustee.scan-upload': <FaFileAlt />,
  'page.feature.trustee.instance-roles': <FaUserShield />,
  'page.feature.trustee.settings': <FaCog />,
  'page.feature.trustee.analyse': <FaChartBar />,
  'page.feature.trustee.abschluss': <FaClipboardCheck />,

  // Feature pages - Real Estate
  'page.feature.realestate.projects': <FaProjectDiagram />,
  'page.feature.realestate.parcels': <FaMapMarkedAlt />,
  
  // Feature pages - Teams Bot
  'page.feature.teamsbot.dashboard': <FaChartLine />,
  'page.feature.teamsbot.sessions': <FaVideo />,
  'page.feature.teamsbot.settings': <FaCog />,

  // Feature pages - Neutralization
  'page.feature.neutralization.dashboard': <FaShieldAlt />,
  'page.feature.neutralization.playground': <FaShieldAlt />,
  'page.feature.neutralization.config': <FaCog />,
  'page.feature.neutralization.attributes': <FaDatabase />,

  // Feature pages - CommCoach
  'page.feature.commcoach.dashboard': <FaChartLine />,
  'page.feature.commcoach.coaching': <FaComments />,
  'page.feature.commcoach.dossier': <FaClipboardList />,
  'page.feature.commcoach.settings': <FaCog />,

  // Feature icons (for feature grouping in navigation)
  'feature.commcoach': <FaUserTie />,
  'feature.neutralization': <FaShieldAlt />,
  'feature.trustee': <FaBriefcase />,
  'feature.realestate': <FaBuilding />,
  'feature.chatworkflow': <FaPlay />,
  'feature.graphicalEditor': <FaProjectDiagram />,
  'page.feature.graphicalEditor.editor': <FaProjectDiagram />,
  'page.feature.graphicalEditor.workflows': <FaProjectDiagram />,
  'page.feature.graphicalEditor.workflows-tasks': <FaClipboardList />,
  'page.feature.chatbot.conversations': <FaComments />,
  'feature.chatbot': <FaComments />,
  'feature.teamsbot': <FaHeadset />,

  // Feature pages - Workspace
  'page.feature.workspace.dashboard': <FaPlay />,
  'page.feature.workspace.editor': <FaPlay />,
  'feature.workspace': <FaPlay />,
};

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

/**
 * Get icon for a uiComponent code.
 * Returns null if not found -- missing icons should be added to PAGE_ICONS.
 */
export function getPageIcon(uiComponent: string): React.ReactNode {
  if (!PAGE_ICONS[uiComponent]) {
    console.warn(`[pageRegistry] Missing icon for uiComponent: "${uiComponent}"`);
  }
  return PAGE_ICONS[uiComponent] || null;
}

/**
 * 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;
