"use client";

import { useState } from "react";
import { 
  Search, MapPin, Calendar, Car, User, ArrowRight, ShieldCheck, Clock, ShieldAlert, Navigation
} from "lucide-react";
import { CarRentalCard } from "@/components/car-rental/CarRentalCard";
import { EmptyState } from "@/components/ui/EmptyState";
import { FadeInUp, StaggerContainer, StaggerItem } from "@/components/ui/motion";

interface CarRentalData {
  id: number;
  name: string;
  type: string | null;
  capacity: string | null;
  price: string | null;
  imageUrl: string | null;
  includeDriver: boolean;
}

export function CarRentalListingClient({ cars }: { cars: CarRentalData[] }) {
  const [activeTab, setActiveTab] = useState("Lepas Kunci");

  return (
    <div className="relative -mt-16 md:-mt-24 z-20 max-w-[1300px] mx-auto w-full px-4 sm:px-6">
      
      {/* 1. Advanced Car Search Widget */}
      <FadeInUp className="bg-white rounded-[24px] shadow-xl shadow-red-900/5 border border-gray-100 overflow-hidden mb-6">
        {/* Tabs */}
        <div className="flex border-b border-gray-100 px-4 md:px-8 pt-4 gap-8">
          {[
            { id: "Lepas Kunci", icon: Car },
            { id: "Dengan Sopir", icon: User }
          ].map((tab) => (
            <button
              key={tab.id}
              onClick={() => setActiveTab(tab.id)}
              className={`flex items-center gap-2 pb-4 text-[15px] font-bold transition-colors relative ${
                activeTab === tab.id ? "text-[#d32f2f]" : "text-gray-500 hover:text-gray-700"
              }`}
            >
              <tab.icon className="w-5 h-5" />
              {tab.id}
              {activeTab === tab.id && (
                <div className="absolute bottom-0 left-0 right-0 h-1 bg-[#d32f2f] rounded-t-full" />
              )}
            </button>
          ))}
        </div>

        {/* Form Inputs */}
        <div className="p-6 md:p-8 flex flex-col md:flex-row items-center gap-4">
          <div className="flex-1 grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4 gap-4 w-full">
            {[
              { icon: MapPin, label: "Lokasi Pengambilan", value: "Pilih lokasi pengambilan" },
              { icon: MapPin, label: "Lokasi Pengembalian", value: "Pilih lokasi pengembalian" },
              { icon: Calendar, label: "Tanggal Mulai", value: "Pilih tanggal & waktu" },
              { icon: Calendar, label: "Tanggal Selesai", value: "Pilih tanggal & waktu" },
            ].map((field, idx) => (
              <div key={idx} className="col-span-1 bg-white border-b-2 border-gray-100 hover:border-gray-200 transition-colors p-2 pb-3 cursor-pointer">
                <p className="text-[12px] text-gray-500 font-medium mb-2">{field.label}</p>
                <div className="flex items-center justify-between">
                  <div className="flex items-center gap-2">
                    <field.icon className="w-4 h-4 text-gray-400" />
                    <p className="text-[14px] text-gray-400 font-medium truncate">{field.value}</p>
                  </div>
                  <ChevronDown className="w-4 h-4 text-gray-400" />
                </div>
              </div>
            ))}
          </div>

          <button className="w-full md:w-auto bg-[#d32f2f] text-white px-8 py-4 rounded-xl font-bold flex items-center justify-center gap-2 hover:bg-[#b71c1c] transition-colors shadow-lg shadow-red-900/20 shrink-0 h-[64px] ml-0 md:ml-4">
            <Search className="w-5 h-5" />
            Cari Mobil
          </button>
        </div>
      </FadeInUp>

      {/* 2. Benefits Bar (Pink Background) */}
      <FadeInUp className="bg-[#fff5f5] rounded-[24px] p-6 md:px-12 grid grid-cols-1 sm:grid-cols-2 md:grid-cols-4 gap-6 mb-20 border border-red-50">
        {[
          { icon: ShieldCheck, title: "Asuransi All Risk", desc: "Perlindungan menyeluruh selama perjalanan" },
          { icon: Car, title: "Gratis Antar Jemput", desc: "Layanan antar jemput di area tertentu" },
          { icon: Gauge, title: "Kilometer Unlimited", desc: "Jarak tempuh bebas tanpa batasan" },
          { icon: ShieldAlert, title: "Bantuan Darurat 24/7", desc: "Siap membantu Anda kapan pun dibutuhkan" }
        ].map((item, idx) => (
          <div key={idx} className="flex items-center gap-4">
            <div className="w-12 h-12 rounded-full bg-white flex items-center justify-center shrink-0 shadow-sm border border-red-100">
              <item.icon className="w-6 h-6 text-[#d32f2f]" />
            </div>
            <div>
              <h4 className="text-[14px] font-bold text-slate-900 mb-0.5">{item.title}</h4>
              <p className="text-[11px] text-gray-500 leading-snug max-w-[150px]">{item.desc}</p>
            </div>
          </div>
        ))}
      </FadeInUp>

      {/* 3. Popular Cars Grid */}
      <div className="mb-24">
        <div className="flex items-end justify-between mb-8">
          <div>
            <p className="text-[11px] font-bold text-[#d32f2f] uppercase tracking-wider mb-1">Pilihan Terpopuler</p>
            <h2 className="text-2xl md:text-3xl font-bold text-slate-900">Mobil Favorit Kami</h2>
            <p className="text-sm text-gray-500 mt-2">Berbagai pilihan mobil terbaik sesuai kebutuhan perjalanan Anda</p>
          </div>
          <button className="hidden sm:flex items-center gap-2 px-6 py-2.5 rounded-full border border-red-200 text-[#d32f2f] font-bold text-sm hover:bg-red-50 transition-colors">
            Lihat Semua Mobil <ArrowRight className="w-4 h-4" />
          </button>
        </div>

        {cars.length === 0 ? (
          <EmptyState message="Belum ada mobil yang tersedia." />
        ) : (
          <StaggerContainer className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-6">
            {cars.map((car) => (
              <StaggerItem key={car.id} className="h-full">
                <CarRentalCard car={car} />
              </StaggerItem>
            ))}
          </StaggerContainer>
        )}
      </div>
      
    </div>
  );
}

// Simple internal icon for dropdown to avoid adding more imports if not needed
function ChevronDown(props: any) {
  return <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...props}><path d="m6 9 6 6 6-6"/></svg>
}
function Gauge(props: any) {
  return <svg xmlns="http://www.w3.org/2000/svg" width="24" height="24" viewBox="0 0 24 24" fill="none" stroke="currentColor" strokeWidth="2" strokeLinecap="round" strokeLinejoin="round" {...props}><path d="m12 14 4-4"/><path d="M3.34 19a10 10 0 1 1 17.32 0"/></svg>
}
