"use client";

import { useEffect, useState } from "react";
import { StarIcon, ArrowRightIcon } from "./icons";

type Testimonial = {
  name: string;
  vehicle: string;
  review: string;
  rating: number;
  color: string;
  initials: string;
};

const TESTIMONIALS: Testimonial[] = [
  {
    name: "Priya Sharma",
    vehicle: "Hyundai Creta Owner, Delhi",
    review:
      "My car was blocking someone in our society parking lot early morning. Instead of banging on my door, they just scanned the QR code and messaged me. No number exchanged, problem sorted in two minutes.",
    rating: 5,
    color: "#5C7CFA",
    initials: "PS",
  },
  {
    name: "Rohan Mehta",
    vehicle: "Maruti Suzuki Swift Owner, Pune",
    review:
      "Parked at a mall in Pune and came back to a small scratch on my bumper. The other driver called me straight through VehCall to sort it out — didn't have to hand out my personal number to a stranger.",
    rating: 5,
    color: "#7C6CFD",
    initials: "RM",
  },
  {
    name: "Ananya Iyer",
    vehicle: "Tata Nexon Owner, Bengaluru",
    review:
      "Someone noticed I'd left my parking lights on outside my apartment. Got a message through VehCall before my battery drained. Such a simple idea, but genuinely useful for daily life in Bengaluru traffic.",
    rating: 5,
    color: "#9B6BFF",
    initials: "AI",
  },
  {
    name: "Vikram Singh",
    vehicle: "Fleet Manager, Chennai",
    review:
      "We run a fleet of 60+ delivery vehicles across Chennai. VehCall's QR stickers let customers and traffic police reach our drivers instantly without sharing personal numbers. Managing verified contact points for the whole fleet is effortless now.",
    rating: 5,
    color: "#25D366",
    initials: "VS",
  },
];

export default function Testimonials() {
  const [index, setIndex] = useState(0);
  const [paused, setPaused] = useState(false);
  const count = TESTIMONIALS.length;

  useEffect(() => {
    if (paused) return;
    const id = setInterval(() => setIndex((i) => (i + 1) % count), 5500);
    return () => clearInterval(id);
  }, [paused, count]);

  const go = (dir: number) =>
    setIndex((i) => (i + dir + count) % count);

  return (
    <section id="testimonials" className="relative py-24 sm:py-28">
      <div className="pointer-events-none absolute left-1/2 top-1/2 h-72 w-[36rem] -translate-x-1/2 -translate-y-1/2 rounded-full bg-brand/10 blur-[140px]" />
      <div className="relative mx-auto max-w-4xl px-5 sm:px-8">
        <div className="flex flex-col items-center gap-4 text-center">
          <span className="eyebrow">What Users Say</span>
          <h2 className="text-3xl font-bold sm:text-4xl">
            Loved by{" "}
            <span className="text-gradient">drivers everywhere</span>
          </h2>
        </div>

        <div
          className="relative mt-12"
          onMouseEnter={() => setPaused(true)}
          onMouseLeave={() => setPaused(false)}
        >
          <div className="overflow-hidden">
            <div
              className="flex transition-transform duration-700 ease-out"
              style={{ transform: `translateX(-${index * 100}%)` }}
            >
              {TESTIMONIALS.map((t) => (
                <article
                  key={t.name}
                  className="w-full shrink-0 px-1"
                  aria-hidden={TESTIMONIALS[index].name !== t.name}
                >
                  <div className="glass mx-auto max-w-2xl rounded-3xl p-8 sm:p-10">
                    <div className="flex text-warning">
                      {Array.from({ length: t.rating }).map((_, i) => (
                        <StarIcon key={i} width={18} height={18} />
                      ))}
                    </div>
                    <p className="mt-5 text-lg leading-relaxed text-white/85 sm:text-xl">
                      &ldquo;{t.review}&rdquo;
                    </p>
                    <div className="mt-7 flex items-center gap-4">
                      <span
                        className="flex h-12 w-12 items-center justify-center rounded-full text-sm font-bold text-white"
                        style={{ background: t.color }}
                      >
                        {t.initials}
                      </span>
                      <div className="text-left">
                        <p className="font-semibold text-white">{t.name}</p>
                        <p className="text-sm text-white/55">{t.vehicle}</p>
                      </div>
                    </div>
                  </div>
                </article>
              ))}
            </div>
          </div>

          {/* Controls */}
          <div className="mt-8 flex items-center justify-center gap-4">
            <button
              type="button"
              aria-label="Previous testimonial"
              onClick={() => go(-1)}
              className="flex h-10 w-10 items-center justify-center rounded-full border border-white/12 text-white/70 transition-colors hover:border-brand/50 hover:text-white"
            >
              <ArrowRightIcon width={18} height={18} className="rotate-180" />
            </button>

            <div className="flex items-center gap-2">
              {TESTIMONIALS.map((t, i) => (
                <button
                  key={t.name}
                  type="button"
                  aria-label={`Go to testimonial ${i + 1}`}
                  onClick={() => setIndex(i)}
                  className={`h-2 rounded-full transition-all ${
                    i === index
                      ? "w-7 bg-brand"
                      : "w-2 bg-white/20 hover:bg-white/40"
                  }`}
                />
              ))}
            </div>

            <button
              type="button"
              aria-label="Next testimonial"
              onClick={() => go(1)}
              className="flex h-10 w-10 items-center justify-center rounded-full border border-white/12 text-white/70 transition-colors hover:border-brand/50 hover:text-white"
            >
              <ArrowRightIcon width={18} height={18} />
            </button>
          </div>
        </div>
      </div>
    </section>
  );
}
