import apiClient from "@/lib/api/client";
import type { ApiResponse } from "@/lib/api/types";
import type {
  CreatedSponsorRecord,
  SponsorRecord,
} from "@/features/partners/sponsorRecord";

/* `index()` returns the whole approved set in one response — no `meta`. */
export const getSponsors = (): Promise<ApiResponse<SponsorRecord[]>> =>
  apiClient.get("/sponsors/").then((r) => r.data);

/**
 * `SponsorController::store()` — the "become a partner" form. Multipart: a
 * `sponsor_logo` and any number of `new_sponsor_documents[]` ride along.
 *
 * Two things worth knowing, both from the controller rather than the field
 * names:
 *
 *  - the row is created with `approval_status: PENDING`, so a success here is
 *    an application and not a listing — `GET /sponsors/` filters to approved;
 *  - `mergeUnderscorePrefixedChoiceFields()` runs first, so the
 *    underscore-prefixed `_sponsor_type` / `_type_of_support` shapes the read
 *    side uses are accepted here too and folded onto the `*_id` keys.
 *
 * Required: `org_name`, `person_name`, `email`. 201 with the created sponsor
 * through `SponsorResource` — the **admin-shaped** one, not the
 * `WebsiteSponsorResource` that `SponsorRecord` is transcribed from, which is
 * why the reply is `CreatedSponsorRecord` and not `SponsorRecord`. It was
 * `unknown`; the two resources are now both modelled, so the difference is
 * stated rather than left as an absence. No caller reads it today.
 */
export const createSponsors = (
  formData: FormData
): Promise<ApiResponse<CreatedSponsorRecord>> =>
  apiClient.post("/sponsors/", formData).then((r) => r.data);
