// app/page.tsx

"use client";

import { useEffect, useState } from "react";
import CitySelectModal from "@/components/common/CitySelectModal";
import { useAppDispatch } from "@/store/hooks";
import { setCity } from "@/store/slices/bookingSlice";

import { useRouter } from "next/navigation";

import HeroSection from "@/components/home/HeroSection";
import SearchSection from "@/components/home/SearchSection";
import PopularBikes from "@/components/home/PopularBikes";
import ExplorePlaces from "@/components/home/ExplorePlaces";
import LandingExtras from "@/components/home/LandingExtras";
import AppDownloadSection from "@/components/home/AppDownloadSection";
import ReviewsSection from "@/components/home/ReviewsSection";
import ExploreCities from "@/components/home/ExploreCities";
import ExplorePartners from "@/components/home/ExplorePartners";
import ExplorePickupPoints from "@/components/home/ExplorePickupPoints";

const toSlug = (city: string) => {
  return city.trim().replace(/\s+/g, "-");
};

export default function Home() {
  const dispatch = useAppDispatch();
  const router = useRouter();

  const [openCityModal, setOpenCityModal] = useState(false);

//  useEffect(() => {
//   const city = localStorage.getItem("selectedCity");
//   const vehicle = localStorage.getItem("selectedVehicle");

//   // ✅ Case 1: both exist → go to main page
//   if (city && vehicle) {
//     const category =
//       vehicle === "bike" ? "bike-rentals" : "car-rentals";

//     router.push(`/${toSlug(city.toLowerCase())}/${category}`);
//   }

//   // ✅ Case 2: only city → go to select flow
//   else if (city && !vehicle) {
//     router.push(`/${toSlug(city.toLowerCase())}/select`);
//   }

//   // ✅ Case 3: new user → open modal
//   else {
//     setOpenCityModal(true);
//   }
// }, []);


useEffect(() => {
  const city = localStorage.getItem("selectedCity");
  const vehicle = localStorage.getItem("selectedVehicle");
  const onboardingDone = localStorage.getItem("onboardingDone");

  // ✅ Existing user
  if (onboardingDone && city && vehicle) {
    const category =
      vehicle === "bike"
        ? "bike-rentals"
        : "car-rentals";

    router.push(`/${toSlug(city.toLowerCase())}/${category}`);

    return;
  }

  // ✅ New user
  setOpenCityModal(true);
}, []);

return (
  <>
    {/* Background Homepage Content */}
    <div
      className={
        openCityModal
          ? "blur-sm pointer-events-none select-none"
          : ""
      }
    >
      <HeroSection vehicle="bike" />

      <SearchSection searchParams={{}} />

      <PopularBikes
        vehicle="bike"
        searchParams={{}}
      />

      <ExplorePlaces />

      <ExploreCities />

      <ExplorePartners />

      <ExplorePickupPoints />

      <LandingExtras />

       <ReviewsSection />

      <AppDownloadSection />
    </div>

    {/* Modal */}
    <CitySelectModal
      open={openCityModal}
      onClose={(selectedCity?: string) => {
        if (selectedCity) {
          localStorage.setItem(
            "selectedCity",
            selectedCity
          );

          dispatch(setCity(selectedCity));

          setOpenCityModal(false);

          router.push(
            `/${toSlug(
              selectedCity.toLowerCase()
            )}/select`
          );
        }
      }}
    />
  </>
);
}