"use client";

import { useEffect, useState } from "react";
import { Quote } from "lucide-react";
import { getOrganizationRatings } from "@/api/ratingApi";

interface Review {
  rating: number;
  review_text: string;
  created_at: string;
  user_id: string;
  user_role: string;
  user_name: string;
}

const ReviewsSection = () => {
  const [reviews, setReviews] = useState<Review[]>([]);
  const [loading, setLoading] = useState(true);

  useEffect(() => {
    fetchReviews();
  }, []);

  const fetchReviews = async () => {
    try {
      const data = await getOrganizationRatings();

      setReviews(data?.reviews || []);
    } catch (error) {
      console.error("Failed to fetch reviews", error);
    } finally {
      setLoading(false);
    }
  };

  return (
    <section className="w-full bg-[#f5f5f5] py-12 overflow-hidden mb-20">
      <div className="max-w-6xl mx-auto px-4">
        {/* Heading */}
        <div className="text-center mb-10">
          <h2 className="text-3xl md:text-5xl font-extrabold leading-tight">
            What our users have to{" "}
            <span className="text-[#f4c430]">say</span>
          </h2>
        </div>

        {/* Loading */}
        {loading ? (
          <div className="text-center text-base font-medium">
            Loading reviews...
          </div>
        ) : reviews.length === 0 ? (
          <div className="text-center text-base font-medium">
            No reviews available
          </div>
        ) : (
          <div className="grid grid-cols-1 md:grid-cols-2 xl:grid-cols-3 gap-8">
            {reviews.map((item, index) => (
              <div
                key={item.user_id + index}
                className={`relative ${
                  index === 1 ? "xl:-mt-10" : ""
                }`}
              >
                {/* Yellow Border */}
                <div className="absolute top-4 left-4 w-full h-full border-2 border-[#f4c430] rounded-2xl"></div>

                {/* Card */}
                <div className="relative bg-white rounded-2xl shadow-lg p-6 min-h-[280px] flex flex-col justify-between z-10">
                  {/* Quote Icon */}
                  <div className="absolute -top-5 left-0">
                    <Quote
                      size={42}
                      strokeWidth={1.5}
                      className="text-[#f4c430] fill-[#f4c430]"
                    />
                  </div>

                  {/* Review Text */}
                  <p className="text-[15px] leading-7 text-[#222] mt-5 line-clamp-6">
                    {item.review_text}
                  </p>

                  {/* User Info */}
                  <div className="mt-6">
                    <h4 className="font-bold text-lg text-black">
                      {item.user_name}
                    </h4>

                    <p className="text-gray-500 text-sm mt-1">
                      Google play store
                    </p>

                    {/* Rating */}
                    <div className="flex items-center gap-1 mt-2">
                      {Array.from({ length: item.rating }).map(
                        (_, i) => (
                          <span
                            key={i}
                            className="text-[#f4c430] text-sm"
                          >
                            ★
                          </span>
                        )
                      )}
                    </div>
                  </div>
                </div>
              </div>
            ))}
          </div>
        )}
      </div>
    </section>
  );
};

export default ReviewsSection;