/**
 * Services: each one is a card on the Services page (under one of the four divisions)
 * plus its own page at /services/<slug> (API reference: 'Services ("Read More" pages)').
 *
 * The card and the page sections are edited like CMS pages (serviceEditorApi in ./cms):
 * draft → review → published per language. A service is on the website once its card
 * is published; hiding the card section (layout) takes the whole service off.
 * Services are the same in every region.
 */
import { api } from "./api";
import type { Img, SectionType } from "./cms";

export type DivisionKey = "science-nutrition" | "trade-commerce" | "sustainability-strategy" | "innovation-knowledge";

export type ServiceCardContent = { image: Img; title: string; description?: string; ctaLabel: string; href?: string };

/** GET /admin/services item. */
export type Service = {
  id: string;
  slug: string;
  division: DivisionKey;
  /** Position within the division (lowest first). */
  order: number;
  /** The website address: /services/<slug>. */
  path: string;
  /** The English card (draft if there is one, else published); null when none. */
  card: ServiceCardContent | null;
  /** The card's state in each language that has one. */
  cardStatus: Record<string, "draft" | "in_review" | "published">;
  createdAt: string;
  updatedAt: string;
};

/** GET /admin/services/template: the divisions and the sections every service page has. */
export type ServiceTemplate = {
  divisions: { key: DivisionKey; name: string }[];
  sections: { key: string; type: SectionType; name: string }[];
};

export const servicesApi = {
  template: () => api<ServiceTemplate>("/admin/services/template"),
  list: (division?: string) => api<Service[]>("/admin/services", { query: { division } }),
  get: (id: string) => api<Service>(`/admin/services/${id}`),
  create: (body: { slug: string; division: DivisionKey; order?: number }) => api<Service>("/admin/services", { method: "POST", body }),
  update: (id: string, body: { slug?: string; division?: DivisionKey; order?: number }) => api<Service>(`/admin/services/${id}`, { method: "PATCH", body }),
  /** Permanent: the card and page content in every language go too. */
  remove: (id: string) => api<void>(`/admin/services/${id}`, { method: "DELETE" }),
};

/** Fallback division names, used until the template has loaded. */
export const DIVISION_NAMES: Record<DivisionKey, string> = {
  "science-nutrition": "Science & Nutrition Division",
  "trade-commerce": "Trade & Commerce",
  "sustainability-strategy": "Sustainability & Strategy Division",
  "innovation-knowledge": "Innovation & Knowledge Division",
};
