import { apiFetch } from './client';

export type AisleOption = { value: string; label: string; color: string; icon: string };

export type ShoppingItem = {
  id: number;
  name: string;
  quantity: string;
  adjustment: { label: string; positive: boolean } | null;
  trackQuantity: boolean;
  usages: { recipeName: string; categoryLabel: string; plannedDate: string | null; quantity: string }[];
  checked: boolean;
  alreadyOwned: boolean;
  custom: boolean;
  aisle: AisleOption;
};

export type ShoppingListPayload = {
  weekStart: string;
  stats: { total: number; owned: number; toBuy: number; checked: number; pct: number };
  items: ShoppingItem[];
  meta: { aisles: AisleOption[] };
};

// `to` : borne de fin personnalisée (encadrement de dates custom, voir CustomRangeModal.tsx)
// — absente/`null` = semaine de 7 jours à partir de `week` (navigation normale).
function rangeQuery(week: string, to?: string | null): string {
  return `week=${week}${to ? `&to=${to}` : ''}`;
}

export function fetchShoppingList(week: string, to?: string | null) {
  return apiFetch<ShoppingListPayload>(`/shopping-list?${rangeQuery(week, to)}`);
}

export function addShoppingItem(week: string, name: string, aisle?: string, to?: string | null) {
  return apiFetch<ShoppingListPayload>('/shopping-list/items', { method: 'POST', body: { week, to, name, aisle } });
}

// En POST plutôt que DELETE — l'hébergement de prod bloque ce verbe HTTP (403 avant
// même d'atteindre PHP), voir Api\V1\TrackingController::removeEntry().
export function removeShoppingItem(id: number, week: string, to?: string | null) {
  return apiFetch<ShoppingListPayload>(`/shopping-list/items/${id}/remove?${rangeQuery(week, to)}`, { method: 'POST' });
}

export function toggleOwned(id: number, week: string, to?: string | null) {
  return apiFetch<ShoppingListPayload>(`/shopping-list/items/${id}/toggle-owned?${rangeQuery(week, to)}`, { method: 'POST' });
}

export function toggleChecked(id: number, week: string, to?: string | null) {
  return apiFetch<ShoppingListPayload>(`/shopping-list/items/${id}/toggle-checked?${rangeQuery(week, to)}`, { method: 'POST' });
}

export function increaseQuantity(id: number, week: string, to?: string | null) {
  return apiFetch<ShoppingListPayload>(`/shopping-list/items/${id}/quantity/increase?${rangeQuery(week, to)}`, { method: 'POST' });
}

export function decreaseQuantity(id: number, week: string, to?: string | null) {
  return apiFetch<ShoppingListPayload>(`/shopping-list/items/${id}/quantity/decrease?${rangeQuery(week, to)}`, { method: 'POST' });
}

export function setQuantity(id: number, week: string, value: number, to?: string | null) {
  return apiFetch<ShoppingListPayload>(`/shopping-list/items/${id}/quantity`, { method: 'POST', body: { week, to, value } });
}

export type CustomRange = { id: number; start: string; end: string };

export function fetchCustomRanges() {
  return apiFetch<{ ranges: CustomRange[] }>('/shopping-list/custom-ranges');
}

// Enregistre (ou réutilise, voir ShoppingListService::saveCustomRange()) un créneau dès
// qu'il est appliqué depuis CustomRangeModal.tsx — pas de geste "sauvegarder" séparé.
export function saveCustomRange(start: string, end: string) {
  return apiFetch<{ ranges: CustomRange[] }>('/shopping-list/custom-ranges', { method: 'POST', body: { start, end } });
}

// En POST plutôt que DELETE — même contrainte hébergement que les autres routes ci-dessus.
export function removeCustomRange(id: number) {
  return apiFetch<{ ranges: CustomRange[] }>(`/shopping-list/custom-ranges/${id}/remove`, { method: 'POST' });
}
