import { fetchApi } from './client';
import type { LegalPage, LegalPagePublic, LegalPageListItem } from './types';

export const legalPagesApi = {
  // Public endpoints
  getAll: (locale?: string) =>
    fetchApi<{ pages: LegalPageListItem[] }>(`/api/legal-pages${locale ? `?locale=${locale}` : ''}`),

  getBySlug: (slug: string, locale?: string) =>
    fetchApi<{ page: LegalPagePublic }>(`/api/legal-pages/${slug}${locale ? `?locale=${locale}` : ''}`),

  // Admin endpoints
  getAdminList: () =>
    fetchApi<{ pages: LegalPage[] }>('/api/legal-pages/admin/list'),

  getAdminPage: (slug: string) =>
    fetchApi<{ page: LegalPage }>(`/api/legal-pages/admin/${slug}`),

  update: (slug: string, data: {
    contentMarkdown?: string;
    contentMarkdownEs?: string;
    title?: string;
    titleEs?: string;
  }) =>
    fetchApi<{ page: LegalPage; message: string }>(`/api/legal-pages/admin/${slug}`, {
      method: 'PUT',
      body: JSON.stringify(data),
    }),

  seed: () =>
    fetchApi<{ message: string; seeded: number; pages?: string[] }>('/api/legal-pages/admin/seed', {
      method: 'POST',
    }),
};
