import { BrowserRouter as Router, Routes, Route, useLocation, Navigate } from "react-router-dom";
import NotFound from "./Pages/NotFound";
import Login from "./Pages/Login";
import AdminDashboard from "./Pages/AdminDashboard";
import AdsPage from "./Pages/Ads";
import PartnersPage from "./Pages/PartnersPage";
import AddPartner from "./Pages/AddPartner";
import ManagePartner from "./Pages/ManagePartner";
import ClientsPage from "./Pages/ClientsPage";
import AddClient from "./Pages/AddClient";
import ManageClient from "./Pages/ManageClient";
import ClientPartnerMapping from "./Pages/ClientPartnerMapping";
import NewAdConfiguration from "./Pages/NewAdConfiguration";
import Header from "./Components/Header";
import ManageFilesOrder from "./Pages/ManageFilesOrderPage";
import SettingsPage from "./Pages/SettingsPage";
import YouTubePlayer from "./Pages/YT";

const Layout = () => {
  const location = useLocation();
  const getLoginState = () => {
    const storedLogin = JSON.parse(localStorage.getItem('loggedIn'));
    if (storedLogin && storedLogin.value) {
      const expirationTime = 1 * 60 * 60 * 1000; // 1 hour expiration
      if (Date.now() - storedLogin.timestamp < expirationTime) {
        return true;
      } else {
        localStorage.removeItem('loggedIn'); // Expired, remove
        return false;
      }
    }
    return false;
  };

  const isLoggedIn = getLoginState();

  const hideHeaderRoutes = ["/", "/login"];
  const isAdsPage = location.pathname.startsWith("/ads/");



  // App.js
  const ProtectedRoute = ({ children }) => {
    const location = useLocation();
    const storedLogin = localStorage.getItem('loggedIn');
    let isLoggedIn = false; // Default to false

    if (storedLogin) {
      try {
        const parsedLogin = JSON.parse(storedLogin);
        isLoggedIn = parsedLogin.value; // Access the 'value' property
      } catch (error) {
        console.error("Error parsing login data:", error);
        // Handle parsing error (e.g., clear localStorage)
        localStorage.removeItem('loggedIn');
      }
    }

    console.log(isLoggedIn); // Log the boolean value

    const isStaticFile = location.pathname.endsWith('.css') || location.pathname.endsWith('.js');

    if (isStaticFile) {
      return children; // Allow access to static files
    }

    return isLoggedIn ? children : <Navigate to="/login" />;
  };

  return (
    <>
      {!hideHeaderRoutes.includes(location.pathname) && !isAdsPage && <Header />}
      <Routes>
        <Route path="/" element={<Login />} />
        <Route path="/login" element={<Login />} />
        <Route path="/yt" element={<YouTubePlayer />} />
        <Route path="/ads/:partnerid/:screenName" element={<AdsPage />} />

        <Route path="/admin-dashboard" element={<ProtectedRoute><AdminDashboard /></ProtectedRoute>} />
        <Route path="/partners" element={<ProtectedRoute><PartnersPage /></ProtectedRoute>} />
        <Route path="/add-partner" element={<ProtectedRoute><AddPartner /></ProtectedRoute>} />
        <Route path="/manage-partner/:id" element={<ProtectedRoute><ManagePartner /></ProtectedRoute>} />
        <Route path="/clients" element={<ProtectedRoute><ClientsPage /></ProtectedRoute>} />
        <Route path="/add-client" element={<ProtectedRoute><AddClient /></ProtectedRoute>} />
        <Route path="/manage-client/:id" element={<ProtectedRoute><ManageClient /></ProtectedRoute>} />
        <Route path="/Client-Partner-Mapping" element={<ProtectedRoute><ClientPartnerMapping /></ProtectedRoute>} />
        <Route path="/new-ad-configuration" element={<ProtectedRoute><NewAdConfiguration /></ProtectedRoute>} />
        <Route path="/ads-order-configuration/:id" element={<ProtectedRoute><ManageFilesOrder /></ProtectedRoute>} />
        <Route path="/settings" element={<ProtectedRoute><SettingsPage /></ProtectedRoute>} />

        <Route path="*" element={<NotFound />} />
      </Routes>
    </>
  );
};

function App() {
  return (
    <Router>
      <Layout />
    </Router>
  );
}

export default App;