"use client";

import { useTranslation } from "react-i18next";

/**
 * The chip rows above a profile's tabs, and the tab styling they share.
 *
 * `FilterChips` is generic over its value type so the opportunity-type row and
 * the activity-tag row are the same component rather than two near-copies.
 */
export const TAB_TRIGGER_CLASS =
  "xsl:w-[150px] xss:w-[110px] shrink-0 relative px-6 xs:px-2 2xl:py-5 lg:py-3 md:py-3 py-3 xss:py-3 laptopmain:py-5 rounded-t-[20px] rounded-b-[0px] data-[state=active]:bg-white border-t border-l border-r data-[state=active]:border-t data-[state=active]:border-l data-[state=active]:border-r data-[state=active]:border-primary-5 data-[state=inactive]:border-primary-5 data-[state=active]:text-primary-5 data-[state=active]:font-bold data-[state=inactive]:bg-[#D2D8F6] before:content-[''] before:absolute before:top-[96%] data-[state=active]:before:top-[99%] before:left-0 before:w-full before:h-[3px] before:bg-[#D2D8F6] before:hidden data-[state=active]:before:block data-[state=active]:before:bg-[#fff] after:content-[''] after:absolute after:w-[16px] after:bg-primary-5 after:left-[-16px] rtl:after:left-[0] rtl:after:right-[-16px] after:bottom-[-1px] after:block";

/**
 * The opportunity tabs split by type on top of the organized/sponsored tag.
 * `/list-all-opportunities/` accepts both the long `volunteer_opportunity` /
 * `learn_serve_opportunity` form used here and the short `volunteer` / `learn`
 * one (BE-15) — `CommonProfile.tsx` sends the same long form, so keep them in
 * step if either changes.
 */
export type OpportunityTypeFilter = "all" | "volunteer" | "development";

export const OPPORTUNITY_TYPE_TABS: Array<{
  value: OpportunityTypeFilter;
  labelKey: string;
  param?: string;
}> = [
  { value: "all", labelKey: "COMMON.ALL" },
  { value: "volunteer", labelKey: "COMMON.VOLUNTEER", param: "volunteer_opportunity" },
  { value: "development", labelKey: "COMMON.LEARN_SERVE", param: "learn_serve_opportunity" },
];

/**
 * The pill row used above a listing. Presentational — the caller resolves each
 * label, because the opportunity chips are i18n keys while the certificate
 * chips are already-localized values from the API.
 */
export function FilterChips<T extends string>({
  tabs,
  value,
  onChange,
}: {
  tabs: ReadonlyArray<{ value: T; label: string }>;
  value: T;
  onChange: (value: T) => void;
}) {
  return (
    // `shrink-0` matters: without it a narrow row squeezes the group down to
    // one chip wide and it stacks into a vertical column instead of staying a
    // row. The group keeps its width and the *container* wraps it instead.
    <div className="flex shrink-0 flex-wrap gap-2">
      {tabs.map((type) => (
        <button
          key={type.value}
          type="button"
          onClick={() => onChange(type.value)}
          className={`rounded-full border border-primary-5 px-4 py-2 text-sm font-bold whitespace-nowrap ${
            value === type.value
              ? "bg-primary-5 text-white"
              : "bg-white text-primary-5"
          }`}
        >
          {type.label}
        </button>
      ))}
    </div>
  );
}

export function OpportunityTypeChips({
  value,
  onChange,
}: {
  value: OpportunityTypeFilter;
  onChange: (value: OpportunityTypeFilter) => void;
}) {
  const { t } = useTranslation();
  return (
    <FilterChips
      tabs={OPPORTUNITY_TYPE_TABS.map((tab) => ({
        value: tab.value,
        label: t(tab.labelKey),
      }))}
      value={value}
      onChange={onChange}
    />
  );
}

/**
 * The role the profile owner played in an activity. A development activity
 * counts towards their counter whichever of the two it was — they took part in
 * it (Participant) or they ran it (Provider) — so the listing can be split by
 * that role.
 *
 * **Development opportunities only.** The rule is stated about فرص تطور, and a
 * volunteer opportunity has no Provider side on a volunteer's profile, so these
 * chips are shown only while the Development type chip is active and the filter
 * resets whenever the type moves off it.
 *
 * The value is sent to the listing endpoints as `profile_activity_tag`; "All"
 * omits the param. The backend matches the input case-insensitively while row
 * payloads use lowercase `participant` / `provider`.
 */
export const ACTIVITY_TAG_TABS: ReadonlyArray<{
  value: string;
  labelKey: string;
  /** The API value. Absent for "All", which sends nothing. */
  param?: string;
}> = [
  { value: "all", labelKey: "COMMON.ALL" },
  { value: "participant", labelKey: "COMMON.PARTICIPANT", param: "Participant" },
  { value: "provider", labelKey: "COMMON.PROVIDER", param: "Provider" },
];

export function ActivityTagChips({
  value,
  onChange,
}: {
  value: string;
  onChange: (value: string) => void;
}) {
  const { t } = useTranslation();
  return (
    <FilterChips
      tabs={ACTIVITY_TAG_TABS.map((tab) => ({
        value: tab.value,
        label: t(tab.labelKey),
      }))}
      value={value}
      onChange={onChange}
    />
  );
}

/**
 * The certificates tab splits by what the certificate was earned for.
 *
 * The list of types is **admin-editable master data**, fetched from
 * `GET /api/choices/certificate_filter_type/` like every other dropdown in the
 * app — not a list hardcoded here. The selected value is then sent to
 * `/user-certificates/` as `certificate_type`, so the API does the filtering;
 * see `getUserCertificates()`.
 *
 * The existing `filter-type` choice is deliberately **not** reused: it is
 * shared with the opportunities filter modal and carries `Class` and
 * `Consultation` as well, neither of which can ever have a certificate (BE-47).
 */
