'use client' import { useState, useEffect } from 'react' import { useRouter } from 'next/navigation' import { Button } from '@/components/ui/button' import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card' import { Input } from '@/components/ui/input' import { Label } from '@/components/ui/label' import { Calendar, Clock, MapPin, User, Mail, Phone } from 'lucide-react' import { format } from 'date-fns' import { es } from 'date-fns/locale' import { useAuth } from '@/lib/auth/context' /** @description Customer profile management page component for viewing and editing personal information and booking history. */ export default function PerfilPage() { const { user, loading: authLoading } = useAuth() const router = useRouter() const [customer, setCustomer] = useState(null) const [bookings, setBookings] = useState([]) const [isEditing, setIsEditing] = useState(false) const [pageLoading, setPageLoading] = useState(false) const [formData, setFormData] = useState({ first_name: '', last_name: '', email: '', phone: '' }) useEffect(() => { if (!authLoading && !user) { router.push('/booking/login') } }, [user, authLoading, router]) useEffect(() => { if (!authLoading && user) { loadCustomerProfile() loadCustomerBookings() } }, [user, authLoading]) if (authLoading) { return (

Cargando...

) } if (!user) { return null } const loadCustomerProfile = async () => { try { // En una implementación real, esto vendría de autenticación // Por ahora, simulamos datos del cliente const mockCustomer = { id: 'customer-123', first_name: 'María', last_name: 'García', email: 'maria.garcia@email.com', phone: '+52 844 123 4567', tier: 'gold', created_at: '2024-01-15' } setCustomer(mockCustomer) setFormData({ first_name: mockCustomer.first_name, last_name: mockCustomer.last_name, email: mockCustomer.email, phone: mockCustomer.phone || '' }) } catch (error) { console.error('Error loading customer profile:', error) } } const loadCustomerBookings = async () => { try { // En una implementación real, esto vendría de la API // Por ahora, simulamos algunas citas const mockBookings = [ { id: 'booking-1', short_id: 'ABC123', status: 'confirmed', start_time_utc: '2024-01-20T10:00:00Z', service: { name: 'Corte y Estilismo', duration_minutes: 60, base_price: 2500 }, staff: { display_name: 'Ana López' }, location: { name: 'Anchor:23 Saltillo' } }, { id: 'booking-2', short_id: 'DEF456', status: 'pending', start_time_utc: '2024-01-25T14:30:00Z', service: { name: 'Manicure de Precisión', duration_minutes: 45, base_price: 1200 }, staff: { display_name: 'Carlos Martínez' }, location: { name: 'Anchor:23 Saltillo' } } ] setBookings(mockBookings) } catch (error) { console.error('Error loading customer bookings:', error) } } const handleSaveProfile = async () => { setPageLoading(true) try { // En una implementación real, esto actualizaría el perfil del cliente setCustomer({ ...customer, ...formData }) setIsEditing(false) alert('Perfil actualizado exitosamente') } catch (error) { console.error('Error updating profile:', error) alert('Error al actualizar el perfil') } finally { setPageLoading(false) } } const handleChange = (e: React.ChangeEvent) => { setFormData({ ...formData, [e.target.name]: e.target.value }) } const getTierBadge = (tier: string) => { const tiers = { free: { label: 'Free', color: 'bg-gray-100 text-gray-800' }, gold: { label: 'Gold', color: 'bg-yellow-100 text-yellow-800' }, black: { label: 'Black', color: 'bg-gray-900 text-white' }, vip: { label: 'VIP', color: 'bg-purple-100 text-purple-800' } } return tiers[tier as keyof typeof tiers] || tiers.free } const getStatusBadge = (status: string) => { const statuses = { pending: { label: 'Pendiente', color: 'bg-yellow-100 text-yellow-800' }, confirmed: { label: 'Confirmada', color: 'bg-green-100 text-green-800' }, completed: { label: 'Completada', color: 'bg-blue-100 text-blue-800' }, cancelled: { label: 'Cancelada', color: 'bg-red-100 text-red-800' }, no_show: { label: 'No Show', color: 'bg-gray-100 text-gray-800' } } return statuses[status as keyof typeof statuses] || statuses.pending } if (!customer) { return (

Cargando perfil...

) } const tierInfo = getTierBadge(customer.tier) return (

Mi Perfil

Gestiona tu información y citas

Información Personal {!isEditing && ( )} {isEditing ? (
) : (
{customer.first_name} {customer.last_name}
{customer.email}
{customer.phone && (
{customer.phone}
)}
{tierInfo.label} Tier
)}
Resumen de Actividad
{bookings.length}
Citas totales
{bookings.filter(b => b.status === 'completed').length}
Completadas
Miembro desde {format(new Date(customer.created_at), 'MMM yyyy', { locale: es })}
Mis Últimas Citas Historial de tus reservas {bookings.length === 0 ? (

No tienes citas programadas

) : (
{bookings.map((booking) => (

{booking.service?.name}

{booking.staff?.display_name} • {booking.location?.name}

{format(new Date(booking.start_time_utc), 'PPP HH:mm', { locale: es })}

{getStatusBadge(booking.status).label}

${booking.service?.base_price?.toLocaleString()}

Código: {booking.short_id}

))}
)}
) }