/**
 * WAITLIST (RennmausPro nachfrage_tb): API client for the prospective-buyer waiting list.
 * Full CRUD against /waiting-list (GET list, POST, PUT /{id}, DELETE /{id}).
 *
 * Entries are decoupled from contacts on the backend (loose nullable contactId, no FK),
 * so they survive the import re-ingest wipe — same pattern as Feedback.
 */
import { api } from './client'

const RESOURCE = '/waiting-list'

/** Workflow status (matches the backend contract). */
export type WaitingListStatus = 'offen' | 'erfuellt' | 'storniert'
export const WAITING_LIST_STATUSES: WaitingListStatus[] = ['offen', 'erfuellt', 'storniert']

/** Wished-for gender; null = no preference. */
export type WishGender = 'male' | 'female' | null

export interface WaitingListEntry {
  id: string
  contactId: string | null
  contactName: string | null
  wishColor: string | null
  wishGender: string | null
  requestedAt: string | null
  status: string
  note: string | null
  createdAt: string
}

/** Payload for POST/PUT /waiting-list. */
export interface WaitingListInput {
  contactId?: string | null
  contactName?: string | null
  wishColor?: string | null
  wishGender?: string | null
  requestedAt?: string | null
  status: WaitingListStatus
  note?: string | null
}

export function listWaitingList(): Promise<WaitingListEntry[]> {
  return api.get<WaitingListEntry[]>(RESOURCE)
}

export function createWaitingListEntry(body: WaitingListInput): Promise<WaitingListEntry> {
  return api.post<WaitingListEntry>(RESOURCE, body)
}

export function updateWaitingListEntry(id: string, body: WaitingListInput): Promise<WaitingListEntry> {
  return api.put<WaitingListEntry>(`${RESOURCE}/${id}`, body)
}

export function deleteWaitingListEntry(id: string): Promise<void> {
  return api.delete(`${RESOURCE}/${id}`)
}
