'use client'; import { useState, useEffect } from 'react'; import Image from 'next/image'; import { useParams } from 'next/navigation'; // Import shared components import { InfoCard, ExperienceSection, ProfileCard, SpecializationSection, TestimonialsSection, StatusButtons, ContactInfo, } from '@/components/profile'; import { agentsService, AgentProfile, FieldValueResponse } from '@/services/agents.service'; import { getProxyImageUrl } from '@/lib/imageProxy'; import { mapFieldValuesToExperience, mapFieldValuesToSpecializationFields, ExperienceData, SpecializationFieldsData } from '@/utils/profileDataMapper'; // Mock data for sections not yet available from API const mockData = { availability: { type: 'Full-time', days: ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'], }, preferredWorkEnvironment: 'Preferred work environment includes on-site property visits, in-depth market research, client consultations, property tours, and active involvement in negotiations to ensure the best outcomes', testimonialHighlight: "The most amazing experience I've had as a real estate professional is helping a family secure their dream home and seeing their happiness when they received the keys.", testimonials: [ { id: 1, text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo ligula eget dolor. Sed dignissim, nisl eget tincidunt vulputate, lacus justo bibendum ipsum, vitae tempus risus lorem at nunc. Integer sed arcu vitae risus feugiat vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.', author: 'Kenedy Kenney', role: 'Chief Operations Officer', rating: 5, }, { id: 2, text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo ligula eget dolor. Sed dignissim, nisl eget tincidunt vulputate, lacus justo bibendum ipsum, vitae tempus risus lorem at nunc. Integer sed arcu vitae risus feugiat vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.', author: 'Kenedy Kenney', role: 'Chief Operations Officer', rating: 5, }, { id: 3, text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Aenean commodo ligula eget dolor. Sed dignissim, nisl eget tincidunt vulputate, lacus justo bibendum ipsum, vitae tempus risus lorem at nunc. Integer sed arcu vitae risus feugiat vehicula. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas.', author: 'Kenedy Kenney', role: 'Chief Operations Officer', rating: 5, }, ], }; // Default experience data when no field values are available const defaultExperience: ExperienceData = { years: '-', contracts: '-', licensingAreas: [], expertiseYears: [], certifications: [], }; // Default specialization fields data when no field values are available const defaultSpecializationFieldsData: SpecializationFieldsData = { fields: [], }; export default function AgentProfileView() { const params = useParams(); const id = params.id as string; const [agentProfile, setAgentProfile] = useState(null); const [fieldValues, setFieldValues] = useState([]); const [experienceData, setExperienceData] = useState(defaultExperience); const [specializationFieldsData, setSpecializationFieldsData] = useState(defaultSpecializationFieldsData); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [imageError, setImageError] = useState(false); const defaultImage = '/assets/demo_images/8f017153a7b4a239a4f0691234ef97dd55092282.jpg'; // Fetch agent profile and field values on mount useEffect(() => { const fetchData = async () => { if (!id) return; try { setLoading(true); setError(null); // Fetch profile and field values in parallel const [profile, fieldValuesResponse] = await Promise.all([ agentsService.getAgentById(id), agentsService.getFieldValuesByAgentId(id), ]); setAgentProfile(profile); setFieldValues(fieldValuesResponse.fieldValues); // Map field values to experience data structure const mappedExperience = mapFieldValuesToExperience(fieldValuesResponse.fieldValues); setExperienceData(mappedExperience); // Map field values to specialization fields data (only fields from "Specialization" section) const mappedSpecializationFields = mapFieldValuesToSpecializationFields(fieldValuesResponse.fieldValues); setSpecializationFieldsData(mappedSpecializationFields); } catch (err) { console.error('Failed to fetch profile:', err); setError('Failed to load profile data'); } finally { setLoading(false); } }; fetchData(); }, [id]); const getProfileImageUrl = () => { // If image failed to load, return default if (imageError) { return defaultImage; } // Check for null, undefined, or empty string if (!agentProfile?.avatar || agentProfile.avatar.trim() === '') { return defaultImage; } // S3 URLs need to go through the backend proxy if (agentProfile.avatar.startsWith('http')) { return getProxyImageUrl(agentProfile.avatar); } // For relative paths (local assets), return as-is return agentProfile.avatar; }; // Format member since date const formatMemberSince = (dateString?: string) => { if (!dateString) return 'Member'; try { const date = new Date(dateString); return date.toLocaleDateString('en-US', { month: 'long', year: 'numeric' }); } catch { return 'Member'; } }; if (loading) { return (

Loading profile...

); } if (error || !agentProfile) { return (

Unable to Load Profile

{error || 'Profile not found'}

); } return (
{/* Main Layout - Responsive: Column on mobile, Row on desktop */}
{/* Left Sidebar - Status & Contact */}
{/* Profile Image */}
{/* eslint-disable-next-line @next/next/no-img-element */} Profile { const target = e.target as HTMLImageElement; target.src = defaultImage; setImageError(true); }} /> {/* Gradient Overlay */}
{/* Status Buttons */} {/* Contact Info */}
{/* Right Content - Profile Info + Experience + All Sections */}
{/* Profile Card - No edit button for user view */} {/* Experience Section - Dynamic data from profile fields */} {/* Info Cards Section */}
} content={

{mockData.availability.type}

{mockData.availability.days.map((day) => (

{day}

))}
} /> } content={

{mockData.preferredWorkEnvironment}

} /> } content={

“{mockData.testimonialHighlight}”

} />
{/* Specialization Section */} {/* Testimonials Section */}
); }