// App.js
import React, { useState, useEffect } from "react";
import { Navbar } from "./shared/components";
import HomePage from "./features/chat/pages/HomePage";
import { VoiceChatPage } from "./features/voice";
import { SwarmControlPage } from "./features/swarm";
import { AgentHarnessPage, AgentDrillDown } from "./features/agents";
import LoginPage from "./features/auth/pages/LoginPage";
import SettingsPage from "./features/settings/pages/SettingsPage";
import ProfilePage from "./features/profile/pages/ProfilePage";
import { NodesPage } from "./features/nodes";
import SkillsPage from "./features/skills/pages/SkillsPage";
import { getUserStatus, logout, getUserProfile } from "./services/apiService";

const Icon = ({ path, onClick, className }) => (
  <svg
    xmlns="http://www.w3.org/2000/svg"
    viewBox="0 0 24 24"
    fill="none"
    stroke="currentColor"
    strokeWidth="2"
    strokeLinecap="round"
    strokeLinejoin="round"
    className={`w-6 h-6 cursor-pointer transition-colors duration-200 ${className}`}
    onClick={onClick}
  >
    <path d={path} />
  </svg>
);

export default function App() {
  const [currentPage, setCurrentPage] = useState("home");
  const [isSidebarOpen, setIsSidebarOpen] = useState(false);
  const [isLoggedIn, setIsLoggedIn] = useState(false);
  const [userId, setUserId] = useState(null);
  const [userProfile, setUserProfile] = useState(null);

  const authenticatedPages = ["voice-chat", "swarm-control", "agents", "agents-drilldown", "settings", "profile", "nodes", "skills"];
  const pageToPath = {
    "home": "/",
    "voice-chat": "/voice",
    "swarm-control": "/swarm",
    "agents": "/agents",
    "agents-drilldown": "/agents-drilldown",
    "settings": "/settings",
    "profile": "/profile",
    "nodes": "/nodes",
    "skills": "/skills",
    "login": "/login"
  };
  const pathToPage = Object.fromEntries(Object.entries(pageToPath).map(([pk, pv]) => [pv, pk]));

  // Sync state with URL on mount and handle popstate
  useEffect(() => {
    const handlePopState = () => {
      const path = window.location.pathname;
      if (path.startsWith("/agents/drilldown/")) {
        setCurrentPage("agents-drilldown");
      } else {
        const page = pathToPage[path] || "home";
        setCurrentPage(page);
      }
    };

    window.addEventListener("popstate", handlePopState);

    // Initial sync
    const initialPath = window.location.pathname;
    let initialPage = "home";
    if (initialPath.startsWith("/agents/drilldown/")) {
        initialPage = "agents-drilldown";
    } else {
        initialPage = pathToPage[initialPath] || "home";
    }
    
    if (initialPage !== currentPage) {
      setCurrentPage(initialPage);
    }

    return () => window.removeEventListener("popstate", handlePopState);
  }, []);


  useEffect(() => {
    const urlParams = new URLSearchParams(window.location.search);
    const userIdFromUrl = urlParams.get('user_id');
    const tokenFromUrl = urlParams.get('token');

    if (userIdFromUrl) {
      localStorage.setItem('userId', userIdFromUrl);
      console.log('User ID from URL saved to localStorage:', userIdFromUrl);
    }
    
    if (tokenFromUrl) {
      localStorage.setItem('authToken', tokenFromUrl);
      console.log('Auth Token from URL saved to localStorage');
    }

    if (userIdFromUrl || tokenFromUrl) {
      // Clean up URL after capturing credentials
      window.history.replaceState({}, document.title, window.location.pathname);
    }
  }, []);

  useEffect(() => {
    const checkLoginStatus = async () => {
      const storedUserId = localStorage.getItem("userId");

      if (storedUserId) {
        try {
          const status = await getUserStatus(storedUserId);
          if (status.is_logged_in) {
            setIsLoggedIn(true);
            setUserId(storedUserId);
            // Fetch profile on success
            const profile = await getUserProfile();
            setUserProfile(profile);

            if (currentPage === "login") {
              setCurrentPage("home");
            }
          } else {
            setIsLoggedIn(false);
            setUserId(null);
            setUserProfile(null);
            localStorage.removeItem("userId");
            if (authenticatedPages.includes(currentPage)) {
              setCurrentPage("login");
            }
          }
        } catch (error) {
          console.error("Failed to check user status:", error);
          setIsLoggedIn(false);
          setUserId(null);
          setUserProfile(null);
          localStorage.removeItem("userId");
          if (authenticatedPages.includes(currentPage)) {
            setCurrentPage("login");
          }
        }
      } else {
        setIsLoggedIn(false);
        setUserId(null);
        setUserProfile(null);
        if (authenticatedPages.includes(currentPage)) {
          setCurrentPage("login");
        }
      }
    };

    checkLoginStatus();
  }, [currentPage]);

  const handleLogout = async () => {
    try {
      await logout();
      setIsLoggedIn(false);
      setUserId(null);
      setUserProfile(null);
      localStorage.removeItem("userId");
      setCurrentPage("home");
    } catch (error) {
      console.error("Logout failed:", error);
    }
  };

  const handleNavigate = (page) => {
    // Handle parameterized paths (paths starting with '/')
    if (page.startsWith("/")) {
      const path = page;
      if (path.startsWith("/agents/drilldown/")) {
        setCurrentPage("agents-drilldown");
      } else {
        setCurrentPage(pathToPage[path] || "home");
      }
      window.history.pushState({}, "", path);
      return;
    }

    if (authenticatedPages.includes(page) && !isLoggedIn) {
      setCurrentPage("login");
      window.history.pushState({}, "", pageToPath["login"]);
    } else {
      setCurrentPage(page);
      window.history.pushState({}, "", pageToPath[page] || "/");
    }
  };

  const toggleSidebar = () => {
    setIsSidebarOpen(!isSidebarOpen);
  };

  const renderPage = () => {
    switch (currentPage) {
      case "home":
        // Pass both isLoggedIn and handleLogout to HomePage
        return <HomePage onNavigate={handleNavigate} isLoggedIn={isLoggedIn} />;
      case "voice-chat":
        return <VoiceChatPage Icon={Icon} />;
      case "swarm-control":
        return <SwarmControlPage onNavigate={handleNavigate} Icon={Icon} />;
      case "settings":
        // Only admins can see global settings
        if (userProfile?.role !== "admin") {
          return <ProfilePage onLogout={handleLogout} />;
        }
        return <SettingsPage />;
      case "profile":
        return <ProfilePage onLogout={handleLogout} />;
      case "nodes":
        return <NodesPage user={userProfile} />;
      case "skills":
        return <SkillsPage user={userProfile} Icon={Icon} />;
      case "agents":
        return <AgentHarnessPage onNavigate={handleNavigate} />;
      case "agents-drilldown":
        const dashIndex = window.location.pathname.lastIndexOf("/");
        const agentId = window.location.pathname.substring(dashIndex + 1);
        return <AgentDrillDown agentId={agentId} onNavigate={handleNavigate} />;
      case "login":
        return <LoginPage />;
      default:
        return <HomePage onNavigate={handleNavigate} isLoggedIn={isLoggedIn} />;
    }
  };

  return (
    <div className="flex h-screen overflow-hidden bg-gray-100 dark:bg-gray-900 font-sans">
      {currentPage !== "login" && (
        <Navbar
          isOpen={isSidebarOpen}
          onToggle={toggleSidebar}
          onNavigate={handleNavigate}
          onLogout={handleLogout}
          isLoggedIn={isLoggedIn}
          user={userProfile}
          Icon={Icon}
        />
      )}
      <div className="flex-grow overflow-hidden">
        {renderPage()}
      </div>
    </div>
  );
}