'use client';

import { useState, useEffect } from 'react';
import Image from 'next/image';
import Link from 'next/link';
import type { FeaturesContent, FeaturedAgentItem } from '@/types/cms';
import { resolveImageUrl } from '@/services/cms.service';


function AgentCard({ agent, className = '' }: { agent: FeaturedAgentItem; className?: string }) {
  const [imgLoaded, setImgLoaded] = useState(false);
  const [imgSrc, setImgSrc] = useState(agent.imageUrl);

  useEffect(() => {
    // Only resolve if it's an S3 key (not a URL or local path)
    if (agent.imageUrl && !agent.imageUrl.startsWith('http') && !agent.imageUrl.startsWith('/')) {
      resolveImageUrl(agent.imageUrl).then((url) => {
        if (url) setImgSrc(url);
      });
    } else {
      setImgSrc(agent.imageUrl);
    }
  }, [agent.imageUrl]);

  const cardInner = (
    <div className={`bg-white border border-[#00293d]/10 rounded-[15px] w-[254px] overflow-hidden ${agent.id ? 'hover:shadow-xl transition-shadow cursor-pointer' : ''} ${className}`}>
      <div className="h-[161px] w-full overflow-hidden rounded-t-[15px] bg-gray-100 relative">
        {imgSrc && (
          <img
            ref={(el) => { if (el?.complete && el.naturalWidth > 0) setImgLoaded(true); }}
            src={imgSrc}
            alt={agent.name}
            className={`w-full h-full object-cover transition-opacity duration-300 ${imgLoaded ? 'opacity-100' : 'opacity-0'}`}
            onLoad={() => setImgLoaded(true)}
            onError={() => setImgLoaded(true)}
          />
        )}
        {!imgLoaded && <div className="absolute inset-0 shimmer-loading" />}
      </div>
      <div className="p-4">
        <h4 className="font-fractul font-bold text-[16px] leading-[19px] text-[#00293d]">{agent.name}</h4>
        <p className="font-fractul font-normal text-[14px] leading-[17px] text-[#00293d] mt-1">{agent.role}</p>
        <div className="flex items-center gap-4 mt-2">
          <div className="flex items-center gap-1">
            <Image src="/assets/icons/verified-icon.svg" alt="Verified" width={14} height={14} />
            <span className="font-serif font-medium text-[14px] leading-[19px] text-[#00293d]">Verified Agent</span>
          </div>
          {agent.rating && (
            <div className="flex items-center gap-1">
              <Image src="/assets/icons/star-filled-icon.svg" alt="Rating" width={14} height={14} />
              <span className="font-serif font-normal text-[14px] leading-[19px] text-[#00293d]">{agent.rating} Rating</span>
            </div>
          )}
        </div>
        {agent.location && (
          <div className="flex items-center gap-1 mt-2">
            <Image src="/assets/icons/location-filled-icon.svg" alt="Location" width={15} height={15} />
            <span className="font-serif font-medium text-[14px] leading-[19px] text-[#00293d]">{agent.location}</span>
          </div>
        )}
        {agent.experience && (
          <p className="mt-2 text-[14px] leading-[19px] text-[#00293d]">
            <span className="font-serif font-bold">Experience:</span>
            <span className="font-serif font-normal"> {agent.experience}</span>
          </p>
        )}
      </div>
    </div>
  );

  if (agent.id) {
    return <Link href={`/user/profile/${agent.id}`}>{cardInner}</Link>;
  }
  return cardInner;
}

export function FeaturesSection({ content }: { content?: FeaturesContent }) {
  if (!content) return null;
  const data = content;
  const agents = data.featuredAgents || [];
  const [activeCard, setActiveCard] = useState(0);

  return (
    <section className="py-16 md:py-20 bg-white">
      <div className="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
        <div className="flex flex-col lg:flex-row gap-12 lg:gap-8">
          {/* Left Side - Title & Features */}
          <div className="lg:w-1/2">
            <div className="mb-8">
              <h2 className="font-fractul font-bold text-[32px] md:text-[40px] leading-[1.2] text-[#00293d] mb-4">{data.title}</h2>
              <p className="font-fractul font-medium text-[18px] md:text-[20px] leading-[1.4] text-[#00293d]">{data.subtitle}</p>
            </div>
            <div className="grid grid-cols-1 sm:grid-cols-2 gap-4">
              {data.features.map((feature, index) => (
                <div key={index} className="border border-[#00293d]/10 rounded-[15px] p-5">
                  <div className="mb-3">
                    <Image src={feature.iconPath} alt={feature.title} width={35} height={35} />
                  </div>
                  <h3 className="font-fractul font-semibold text-[20px] leading-[24px] text-[#00293d] mb-2">{feature.title}</h3>
                  <p className="font-serif font-normal text-[14px] leading-[19px] text-[#00293d]">{feature.description}</p>
                </div>
              ))}
            </div>
          </div>

          {/* Right Side - Agent Cards (Desktop) */}
          {agents.length > 0 && (
            <div className="lg:w-1/2 relative min-h-[650px] hidden lg:block">
              {agents[0] && <AgentCard agent={agents[0]} className="absolute top-0 left-0 shadow-lg z-10" />}
              {agents[1] && <AgentCard agent={agents[1]} className="absolute top-[170px] right-0 shadow-lg z-20" />}
              {agents[2] && <AgentCard agent={agents[2]} className="absolute top-[320px] left-[60px] shadow-lg z-30" />}
            </div>
          )}

          {/* Mobile Agent Cards - Horizontal swipe carousel */}
          {agents.length > 0 && (
            <div className="lg:hidden">
              <div
                className="flex gap-4 overflow-x-auto snap-x snap-mandatory scroll-smooth pb-2 -mx-4 px-4"
                onScroll={(e) => {
                  const el = e.currentTarget;
                  const cardWidth = el.children[0]?.clientWidth || 254;
                  const gap = 16;
                  const idx = Math.round(el.scrollLeft / (cardWidth + gap));
                  if (idx !== activeCard) setActiveCard(idx);
                }}
              >
                {agents.map((agent, index) => (
                  <div key={index} className="snap-center flex-shrink-0">
                    <AgentCard agent={agent} className="shadow-lg" />
                  </div>
                ))}
              </div>
              {/* Dot indicators (visual only — swipe to change) */}
              <div className="flex justify-center gap-2 mt-4">
                {agents.map((_, index) => (
                  <span
                    key={index}
                    className={`rounded-full transition-all ${
                      index === activeCard
                        ? 'w-[10px] h-[10px] bg-[#00293d]'
                        : 'w-[8px] h-[8px] bg-[#00293d]/30'
                    }`}
                  />
                ))}
              </div>
            </div>
          )}
        </div>
      </div>
    </section>
  );
}
