"use client";

import type { TranslateFn } from "@enode/core/translations/hooks";
import { useTranslation } from "@enode/core/translations/hooks";
import { BottomSheetCard } from "../bottom-sheet-card";
import { Button } from "../button";
import { roleOrganisationLine } from "./account-identity-lines";

/** The member roles the sheet explains (owners manage the plan themselves). */
export type MemberRole = "coach" | "athlete";

/**
 * What a coach or athlete may do, in plain words, plus where to ask for more.
 * Matches the apps' role gates: coaches plan and run workouts for the
 * athletes they look after and record on the plan's training stations;
 * athletes train their coach's workouts or their own and see only their data.
 *
 * @param organisationName - the organisation; null reads "your organisation".
 */
export function yourAccessLines(
  t: TranslateFn,
  role: MemberRole,
  organisationName: string | null,
): { canDo: string; needMore: string } {
  const canDo =
    role === "athlete"
      ? t("Train with your coach's workouts or your own, and see your own data.")
      : t("Plan and run workouts for your athletes, see their data and use the plan's training stations.");
  const needMore = t("Need more? Ask the owner of {organisation}.", {
    organisation: organisationName ?? t("your organisation"),
  });
  return { canDo, needMore };
}

/** Props of {@link YourAccessContent}. */
export type YourAccessContentProps = {
  role: MemberRole;
  /** The organisation's name; null when the server sent none. */
  organisationName: string | null;
  /**
   * The organisation's plan name, when the client has it. Members get no
   * licence on their user read today, so this is usually null and the line
   * is left out.
   */
  planName?: string | null;
  onClose: () => void;
};

/**
 * The read-only body of the "Your access" sheet: role and organisation, the
 * plan (when known), what the role can do and who to ask for more.
 */
export function YourAccessContent({
  role,
  organisationName,
  planName,
  onClose,
}: YourAccessContentProps) {
  const { t } = useTranslation();
  const lines = yourAccessLines(t, role, organisationName);
  return (
    <div className="px-6 pb-2 pt-6">
      <h2 className="heading-large">{t("Your access")}</h2>
      <p className="body-small mt-1 text-foreground-muted">
        {roleOrganisationLine(t, role, organisationName)}
        {planName ? ` · ${planName}` : null}
      </p>
      <p className="body-normal mt-4 text-foreground-default">{lines.canDo}</p>
      <p className="body-normal mt-2 text-foreground-muted">{lines.needMore}</p>
      <div className="mt-5 flex flex-col">
        <Button variant="neutral" size="large" onClick={onClose}>
          {t("Close")}
        </Button>
      </div>
    </div>
  );
}

/**
 * "Your access": the small read-only sheet behind the Profile line "You have
 * a place in {organisation}'s plan." Same sheet in the portal and the app.
 */
export function YourAccessSheet({
  open,
  ...content
}: YourAccessContentProps & { open: boolean }) {
  const { t } = useTranslation();
  return (
    <BottomSheetCard
      open={open}
      onClose={content.onClose}
      ariaLabel={t("Your access")}
      zIndex={260}
      innerClassName="max-w-sm"
    >
      <YourAccessContent {...content} />
    </BottomSheetCard>
  );
}
