import { fetchApi } from './client';
import type { FaqItem, FaqItemAdmin } from './types';

export const faqApi = {
  // Public
  getList: (homepageOnly?: boolean) =>
    fetchApi<{ faqs: FaqItem[] }>(`/api/faq${homepageOnly ? '?homepage=true' : ''}`),

  // Admin
  getAdminList: () =>
    fetchApi<{ faqs: FaqItemAdmin[] }>('/api/faq/admin/list'),

  getById: (id: string) =>
    fetchApi<{ faq: FaqItemAdmin }>(`/api/faq/admin/${id}`),

  create: (data: {
    question: string;
    questionEs?: string;
    answer: string;
    answerEs?: string;
    enabled?: boolean;
    showOnHomepage?: boolean;
  }) =>
    fetchApi<{ faq: FaqItemAdmin }>('/api/faq/admin', {
      method: 'POST',
      body: JSON.stringify(data),
    }),

  update: (id: string, data: {
    question?: string;
    questionEs?: string | null;
    answer?: string;
    answerEs?: string | null;
    enabled?: boolean;
    showOnHomepage?: boolean;
  }) =>
    fetchApi<{ faq: FaqItemAdmin }>(`/api/faq/admin/${id}`, {
      method: 'PUT',
      body: JSON.stringify(data),
    }),

  delete: (id: string) =>
    fetchApi<{ message: string }>(`/api/faq/admin/${id}`, { method: 'DELETE' }),

  reorder: (ids: string[]) =>
    fetchApi<{ faqs: FaqItemAdmin[] }>('/api/faq/admin/reorder', {
      method: 'POST',
      body: JSON.stringify({ ids }),
    }),
};
