import { Switch, Route } from "wouter";
import { queryClient } from "./lib/queryClient";
import { QueryClientProvider } from "@tanstack/react-query";
import { Toaster } from "@/components/ui/toaster";
import { TooltipProvider } from "@/components/ui/tooltip";
import { SidebarProvider, Sidebar, SidebarContent, SidebarGroup, SidebarGroupContent, SidebarGroupLabel, SidebarMenu, SidebarMenuButton, SidebarMenuItem, SidebarTrigger } from "@/components/ui/sidebar";
import { LayoutDashboard, AlertTriangle, Server, Shield, Brain, Menu, Activity, BarChart3, TrendingUp, List } from "lucide-react";
import Dashboard from "@/pages/Dashboard";
import Detections from "@/pages/Detections";
import DashboardLive from "@/pages/DashboardLive";
import AnalyticsHistory from "@/pages/AnalyticsHistory";
import Routers from "@/pages/Routers";
import Whitelist from "@/pages/Whitelist";
import PublicLists from "@/pages/PublicLists";
import Training from "@/pages/Training";
import Services from "@/pages/Services";
import NotFound from "@/pages/not-found";

const menuItems = [
  { title: "Dashboard", url: "/", icon: LayoutDashboard },
  { title: "Rilevamenti", url: "/detections", icon: AlertTriangle },
  { title: "Dashboard Live", url: "/dashboard-live", icon: Activity },
  { title: "Analytics Storici", url: "/analytics", icon: BarChart3 },
  { title: "Training ML", url: "/training", icon: Brain },
  { title: "Router", url: "/routers", icon: Server },
  { title: "Whitelist", url: "/whitelist", icon: Shield },
  { title: "Liste Pubbliche", url: "/public-lists", icon: List },
  { title: "Servizi", url: "/services", icon: TrendingUp },
];

function AppSidebar() {
  return (
    <Sidebar data-testid="sidebar">
      <SidebarContent>
        <SidebarGroup>
          <SidebarGroupLabel data-testid="text-sidebar-title">IDS System</SidebarGroupLabel>
          <SidebarGroupContent>
            <SidebarMenu>
              {menuItems.map((item) => (
                <SidebarMenuItem key={item.title}>
                  <SidebarMenuButton asChild data-testid={`link-${item.title.toLowerCase()}`}>
                    <a href={item.url}>
                      <item.icon />
                      <span>{item.title}</span>
                    </a>
                  </SidebarMenuButton>
                </SidebarMenuItem>
              ))}
            </SidebarMenu>
          </SidebarGroupContent>
        </SidebarGroup>
      </SidebarContent>
    </Sidebar>
  );
}

function Router() {
  return (
    <Switch>
      <Route path="/" component={Dashboard} />
      <Route path="/detections" component={Detections} />
      <Route path="/dashboard-live" component={DashboardLive} />
      <Route path="/analytics" component={AnalyticsHistory} />
      <Route path="/training" component={Training} />
      <Route path="/routers" component={Routers} />
      <Route path="/whitelist" component={Whitelist} />
      <Route path="/public-lists" component={PublicLists} />
      <Route path="/services" component={Services} />
      <Route component={NotFound} />
    </Switch>
  );
}

function App() {
  const style = {
    "--sidebar-width": "16rem",
    "--sidebar-width-icon": "3rem",
  };

  return (
    <QueryClientProvider client={queryClient}>
      <TooltipProvider>
        <SidebarProvider style={style as React.CSSProperties}>
          <div className="flex h-screen w-full" data-testid="app-container">
            <AppSidebar />
            <div className="flex flex-col flex-1 overflow-hidden">
              <header className="flex items-center gap-2 p-4 border-b" data-testid="header">
                <SidebarTrigger data-testid="button-sidebar-toggle">
                  <Menu className="h-5 w-5" />
                </SidebarTrigger>
                <div className="flex-1">
                  <h1 className="text-sm font-medium text-muted-foreground" data-testid="text-header-title">
                    Intrusion Detection System
                  </h1>
                </div>
              </header>
              <main className="flex-1 overflow-auto" data-testid="main-content">
                <Router />
              </main>
            </div>
          </div>
        </SidebarProvider>
        <Toaster />
      </TooltipProvider>
    </QueryClientProvider>
  );
}

export default App;
