/**
 * FeatureView Page
 * 
 * Generische Feature-View-Komponente.
 * Rendert den entsprechenden Content basierend auf Feature-Code und View.
 */

import React from 'react';
import { useCurrentInstance } from '../hooks/useCurrentInstance';
import { useCanViewFeatureView } from '../hooks/useInstancePermissions';
import { getLabel, FEATURE_REGISTRY } from '../types/mandate';

// Trustee Views
import { TrusteeContractsView } from './views/trustee/TrusteeContractsView';
import { TrusteeOrganisationsView } from './views/trustee/TrusteeOrganisationsView';
import { TrusteeDocumentsView } from './views/trustee/TrusteeDocumentsView';
import { TrusteePositionsView } from './views/trustee/TrusteePositionsView';
import { TrusteeRolesView } from './views/trustee/TrusteeRolesView';
import { TrusteeAccessView } from './views/trustee/TrusteeAccessView';
import { TrusteeDashboardView } from './views/trustee/TrusteeDashboardView';

import styles from './FeatureView.module.css';

// =============================================================================
// PLACEHOLDER VIEWS (für nicht implementierte Features)
// =============================================================================

const PlaceholderView: React.FC<{ title: string; description: string }> = ({ title, description }) => (
  <div className={styles.placeholder}>
    <h2>{title}</h2>
    <p>{description}</p>
  </div>
);

// Chatworkflow Views
const ChatworkflowDashboard: React.FC = () => (
  <PlaceholderView title="Workflow Dashboard" description="Übersicht der Workflows" />
);

const ChatworkflowRuns: React.FC = () => (
  <PlaceholderView title="Runs" description="Workflow-Ausführungen" />
);

const ChatworkflowFiles: React.FC = () => (
  <PlaceholderView title="Dateien" description="Workflow-Dateien" />
);

// Chatbot Views
const ChatbotConversations: React.FC = () => (
  <PlaceholderView title="Konversationen" description="Chat-Konversationen" />
);

const ChatbotSettings: React.FC = () => (
  <PlaceholderView title="Chatbot Einstellungen" description="Konfiguration des Chatbots" />
);

// Generic/Fallback
const NotFound: React.FC = () => (
  <div className={styles.notFound}>
    <h2>Seite nicht gefunden</h2>
    <p>Diese View existiert nicht oder wurde noch nicht implementiert.</p>
  </div>
);

const AccessDenied: React.FC = () => (
  <div className={styles.accessDenied}>
    <h2>Zugriff verweigert</h2>
    <p>Du hast keine Berechtigung für diese Ansicht.</p>
  </div>
);

// =============================================================================
// VIEW REGISTRY
// =============================================================================

type ViewComponent = React.FC;

const VIEW_COMPONENTS: Record<string, Record<string, ViewComponent>> = {
  trustee: {
    dashboard: TrusteeDashboardView,
    organisations: TrusteeOrganisationsView,
    contracts: TrusteeContractsView,
    documents: TrusteeDocumentsView,
    positions: TrusteePositionsView,
    roles: TrusteeRolesView,
    access: TrusteeAccessView,
  },
  chatworkflow: {
    dashboard: ChatworkflowDashboard,
    runs: ChatworkflowRuns,
    files: ChatworkflowFiles,
  },
  chatbot: {
    conversations: ChatbotConversations,
    settings: ChatbotSettings,
  },
};

// =============================================================================
// FEATURE VIEW PAGE
// =============================================================================

interface FeatureViewPageProps {
  view: string;
}

export const FeatureViewPage: React.FC<FeatureViewPageProps> = ({ view }) => {
  const { instance, featureCode, isValid } = useCurrentInstance();
  
  // Berechtigungs-Check
  const viewCode = `${featureCode}-${view}`;
  const canView = useCanViewFeatureView(viewCode);
  
  // Nicht valider Kontext
  if (!isValid || !featureCode || !instance) {
    return <NotFound />;
  }
  
  // Keine Berechtigung
  if (!canView && view !== 'not-found') {
    return <AccessDenied />;
  }
  
  // View-Komponente finden
  const featureViews = VIEW_COMPONENTS[featureCode];
  if (!featureViews) {
    return <NotFound />;
  }
  
  const ViewComponent = featureViews[view];
  if (!ViewComponent) {
    return <NotFound />;
  }
  
  // View-Info aus Registry
  const featureConfig = FEATURE_REGISTRY[featureCode];
  const viewConfig = featureConfig?.views?.find(v => v.code === view);
  const viewLabel = viewConfig ? getLabel(viewConfig.label) : view;
  
  return (
    <div className={styles.featureView}>
      <header className={styles.viewHeader}>
        <h1 className={styles.viewTitle}>{viewLabel}</h1>
      </header>
      <main className={styles.viewContent}>
        <ViewComponent />
      </main>
    </div>
  );
};

export default FeatureViewPage;
