// app/offers/OffersClient.tsx

"use client";

import { useEffect, useState } from "react";
import Navbar from "@/components/common/Navbar";
import { getAllCoupons } from "@/api/bookingApi";

import Image from "next/image";

export default function OffersPage() {
  const [vehicle, setVehicle] = useState<"bike" | "car">("bike");
  const [coupons, setCoupons] = useState<any[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetchCoupons();
  }, []);



  const fetchCoupons = async () => {
  try {
    const data = await getAllCoupons();

    const activeCoupons = (data || []).filter(
      (item: any) => item.is_active
    );

    setCoupons(activeCoupons);
  } catch (err) {
    console.error(err);
  } finally {
    setLoading(false);
  }
};

function OfferCard({ coupon }: any) {
  const formatDate = (dateStr: string) => {
    const date = new Date(dateStr);
    return date.toLocaleDateString("en-IN", {
      day: "2-digit",
      month: "short",
      year: "numeric",
    });
  };

  const isPercentage = coupon.discount_type === "PERCENTAGE";

  return (
    <div className="bg-white rounded-xl shadow-md p-3 hover:shadow-lg transition">

      {/* BANNER */}
      <div className="relative rounded-lg overflow-hidden bg-gradient-to-r from-yellow-300 to-pink-400 flex items-center h-[140px] md:h-[160px]">

        {/* IMAGE */}
        <Image
          alt="Bike"
          src="/bike.webp"
           width={180}
  height={54}
          className="h-[100px] md:h-[140px] ml-2 object-contain"
        />

        {/* TEXT */}
        <div className="text-white ml-auto mr-3 md:mr-4 text-right">
          <p className="text-xs md:text-sm font-semibold">FLAT</p>

          <p className="text-xl md:text-3xl font-bold leading-tight">
            {isPercentage
              ? `${coupon.discount_value}%`
              : `₹${parseInt(coupon.discount_value)}`}
            <span className="text-xs md:text-sm"> off</span>
          </p>

          <p className="text-[10px] md:text-xs mt-1 leading-tight">
            on Daily Rentals <br />
            usecode- <b>{coupon.code}</b>
          </p>
        </div>
      </div>

      {/* TEXT */}
      <div className="mt-3">
        <h3 className="font-semibold text-base md:text-lg">
          {coupon.code}
        </h3>

        <p className="text-xs text-gray-500 line-clamp-2">
          {coupon.description}
        </p>

        <p className="text-[11px] md:text-xs text-gray-400 mt-1">
          Valid till {formatDate(coupon.valid_till)}
        </p>
      </div>

      {/* ACTION */}
      {/* <div className="flex items-center justify-between mt-3 md:mt-4">
        <button className="bg-primary text-white text-xs md:text-sm px-3 md:px-4 py-2 rounded-md">
          Rent Now →
        </button>

        <span className="text-[11px] md:text-xs text-gray-500 cursor-pointer">
          Terms & Condition
        </span>
      </div> */}
    </div>
  );
}

  return (
    <>
      {/* <Navbar vehicle={vehicle} setVehicle={setVehicle} /> */}

      <div className="px-4 md:px-10 py-6 md:py-10 bg-[#f7f7f7] min-h-screen">

        {/* TITLE */}
      <div className="max-w-4xl mx-auto text-center mb-14">
  <h1 className="inline-block text-4xl md:text-5xl font-bold text-primary relative">
    Offers
    <span className="absolute left-0 -bottom-2 w-full h-1 bg-primary rounded-full"></span>
  </h1>

  <p className="mt-8 text-gray-600 text-lg leading-8">
    Save more on every ride with our exclusive offers and seasonal discounts.
    Whether you're planning a weekend getaway, a business trip, or your daily
    commute, Ridoo provides exciting coupon codes to help you enjoy premium
    bike rentals at affordable prices. Browse our latest offers below and
    apply the coupon during checkout to unlock instant savings.
  </p>
</div>

        {/* CONTENT */}
       {loading ? (
  <div className="flex flex-col justify-center items-center h-[300px] gap-3">
    <Image
      src="/profile.gif"
      alt="Loading..."
       width={180}
  height={54}
      className="w-16 h-16 md:w-20 md:h-20"
    />
    <p className="text-sm text-gray-500">Loading offers...</p>
  </div>
) : (
          <div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 gap-4 md:gap-6">
            {coupons.map((item, index) => (
              <OfferCard key={index} coupon={item} />
            ))}
          </div>
        )}
      </div>
    </>
  );
}