import React, { useEffect } from 'react';
import { Box } from 'ink';
import { AuthProvider, useAuth } from './state/auth-context.js';
import { NavigationProvider, useNavigation } from './state/navigation-context.js';
import { LoginScreen } from './components/auth/LoginScreen.js';
import { MainMenu } from './components/MainMenu.js';
import { MainLayout } from './components/layout/MainLayout.js';
import { LoadingSpinner } from './components/shared/LoadingSpinner.js';
import { UserListScreen } from './components/users/UserListScreen.js';
import { UserCreateScreen } from './components/users/UserCreateScreen.js';
import { UserDetailScreen } from './components/users/UserDetailScreen.js';
import { ChangePasswordScreen } from './components/users/ChangePasswordScreen.js';
import { RoleListScreen } from './components/roles/RoleListScreen.js';
import { RoleDetailScreen } from './components/roles/RoleDetailScreen.js';
// Stammdaten
import { MasterdataMenu } from './components/masterdata/MasterdataMenu.js';
import { CategoryListScreen } from './components/masterdata/categories/CategoryListScreen.js';
import { CategoryDetailScreen } from './components/masterdata/categories/CategoryDetailScreen.js';
import { CategoryCreateScreen } from './components/masterdata/categories/CategoryCreateScreen.js';
import { SupplierListScreen } from './components/masterdata/suppliers/SupplierListScreen.js';
import { SupplierDetailScreen } from './components/masterdata/suppliers/SupplierDetailScreen.js';
import { SupplierCreateScreen } from './components/masterdata/suppliers/SupplierCreateScreen.js';
import { RateSupplierScreen } from './components/masterdata/suppliers/RateSupplierScreen.js';
import { AddCertificateScreen } from './components/masterdata/suppliers/AddCertificateScreen.js';
import { ArticleListScreen } from './components/masterdata/articles/ArticleListScreen.js';
import { ArticleDetailScreen } from './components/masterdata/articles/ArticleDetailScreen.js';
import { ArticleCreateScreen } from './components/masterdata/articles/ArticleCreateScreen.js';
import { AddSalesUnitScreen } from './components/masterdata/articles/AddSalesUnitScreen.js';
import { CustomerListScreen } from './components/masterdata/customers/CustomerListScreen.js';
import { CustomerDetailScreen } from './components/masterdata/customers/CustomerDetailScreen.js';
import { CustomerCreateScreen } from './components/masterdata/customers/CustomerCreateScreen.js';
import { AddDeliveryAddressScreen } from './components/masterdata/customers/AddDeliveryAddressScreen.js';
import { SetPreferencesScreen } from './components/masterdata/customers/SetPreferencesScreen.js';
// Lagerverwaltung
import { InventoryMenu } from './components/inventory/InventoryMenu.js';
import { StorageLocationListScreen } from './components/inventory/StorageLocationListScreen.js';
import { StorageLocationCreateScreen } from './components/inventory/StorageLocationCreateScreen.js';
import { StorageLocationDetailScreen } from './components/inventory/StorageLocationDetailScreen.js';
import { StorageLocationEditScreen } from './components/inventory/StorageLocationEditScreen.js';
import { StockBatchEntryScreen } from './components/inventory/StockBatchEntryScreen.js';
import { AddBatchScreen } from './components/inventory/AddBatchScreen.js';
import { StockMovementListScreen } from './components/inventory/StockMovementListScreen.js';
import { StockMovementDetailScreen } from './components/inventory/StockMovementDetailScreen.js';
import { StockMovementRecordScreen } from './components/inventory/StockMovementRecordScreen.js';
// Produktion
import { ProductionMenu } from './components/production/ProductionMenu.js';
import { RecipeListScreen } from './components/production/RecipeListScreen.js';
import { RecipeCreateScreen } from './components/production/RecipeCreateScreen.js';
import { RecipeDetailScreen } from './components/production/RecipeDetailScreen.js';
import { AddProductionStepScreen } from './components/production/AddProductionStepScreen.js';
import { AddIngredientScreen } from './components/production/AddIngredientScreen.js';
import { BatchListScreen } from './components/production/BatchListScreen.js';
import { BatchDetailScreen } from './components/production/BatchDetailScreen.js';
import { BatchPlanScreen } from './components/production/BatchPlanScreen.js';
import { RecordConsumptionScreen } from './components/production/RecordConsumptionScreen.js';
import { CompleteBatchScreen } from './components/production/CompleteBatchScreen.js';
import { ProductionOrderListScreen } from './components/production/ProductionOrderListScreen.js';
import { ProductionOrderCreateScreen } from './components/production/ProductionOrderCreateScreen.js';
import { ProductionOrderDetailScreen } from './components/production/ProductionOrderDetailScreen.js';
import { StockListScreen } from './components/inventory/StockListScreen.js';
import { StockDetailScreen } from './components/inventory/StockDetailScreen.js';
import { StockCreateScreen } from './components/inventory/StockCreateScreen.js';
import { ReserveStockScreen } from './components/inventory/ReserveStockScreen.js';
import { InventoryCountListScreen } from './components/inventory/InventoryCountListScreen.js';
import { InventoryCountCreateScreen } from './components/inventory/InventoryCountCreateScreen.js';
import { InventoryCountDetailScreen } from './components/inventory/InventoryCountDetailScreen.js';

