import type { SponsorItem } from "@/features/home/types";
/**
 * The sponsor payload the public partners page reads.
 *
 * Transcribed from `WebsiteSponsorResource::toArray()`, which is what
 * `GET /sponsors/` answers with (`SponsorController::index`, filtered to
 * `approval_status = approved`). Six keys, no pagination.
 *
 * Both choice keys go through `websiteChoicePayload()`, which drops null
 * entries with `array_filter` — so `value_en` can be *absent*, not just null,
 * and the whole object is null when the relation is unset. Every read of them
 * on the partners page is therefore optional-chained, and the category
 * predicates below have to tolerate a missing label rather than assume one.
 */

/** A `*_sponsor_type` / `_type_of_support` choice, as the website resource emits it. */
export interface SponsorChoice {
  id?: number;
  value_en?: string;
  value_ar?: string;
}

export interface SponsorRecord {
  id: number;
  org_name: string | null;
  person_name: string | null;
  /** `getimg()` — absolute URL, or null when no logo was uploaded. */
  sponsor_logo: string | null;
  _sponsor_type: SponsorChoice | null;
  _type_of_support: SponsorChoice | null;
}

/** A sponsor reduced to what the carousel tile renders. */
export interface SponsorTile {
  type?: SponsorChoice | null;
  level?: SponsorChoice | null;
  name: string | null;
  logo: string | null;
}

export function toSponsorTile(sponsor: SponsorRecord): SponsorTile {
  return { name: sponsor.org_name, logo: sponsor.sponsor_logo, type: sponsor._sponsor_type, level: sponsor._type_of_support };
}

/**
 * The approved sponsors as the logo strip draws them (`SponsorsMarquee`).
 * `org_name` is nullable on the website resource; a person-only sponsor is
 * named by its contact so the tile is never blank when the logo is missing.
 */
export function toSponsorItems(records: SponsorRecord[] | null | undefined): SponsorItem[] {
  return (Array.isArray(records) ? records : []).map((sponsor) => ({
    id: sponsor.id,
    sponsor_logo: sponsor.sponsor_logo,
    org_name: sponsor.org_name || sponsor.person_name || "",
  }));
}

/**
 * `Sponsor\SponsorResource` — the **admin** shape, which is what
 * `POST /sponsors/` answers with. `SponsorRecord` above is
 * `WebsiteSponsorResource`, the read side, and the two differ in both
 * directions:
 *
 *  - the choices arrive as raw `*_id` columns here, where the website resource
 *    nests the resolved `_sponsor_type` / `_type_of_support` objects;
 *  - the whole application is exposed (contact details, the free-text answers,
 *    `approval_status`), where the website resource shows only what a logo
 *    tile needs.
 *
 * `documents` is `whenLoaded`, and `store()` resolves
 * `$sponsor->load('documents')`, so on *this* endpoint it is always present —
 * declared optional because it is the resource that decides, not the caller.
 */
export interface CreatedSponsorRecord {
  id: number;
  sponsor_type_id: number | null;
  org_name: string | null;
  org_type_id: number | null;
  person_name: string | null;
  email: string | null;
  country_code: string | null;
  phone_number: string | null;
  type_of_support_id: number | null;
  sponsorship_details: string | null;
  why_interested: string | null;
  resources_expected: string | null;
  /** `getimg()` — absolute URL, or null when no logo was uploaded. */
  sponsor_logo: string | null;
  /** Always `"pending"` on a create; the enum's `->value`. */
  approval_status: string | null;
  preferred_language: string | null;
  documents?: { id: number; document: string }[];
  created_at: string | null;
}
