"use client";

import Image from "next/image";
import Link from "next/link";
import { Car } from "@/lib/data";
import { Fuel, Users, Gauge, Star } from "lucide-react";

interface CarCardProps {
  car: Car;
  priority?: boolean;
}

const WHATSAPP_NUMBER = "212651621512";

export default function CarCard({ car, priority = false }: CarCardProps) {
  const waMsg = encodeURIComponent(
    `Bonjour Setocar! 🚗\n\nJe suis intéressé(e) par la location de: *${car.name}*\n💰 Prix: ${car.pricePerDay} MAD/jour\n\nPouvez-vous me confirmer la disponibilité?\n\nMerci!`
  );

  return (
    <article
      className="card-hover"
      style={{
        background: "#1c2133",
        borderRadius: "10px",
        overflow: "hidden",
        border: "1px solid #2a3050",
        display: "flex",
        flexDirection: "column",
        position: "relative",
      }}
    >
      {/* Badge */}
      {car.badge && (
        <div
          style={{
            position: "absolute",
            top: "0.75rem",
            left: "0.75rem",
            zIndex: 10,
            background: "#e85c1a",
            color: "white",
            padding: "0.2rem 0.6rem",
            borderRadius: "2px",
            fontFamily: "var(--font-montserrat)",
            fontSize: "0.66rem",
            fontWeight: 800,
            textTransform: "uppercase",
            letterSpacing: "0.06em",
            boxShadow: "0 2px 8px rgba(232,92,26,0.4)",
          }}
        >
          {car.badge}
        </div>
      )}

      {/* Category chip */}
      <div
        style={{
          position: "absolute",
          top: "0.75rem",
          right: "0.75rem",
          zIndex: 10,
          background: "rgba(17,21,32,0.85)",
          color: "rgba(255,255,255,0.8)",
          padding: "0.18rem 0.55rem",
          borderRadius: "2px",
          fontFamily: "var(--font-montserrat)",
          fontSize: "0.66rem",
          fontWeight: 600,
          textTransform: "uppercase",
          letterSpacing: "0.05em",
          backdropFilter: "blur(6px)",
          border: "1px solid #2a3050",
        }}
      >
        {car.categoryLabel}
      </div>

      {/* Car Image */}
      <div
        style={{
          position: "relative",
          height: "190px",
          background: "#141926",
          overflow: "hidden",
        }}
      >
        <Image
          src={car.image}
          alt={`${car.name} - Location voiture Marrakech`}
          fill
          style={{ objectFit: "cover" }}
          sizes="(max-width: 768px) 100vw, (max-width: 1200px) 50vw, 33vw"
          priority={priority}
        />
      </div>

      {/* Content */}
      <div style={{ padding: "1.1rem", flex: 1, display: "flex", flexDirection: "column" }}>
        {/* Rating */}
        <div style={{ display: "flex", alignItems: "center", gap: "0.25rem", marginBottom: "0.45rem" }}>
          {Array.from({ length: 5 }, (_, i) => (
            <Star
              key={i}
              size={12}
              fill={i < Math.floor(car.rating) ? "#e85c1a" : "none"}
              color={i < Math.floor(car.rating) ? "#e85c1a" : "#3a4060"}
            />
          ))}
          <span
            style={{
              fontFamily: "var(--font-montserrat)",
              fontSize: "0.73rem",
              color: "rgba(255,255,255,0.45)",
              marginLeft: "0.2rem",
              textTransform: "none",
              letterSpacing: 0,
            }}
          >
            {car.rating} ({car.reviews} avis)
          </span>
        </div>

        {/* Name */}
        <h3
          style={{
            fontFamily: "var(--font-oswald)",
            fontSize: "1.15rem",
            color: "white",
            marginBottom: "0.75rem",
            letterSpacing: "0.03em",
          }}
        >
          {car.name}
        </h3>

        {/* Specs */}
        <div
          style={{
            display: "grid",
            gridTemplateColumns: "1fr 1fr 1fr",
            gap: "0.4rem",
            marginBottom: "1rem",
            padding: "0.65rem",
            background: "#141926",
            borderRadius: "6px",
            border: "1px solid #2a3050",
          }}
        >
          <SpecItem icon={<Fuel size={13} color="#e85c1a" />} label={car.fuel} />
          <SpecItem
            icon={<Gauge size={13} color="#e85c1a" />}
            label={car.transmission === "automatic" ? "Auto" : "Manuel"}
          />
          <SpecItem icon={<Users size={13} color="#e85c1a" />} label={`${car.seats} pl.`} />
        </div>

        {/* Price + CTA */}
        <div style={{ marginTop: "auto", display: "flex", alignItems: "center", justifyContent: "space-between" }}>
          <div>
            <span
              style={{
                fontFamily: "var(--font-oswald)",
                fontSize: "1.5rem",
                fontWeight: 700,
                color: "white",
                letterSpacing: "0.02em",
              }}
            >
              {car.pricePerDay}
            </span>
            <span
              style={{
                fontFamily: "var(--font-montserrat)",
                fontSize: "0.75rem",
                color: "rgba(255,255,255,0.45)",
                marginLeft: "0.25rem",
                textTransform: "none",
                letterSpacing: 0,
              }}
            >
              MAD/jour
            </span>
          </div>

          <div style={{ display: "flex", gap: "0.4rem" }}>
            <Link
              href={`/cars/${car.slug}`}
              id={`details-${car.slug}`}
              style={{
                padding: "0.45rem 0.85rem",
                border: "1px solid #2a3050",
                borderRadius: "4px",
                color: "rgba(255,255,255,0.7)",
                textDecoration: "none",
                fontFamily: "var(--font-montserrat)",
                fontSize: "0.78rem",
                fontWeight: 600,
                textTransform: "uppercase",
                letterSpacing: "0.04em",
                transition: "all 0.2s",
              }}
              onMouseEnter={(e) => {
                (e.currentTarget as HTMLElement).style.borderColor = "#e85c1a";
                (e.currentTarget as HTMLElement).style.color = "#e85c1a";
              }}
              onMouseLeave={(e) => {
                (e.currentTarget as HTMLElement).style.borderColor = "#2a3050";
                (e.currentTarget as HTMLElement).style.color = "rgba(255,255,255,0.7)";
              }}
            >
              Détails
            </Link>
            <a
              href={`https://wa.me/${WHATSAPP_NUMBER}?text=${waMsg}`}
              target="_blank"
              rel="noopener noreferrer"
              id={`book-${car.slug}`}
              style={{
                padding: "0.45rem 0.85rem",
                background: "#25d366",
                borderRadius: "4px",
                color: "white",
                textDecoration: "none",
                fontFamily: "var(--font-montserrat)",
                fontSize: "0.78rem",
                fontWeight: 700,
                textTransform: "uppercase",
                letterSpacing: "0.04em",
                display: "flex",
                alignItems: "center",
                gap: "0.3rem",
                transition: "all 0.2s",
              }}
            >
              <svg width="13" height="13" viewBox="0 0 24 24" fill="white">
                <path d="M17.472 14.382c-.297-.149-1.758-.867-2.03-.967-.273-.099-.471-.148-.67.15-.197.297-.767.966-.94 1.164-.173.199-.347.223-.644.075-.297-.15-1.255-.463-2.39-1.475-.883-.788-1.48-1.761-1.653-2.059-.173-.297-.018-.458.13-.606.134-.133.298-.347.446-.52.149-.174.198-.298.298-.497.099-.198.05-.371-.025-.52-.075-.149-.669-1.612-.916-2.207-.242-.579-.487-.5-.669-.51-.173-.008-.371-.01-.57-.01-.198 0-.52.074-.792.372-.272.297-1.04 1.016-1.04 2.479 0 1.462 1.065 2.875 1.213 3.074.149.198 2.096 3.2 5.077 4.487.709.306 1.262.489 1.694.625.712.227 1.36.195 1.871.118.571-.085 1.758-.719 2.006-1.413.248-.694.248-1.289.173-1.413-.074-.124-.272-.198-.57-.347m-5.421 7.403h-.004a9.87 9.87 0 01-5.031-1.378l-.361-.214-3.741.982.998-3.648-.235-.374a9.86 9.86 0 01-1.51-5.26c.001-5.45 4.436-9.884 9.888-9.884 2.64 0 5.122 1.03 6.988 2.898a9.825 9.825 0 012.893 6.994c-.003 5.45-4.437 9.884-9.885 9.884m8.413-18.297A11.815 11.815 0 0012.05 0C5.495 0 .16 5.335.157 11.892c0 2.096.547 4.142 1.588 5.945L.057 24l6.305-1.654a11.882 11.882 0 005.683 1.448h.005c6.554 0 11.89-5.335 11.893-11.893a11.821 11.821 0 00-3.48-8.413z" />
              </svg>
              Réserver
            </a>
          </div>
        </div>
      </div>
    </article>
  );
}

function SpecItem({ icon, label }: { icon: React.ReactNode; label: string }) {
  return (
    <div
      style={{
        display: "flex",
        flexDirection: "column",
        alignItems: "center",
        gap: "0.15rem",
        textAlign: "center",
      }}
    >
      {icon}
      <span
        style={{
          fontFamily: "var(--font-montserrat)",
          fontSize: "0.68rem",
          color: "rgba(255,255,255,0.55)",
          fontWeight: 500,
          textTransform: "none",
          letterSpacing: 0,
        }}
      >
        {label}
      </span>
    </div>
  );
}