function ScreenRouter() {
  const { isAuthenticated, loading } = useAuth();
  const { current, navigate } = useNavigation();

  // Redirect zu main-menu nach erfolgreichem Login
  useEffect(() => {
    if (isAuthenticated && current === 'login') {
      navigate('main-menu');
    }
  }, [isAuthenticated, current, navigate]);

  if (loading) {
    return (
      <Box alignItems="center" justifyContent="center" paddingY={4}>
        <LoadingSpinner label="Initialisiere..." />
      </Box>
    );
  }

  if (!isAuthenticated) {
    return (
      <MainLayout showHeader={false}>
        <LoginScreen />
      </MainLayout>
    );
  }

  return (
    <MainLayout>
      {current === 'main-menu' && <MainMenu />}
      {current === 'user-list' && <UserListScreen />}
      {current === 'user-create' && <UserCreateScreen />}
      {current === 'user-detail' && <UserDetailScreen />}
      {current === 'change-password' && <ChangePasswordScreen />}
      {current === 'role-list' && <RoleListScreen />}
      {current === 'role-detail' && <RoleDetailScreen />}
      {/* Stammdaten */}
      {current === 'masterdata-menu' && <MasterdataMenu />}
      {current === 'category-list' && <CategoryListScreen />}
      {current === 'category-detail' && <CategoryDetailScreen />}
      {current === 'category-create' && <CategoryCreateScreen />}
      {current === 'supplier-list' && <SupplierListScreen />}
      {current === 'supplier-detail' && <SupplierDetailScreen />}
      {current === 'supplier-create' && <SupplierCreateScreen />}
      {current === 'supplier-rate' && <RateSupplierScreen />}
      {current === 'supplier-add-certificate' && <AddCertificateScreen />}
      {current === 'article-list' && <ArticleListScreen />}
      {current === 'article-detail' && <ArticleDetailScreen />}
      {current === 'article-create' && <ArticleCreateScreen />}
      {current === 'article-add-sales-unit' && <AddSalesUnitScreen />}
      {current === 'customer-list' && <CustomerListScreen />}
      {current === 'customer-detail' && <CustomerDetailScreen />}
      {current === 'customer-create' && <CustomerCreateScreen />}
      {current === 'customer-add-delivery-address' && <AddDeliveryAddressScreen />}
      {current === 'customer-set-preferences' && <SetPreferencesScreen />}
      {/* Lagerverwaltung */}
      {current === 'inventory-menu' && <InventoryMenu />}
      {current === 'storage-location-list' && <StorageLocationListScreen />}
      {current === 'storage-location-create' && <StorageLocationCreateScreen />}
      {current === 'storage-location-detail' && <StorageLocationDetailScreen />}
      {current === 'storage-location-edit' && <StorageLocationEditScreen />}
      {current === 'stock-batch-entry' && <StockBatchEntryScreen />}
      {current === 'stock-add-batch' && <AddBatchScreen />}
      {current === 'stock-list' && <StockListScreen />}
      {current === 'stock-detail' && <StockDetailScreen />}
      {current === 'stock-create' && <StockCreateScreen />}
      {current === 'stock-reserve' && <ReserveStockScreen />}
      {current === 'stock-movement-list' && <StockMovementListScreen />}
      {current === 'stock-movement-detail' && <StockMovementDetailScreen />}
      {current === 'stock-movement-record' && <StockMovementRecordScreen />}
      {current === 'inventory-count-list' && <InventoryCountListScreen />}
      {current === 'inventory-count-create' && <InventoryCountCreateScreen />}
      {current === 'inventory-count-detail' && <InventoryCountDetailScreen />}
      {/* Produktion */}
      {current === 'production-menu' && <ProductionMenu />}
      {current === 'recipe-list' && <RecipeListScreen />}
      {current === 'recipe-create' && <RecipeCreateScreen />}
      {current === 'recipe-detail' && <RecipeDetailScreen />}
      {current === 'recipe-add-production-step' && <AddProductionStepScreen />}
      {current === 'recipe-add-ingredient' && <AddIngredientScreen />}
      {current === 'batch-list' && <BatchListScreen />}
      {current === 'batch-detail' && <BatchDetailScreen />}
      {current === 'batch-plan' && <BatchPlanScreen />}
      {current === 'batch-record-consumption' && <RecordConsumptionScreen />}
      {current === 'batch-complete' && <CompleteBatchScreen />}
      {current === 'production-order-list' && <ProductionOrderListScreen />}
      {current === 'production-order-create' && <ProductionOrderCreateScreen />}
      {current === 'production-order-detail' && <ProductionOrderDetailScreen />}
    </MainLayout>
  );
}

export function App() {
  return (
    <NavigationProvider initialScreen="login">
      <AuthProvider>
        <ScreenRouter />
      </AuthProvider>
    </NavigationProvider>
  );
}
