"use client";

import { useEffect, useState } from "react";
import { getAllCities } from "@/api/bookingApi";

import { useRouter } from "next/navigation";
import { useAppDispatch, useAppSelector } from "@/store/hooks";
import { setCity } from "@/store/slices/bookingSlice";


const toSlug = (city: string) => {
  return city.trim().replace(/\s+/g, "-");
};

export default function OurPresence() {
  const [cities, setCities] = useState<string[]>([]);
 const { city, vehicle } = useAppSelector(
  (state) => state.booking
);

    const router = useRouter();

const dispatch = useAppDispatch();

const selectedVehicle = vehicle;

// const { vehicle } = useAppSelector(
//   (state) => state.booking
// );

  useEffect(() => {
    const fetchCities = async () => {
      try {
        const data = await getAllCities();
        setCities(data || []);
      } catch (err) {
        console.error("Failed to fetch cities");
      }
    };

    fetchCities();
  }, []);

  useEffect(() => {
  const savedCity = localStorage.getItem("selectedCity");
  const savedVehicle = localStorage.getItem("selectedVehicle");

  if (savedCity) {
    dispatch(setCity(savedCity));
  }

  // If you have setVehicle action
  // if (savedVehicle) {
  //   dispatch(setVehicle(savedVehicle));
  // }
}, [dispatch]);

  return (
    <section className="bg-[#FFF9E6] py-12 px-4">
      <div className="max-w-[1400px] mx-auto">

        {/* TOP CONTENT */}
       {city && (
  <div className="mb-12 text-[#2D2D2D]">

    <h2 className="text-2xl md:text-3xl font-bold mb-4">
      {selectedVehicle === "car"
        ? `Cars for Rent in ${city}`
        : `Bike Rentals in ${city}`}
    </h2>

    <p className="text-sm md:text-[15px] leading-7 text-gray-700 mb-8">
      {selectedVehicle === "car"
        ? `Choose from a wide range of self-drive cars in ${city}. Enjoy affordable pricing, flexible booking durations, and a smooth travel experience with Ridoo.`
        : `Experience the best of ${city} when you rent a bike with Ridoo. Explore the city comfortably with affordable and reliable bike rental services for daily rides, commuting, and weekend trips.`}
    </p>

    <h3 className="text-2xl font-bold mb-4">
      {selectedVehicle === "car"
        ? `Why Rent a Car in ${city}?`
        : `Why Rent a Bike in ${city}?`}
    </h3>

    <p className="text-sm md:text-[15px] leading-7 text-gray-700">
      {selectedVehicle === "car"
        ? `Book self-drive cars in ${city} with instant confirmation, transparent pricing, and well-maintained vehicles for business trips, family outings, or weekend getaways.`
        : `Book bikes in ${city} with instant confirmation, affordable pricing, and well-maintained vehicles for commuting, sightseeing, and weekend adventures.`}
    </p>

  </div>
)}

        {/* TITLE */}
        <div className="text-center mb-8">
          <h2 className="text-3xl md:text-4xl font-bold uppercase text-[#2D2D2D]">
            Our Presence
          </h2>

          <div className="w-16 h-1 bg-yellow-500 mx-auto mt-3 rounded-full" />
        </div>

        {/* CITY LIST */}
        <div className="flex flex-wrap justify-center gap-x-3 gap-y-2">

          {cities.map((city, index) => (
  <div
    key={index}
    className="flex flex-wrap items-center gap-2"
  >

    {/* BIKE */}
    <button
      onClick={() => {
        // UI update
       

        // redux update
        dispatch(setCity(city));

        // localStorage
        localStorage.setItem(
          "selectedCity",
          city
        );

        localStorage.setItem(
          "selectedVehicle",
          "bike"
        );

        // route
        router.push(
          `/${toSlug(
            city.toLowerCase()
          )}/bike-rentals`
        );
      }}
      className="text-sm md:text-[15px] transition-all duration-200 hover:text-[#E6A500] text-[#3D3D3D]"
    >
      Bikes for rent in {city}
    </button>

    <span className="text-gray-400">•</span>

    {/* CAR */}
    <button
      onClick={() => {
        // UI update
        

        // redux update
        dispatch(setCity(city));

        // localStorage
        localStorage.setItem(
          "selectedCity",
          city
        );

        localStorage.setItem(
          "selectedVehicle",
          "car"
        );

        // route
        router.push(
          `/${toSlug(
            city.toLowerCase()
          )}/car-rentals`
        );
      }}
      className="text-sm md:text-[15px] transition-all duration-200 hover:text-[#E6A500] text-[#3D3D3D]"
    >
      Cars for rent in {city}
    </button>

  </div>
))}
        </div>
      </div>
    </section>
  );
}