import { fetchApi } from './client';
import type { Contact } from './types';

export const contactsApi = {
  submit: (data: { name: string; email: string; message: string }) =>
    fetchApi<{ message: string }>('/api/contacts', {
      method: 'POST',
      body: JSON.stringify(data),
    }),

  subscribe: (email: string, name?: string) =>
    fetchApi<{ message: string }>('/api/contacts/subscribe', {
      method: 'POST',
      body: JSON.stringify({ email, name }),
    }),

  getAll: (status?: string) => {
    const query = status ? `?status=${status}` : '';
    return fetchApi<{ contacts: Contact[] }>(`/api/contacts${query}`);
  },

  updateStatus: (id: string, status: string) =>
    fetchApi<{ contact: Contact }>(`/api/contacts/${id}`, {
      method: 'PUT',
      body: JSON.stringify({ status }),
    }),
};
