/**
 * Application Routes Configuration
 * 应用路由配置
 */

import { Routes, Route, Navigate } from "react-router-dom";
import { ProtectedRoute } from "./RouteGuard";
import AppShell from "@/shared/components/layout/AppShell";
import { getPageInfo, type NavItem } from "../constants/navigation";
import { useLocation } from "react-router-dom";
import type { AuthResponse } from "@/api";

// Feature pages
import AuthPage from "@/features/auth/pages/AuthPage";
import HomePage from "@/features/home/pages/HomePage";
import ClusterConfigPage from "@/features/configuration/clusters/pages/ClusterConfigPage";
import RegistryConfigPage from "@/features/configuration/registries/pages/RegistryConfigPage";
import ArtifactBrowserPage from "@/features/artifact/registries/pages/ArtifactBrowserPage";
import InstancesManagementPage from "@/features/artifact/instances/pages/InstancesManagementPage";
import MonitoringClustersPage from "@/features/monitoring/clusters/pages/MonitoringClustersPage";
import { ApiTest } from "@/components/ApiTest";

interface AppRoutesProps {
  isAuthenticated: boolean;
  userName?: string;
  navItems: NavItem[];
  onLogin: (tokens: AuthResponse) => void;
  onLogout: () => void;
}

/**
 * Main application routes
 */
export const AppRoutes = ({
  isAuthenticated,
  userName = "User",
  navItems,
  onLogin,
  onLogout,
}: AppRoutesProps) => {
  const location = useLocation();
  const pageInfo = getPageInfo(location.pathname);

  return (
    <Routes>
      {/* Public route - Authentication page */}
      <Route
        path="/"
        element={
          isAuthenticated ? (
            <Navigate to="/home" replace />
          ) : (
            <AuthPage onLogin={onLogin} />
          )
        }
      />

      {/* Protected routes - wrapped in AppShell */}
      <Route
        path="/home"
        element={
          <ProtectedRoute isAuthenticated={isAuthenticated}>
            <AppShell
              title={pageInfo.title}
              icon={pageInfo.icon}
              userName={userName}
              navItems={navItems}
              onSignOut={onLogout}
            >
              <HomePage />
            </AppShell>
          </ProtectedRoute>
        }
      />

      <Route
        path="/configuration/clusters"
        element={
          <ProtectedRoute isAuthenticated={isAuthenticated}>
            <AppShell
              title={pageInfo.title}
              icon={pageInfo.icon}
              userName={userName}
              navItems={navItems}
              onSignOut={onLogout}
            >
              <ClusterConfigPage />
            </AppShell>
          </ProtectedRoute>
        }
      />

      <Route
        path="/configuration/registries"
        element={
          <ProtectedRoute isAuthenticated={isAuthenticated}>
            <AppShell
              title={pageInfo.title}
              icon={pageInfo.icon}
              userName={userName}
              navItems={navItems}
              onSignOut={onLogout}
            >
              <RegistryConfigPage />
            </AppShell>
          </ProtectedRoute>
        }
      />

      <Route
        path="/artifact/registries"
        element={
          <ProtectedRoute isAuthenticated={isAuthenticated}>
            <AppShell
              title={pageInfo.title}
              icon={pageInfo.icon}
              userName={userName}
              navItems={navItems}
              onSignOut={onLogout}
            >
              <ArtifactBrowserPage />
            </AppShell>
          </ProtectedRoute>
        }
      />

      <Route
        path="/artifact/instances"
        element={
          <ProtectedRoute isAuthenticated={isAuthenticated}>
            <AppShell
              title={pageInfo.title}
              icon={pageInfo.icon}
              userName={userName}
              navItems={navItems}
              onSignOut={onLogout}
            >
              <InstancesManagementPage />
            </AppShell>
          </ProtectedRoute>
        }
      />

      <Route
        path="/monitoring/clusters"
        element={
          <ProtectedRoute isAuthenticated={isAuthenticated}>
            <AppShell
              title={pageInfo.title}
              icon={pageInfo.icon}
              userName={userName}
              navItems={navItems}
              onSignOut={onLogout}
            >
              <MonitoringClustersPage />
            </AppShell>
          </ProtectedRoute>
        }
      />

      {/* API Test page - Public for testing */}
      <Route path="/api-test" element={<ApiTest />} />

      {/* Legacy path compatibility - redirects */}
      <Route path="/config" element={<Navigate to="/configuration/clusters" replace />} />
      <Route path="/config/cluster" element={<Navigate to="/configuration/clusters" replace />} />
      <Route path="/config/clusters" element={<Navigate to="/configuration/clusters" replace />} />
      <Route path="/config/app" element={<Navigate to="/configuration/registries" replace />} />
      <Route path="/config/registry" element={<Navigate to="/configuration/registries" replace />} />
      <Route path="/config/registries" element={<Navigate to="/configuration/registries" replace />} />
      <Route path="/artifact/registry" element={<Navigate to="/artifact/registries" replace />} />
      <Route path="/artifact/instance" element={<Navigate to="/artifact/instances" replace />} />
      <Route path="/monitor" element={<Navigate to="/monitoring/clusters" replace />} />
      <Route path="/cluster" element={<Navigate to="/monitoring/clusters" replace />} />
      <Route path="/cluster/monitor" element={<Navigate to="/monitoring/clusters" replace />} />
      <Route path="/registry" element={<Navigate to="/artifact/registries" replace />} />
      <Route path="/register" element={<Navigate to="/" replace />} />
    </Routes>
  );
};


