'use client'; import { useState, useEffect } from 'react'; import Image from 'next/image'; import type { FeaturesContent, FeaturedAgentItem } from '@/types/cms'; import { resolveImageUrl } from '@/services/cms.service'; import { agentsService } from '@/services/agents.service'; import { uploadService } from '@/services/upload.service'; const defaultContent: FeaturesContent = { title: 'Find Trusted Real Estate Professionals On Demand', subtitle: 'Quickly connect with the right agents exactly when you need them.', features: [ { iconPath: '/assets/icons/hourglass-icon.svg', title: 'Hire Quickly', description: 'Connect with experienced local real estate agents who match your needs and preferences. Our simple process helps you get started quickly and move forward without delays or unnecessary complexity.', }, { iconPath: '/assets/icons/verified-badge-icon.svg', title: 'Verified Agents', description: 'All agents are carefully identity-verified and background-checked to ensure trust and safety. You can confidently work with professionals who meet quality and reliability standards.', }, { iconPath: '/assets/icons/star-orange-icon.svg', title: 'Top Rated', description: 'Work with highly rated agents known for strong expertise and positive client feedback. Their consistent performance helps you make informed and confident property decisions.', }, { iconPath: '/assets/icons/trusted-people-icon.svg', title: 'Trusted by Thousands', description: 'Thousands of buyers, sellers, and renters trust our platform to find reliable agents. Our professionals help people navigate property journeys with confidence and ease.', }, ], featuredAgents: [ { name: 'Anderson', role: 'Rental & Investment Consultant', rating: '4.9', location: 'New York', experience: '7+ years in the real estate industry.', imageUrl: '/assets/images/agent-anderson.jpg' }, { name: 'Deepak', role: 'Rental & Investment Consultant', rating: '4.9', location: 'New York', experience: '7+ years in the real estate industry.', imageUrl: '/assets/images/agent-deepak.jpg' }, { name: 'Daniel', role: 'Rental & Investment Consultant', rating: '4.9', location: 'New York', experience: '7+ years in the real estate industry.', imageUrl: '/assets/images/agent-daniel.jpg' }, ], }; 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]); return (
{imgSrc && ( { 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 &&
}

{agent.name}

{agent.role}

Verified Verified Agent
{agent.rating && (
Rating {agent.rating} Rating
)}
{agent.location && (
Location {agent.location}
)} {agent.experience && (

Experience: {agent.experience}

)}
); } export function FeaturesSection({ content }: { content?: FeaturesContent }) { const data = content ?? defaultContent; const [agents, setAgents] = useState([]); const [activeCard, setActiveCard] = useState(0); useEffect(() => { const fetchFeaturedAgents = async () => { try { const featured = await agentsService.getFeaturedAgents(3); if (featured.length > 0) { // Map API response to FeaturedAgentItem format const mapped = await Promise.all( featured.map(async (agent) => { let imageUrl = '/assets/icons/user-placeholder-icon.svg'; if (agent.avatar) { if (agent.avatar.startsWith('http') || agent.avatar.startsWith('/')) { imageUrl = agent.avatar; } else { try { imageUrl = await uploadService.getPresignedDownloadUrl(agent.avatar); } catch (_e) { // use placeholder } } } return { name: `${agent.firstName || ''} ${agent.lastName || ''}`.trim(), role: agent.agentType?.name || '', rating: agent.averageRating ? String(agent.averageRating) : '', location: agent.city || agent.state || '', experience: agent.experience ? `${agent.experience}+ years in the real estate industry.` : '', imageUrl, }; }), ); setAgents(mapped); } } catch (err) { console.error('Failed to fetch featured agents:', err); } }; fetchFeaturedAgents(); }, []); return (
{/* Left Side - Title & Features */}

{data.title}

{data.subtitle}

{data.features.map((feature, index) => (
{feature.title}

{feature.title}

{feature.description}

))}
{/* Right Side - Agent Cards (Desktop) */} {agents.length > 0 && (
{agents[0] && } {agents[1] && } {agents[2] && }
)} {/* Mobile Agent Cards - Tab style with dots */} {agents.length > 0 && (
{/* Dot indicators */}
{agents.map((_, index) => (
)}
); }