"use client"; import { useState, useEffect } from "react"; import { motion } from "framer-motion"; import { useRouter } from "next/navigation"; import Link from "next/link"; import { useTranslation } from "react-i18next"; import { User, Mail, Phone, MessageCircle, Check, X, ArrowLeft, Building, Home, Calendar, MapPin, Loader2, Pencil, } from "lucide-react"; import toast, { Toaster } from "react-hot-toast"; import AuthService from "../services/AuthService"; import { getCustomerByUserId, getOwnerByUserId } from "../utils/api"; export default function ProfilePage() { const { t, i18n } = useTranslation(); const router = useRouter(); const [user, setUser] = useState(null); const [isLoading, setIsLoading] = useState(true); const [editingBio, setEditingBio] = useState(false); const [bioDraft, setBioDraft] = useState(""); const [formData, setFormData] = useState({ name: "", email: "", phone: "", whatsapp: "", bio: "", location: "", joinedDate: "", }); const [avatarPreview, setAvatarPreview] = useState(""); useEffect(() => { const authUser = AuthService.getUser(); if (authUser) { const userData = { id: authUser.id, name: authUser.name || "", email: authUser.email || "", phone: authUser.phone || "", role: AuthService.isOwner() ? "owner" : "customer", }; setUser(userData); async function fetchProfile() { try { const fetchFn = userData.role === "owner" ? getOwnerByUserId : getCustomerByUserId; const profile = await fetchFn(userData.id); if (profile) { const profileData = { name: profile.fullName || profile.name || `${profile.firstName || ""} ${profile.lastName || ""}`.trim() || userData.name || "", email: profile.email || userData.email || "", phone: profile.phone || profile.phoneNumber || userData.phone || "", whatsapp: profile.whatsAppNumber || profile.whatsapp || "", bio: profile.bio || "", location: profile.address || profile.location || "", joinedDate: profile.createdAt ? new Date(profile.createdAt).toLocaleDateString(i18n.language === 'ar' ? 'ar-SA' : 'en-US', { month: "long", year: "numeric", }) : new Date().toLocaleDateString(i18n.language === 'ar' ? 'ar-SA' : 'en-US', { month: "long", year: "numeric", }), }; setFormData(profileData); setBioDraft(profileData.bio); localStorage.setItem("userProfile", JSON.stringify(profileData)); setIsLoading(false); return; } } catch (err) { // Ignore API errors and fall back to local data. } const savedProfile = localStorage.getItem("userProfile"); let profileData; if (savedProfile) { profileData = JSON.parse(savedProfile); } else { profileData = { name: userData.name || "", email: userData.email || "", phone: "", whatsapp: "", bio: "", location: "", joinedDate: new Date().toLocaleDateString(i18n.language === 'ar' ? 'ar-SA' : 'en-US', { month: "long", year: "numeric", }), }; } setFormData(profileData); setBioDraft(profileData.bio || ""); setIsLoading(false); } const savedAvatar = localStorage.getItem("userAvatar"); if (savedAvatar) { setAvatarPreview(savedAvatar); } fetchProfile(); } else { router.push("/login"); } }, [router]); const startBioEditing = () => { setBioDraft(formData.bio || ""); setEditingBio(true); }; const cancelBioEditing = () => { setBioDraft(formData.bio || ""); setEditingBio(false); }; const saveBio = () => { const updatedData = { ...formData, bio: bioDraft }; setFormData(updatedData); localStorage.setItem("userProfile", JSON.stringify(updatedData)); setEditingBio(false); toast.success(t('bio-updated-success')); }; const fadeInUp = { initial: { opacity: 0, y: 20 }, animate: { opacity: 1, y: 0 }, transition: { duration: 0.5 }, }; if (isLoading) { return (

{t('loading-profile')}

); } return (
{t('backToHome')}
{avatarPreview ? ( {formData.name} ) : ( formData.name?.charAt(0).toUpperCase() || "U" )}

{formData.name}

{formData.location || t('addressNotSpecified')}
{t('member-since')} {formData.joinedDate}
{formData.email}
{formData.phone || t('not-specified')}
{formData.whatsapp || t('not-specified')}
{user?.role === "owner" ? ( <> {t('property-owner')} ) : ( <> {t('customer')} )}
{/* noo need for the descption of me on the profile*/} {/*
{!editingBio && ( )}
{editingBio ? (