'use client';

import { LoadState } from '@/components/content/feedback';
import { useAdminQuery } from '@/lib/api';
import { Card, Stack, Text, Title } from '@mantine/core';

type Lead = {
  id: string;
  name: string;
  email: string;
  phone: string | null;
  message: string;
  createdAt: string;
};

const dateFormat = new Intl.DateTimeFormat('pt-BR', { dateStyle: 'short', timeStyle: 'short' });

export function LeadsList() {
  const query = useAdminQuery<{ leads: Lead[] }>('/admin/catalog');
  const leads = query.data?.leads ?? [];

  return (
    <Stack gap="md">
      <LoadState error={query.error} loading={query.loading} />
      {query.data && leads.length === 0 && (
        <Text c="var(--mantine-color-muted)" size="sm">
          Nenhuma mensagem recebida até agora.
        </Text>
      )}
      {leads.map((lead) => (
        <Card className="border border-line bg-elevated" key={lead.id} padding="lg" radius="md" withBorder>
          <Stack gap="xs">
            <Title order={2} size="h5">
              {lead.name}
            </Title>
            <Text c="var(--mantine-color-muted)" size="sm">
              {lead.email}
              {lead.phone ? ` · ${lead.phone}` : ''} · {dateFormat.format(new Date(lead.createdAt))}
            </Text>
            <Text size="sm">{lead.message}</Text>
          </Stack>
        </Card>
      ))}
    </Stack>
  );
}
