import apiClient from './client'

export interface TemplateResponse {
  id: string
  tenant_id: string
  name: string
  description: string | null
  subject: string
  body_text: string | null
  body_html: string | null
  created_by: string | null
  created_at: string
  updated_at: string
}

export interface TemplateCreate {
  name: string
  description?: string | null
  subject?: string
  body_text?: string | null
  body_html?: string | null
}

export interface TemplateUpdate {
  name?: string
  description?: string | null
  subject?: string
  body_text?: string | null
  body_html?: string | null
}

export const templatesApi = {
  list: (q?: string) =>
    apiClient.get<{ items: TemplateResponse[]; total: number }>('/templates', {
      params: { q },
    }).then((r) => r.data),

  get: (id: string) =>
    apiClient.get<TemplateResponse>(`/templates/${id}`).then((r) => r.data),

  create: (data: TemplateCreate) =>
    apiClient.post<TemplateResponse>('/templates', data).then((r) => r.data),

  update: (id: string, data: TemplateUpdate) =>
    apiClient.put<TemplateResponse>(`/templates/${id}`, data).then((r) => r.data),

  delete: (id: string) =>
    apiClient.delete(`/templates/${id}`).then((r) => r.data),
}
