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

import { Routes, Route, Navigate } from "react-router-dom";
import type { ReactNode } from "react";
import { ProtectedRoute } from "./RouteGuard";
import { AccessDeniedPage } from "./AccessDeniedPage";
import AppShell from "@/shared/components/layout/AppShell";
import { getPageInfo, type NavItem } from "../constants/navigation";
import { useLocation, useNavigate } from "react-router-dom";
import type { AuthResponse } from "@/api";
import type { User } from "../providers/AuthContext";
import { canAccessRoute } from "../providers/auth-model";

// 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 UserManagementPage from "@/features/configuration/users/pages/UserManagementPage";
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;
  user: User | null;
  navItems: NavItem[];
  onLogin: (tokens: AuthResponse) => void;
  onLogout: () => void;
}

/**
 * Main application routes
 */
export const AppRoutes = ({
  isAuthenticated,
  userName = "User",
  user,
  navItems,
  onLogin,
  onLogout,
}: AppRoutesProps) => {
  const location = useLocation();
  const navigate = useNavigate();
  const pageInfo = getPageInfo(location.pathname);
  const shell = (children: ReactNode) => (
    <AppShell
      title={pageInfo.title}
      icon={pageInfo.icon}
      userName={userName}
      userRole={user?.role}
      navItems={navItems}
      onSignOut={onLogout}
    >
      {children}
    </AppShell>
  );
  const protectedPage = (path: string, children: ReactNode) => (
    <ProtectedRoute isAuthenticated={isAuthenticated} isAllowed={canAccessRoute(path, user)}>
      {shell(children)}
    </ProtectedRoute>
  );

  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={protectedPage("/home", <HomePage />)}
      />

      <Route
        path="/configuration/clusters"
        element={protectedPage("/configuration/clusters", <ClusterConfigPage />)}
      />

      <Route
        path="/configuration/registries"
        element={protectedPage("/configuration/registries", <RegistryConfigPage />)}
      />

      <Route
        path="/artifact/registries"
        element={protectedPage("/artifact/registries", <ArtifactBrowserPage />)}
      />

      <Route
        path="/artifact/instances"
        element={protectedPage("/artifact/instances", <InstancesManagementPage />)}
      />

      <Route
        path="/monitoring/clusters"
        element={protectedPage("/monitoring/clusters", <MonitoringClustersPage />)}
      />

      <Route
        path="/admin/*"
        element={protectedPage("/admin", <AccessDeniedPage onBackHome={() => navigate("/home")} />)}
      />

      <Route
        path="/configuration/users"
        element={protectedPage("/configuration/users", <UserManagementPage />)}
      />

      <Route
        path="/configuration/workspaces"
        element={protectedPage("/configuration/workspaces", <AccessDeniedPage onBackHome={() => navigate("/home")} />)}
      />

      <Route
        path="/forbidden"
        element={
          <ProtectedRoute isAuthenticated={isAuthenticated}>
            {shell(<AccessDeniedPage onBackHome={() => navigate("/home")} />)}
          </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>
  );
};
