"use client";

import type { FeatureId } from "@enode/core/plan/plan-summary";
import { useTranslation } from "@enode/core/translations/hooks";
import { CheckIcon, MinusIcon } from "../icons";
import { ChevronRightIcon } from "../settings/icons";
import { SettingsCardGroup } from "../settings/settings-card-group";

/** Plain-word names of the plan features (kept literal so extraction finds them). */
export function usePlanFeatureWords(): Record<FeatureId, string> {
  const { t } = useTranslation();
  return {
    liveHub: t("Live Hub"),
    planner: t("Planner"),
    insights: t("Insights"),
    dataExport: t("Data export"),
    oldDataImport: t("Import of old data"),
    videoStorage: t("Video storage"),
  };
}

/** Props of {@link PlanFeatureList}. */
export type PlanFeatureListProps = {
  /** From `planFeatureWords` (`@enode/core/plan/plan-summary`). */
  included: readonly FeatureId[];
  notIncluded: readonly FeatureId[];
  /** Makes "Data export" a link (the portal's Data & import tab); plain text without. */
  dataExportHref?: string;
};

/**
 * The "Included" card of Plan & usage in the portal and the app: what the
 * plan includes (check) and what it doesn't (dash, "Not included" for screen
 * readers). Renders nothing when there are no features to list.
 */
export function PlanFeatureList({
  included,
  notIncluded,
  dataExportHref,
}: PlanFeatureListProps) {
  const { t } = useTranslation();
  const words = usePlanFeatureWords();
  if (included.length === 0 && notIncluded.length === 0) return null;
  return (
    <SettingsCardGroup header={t("Included")} findId="acc-plan.included">
      <ul className="grid grid-cols-1 gap-x-6 gap-y-3 px-4 py-4 md:grid-cols-2">
        {included.map((id) => (
          <li key={id} className="flex items-center gap-2.5">
            <CheckIcon className="size-4 shrink-0 text-brand-red-orange" />
            {id === "dataExport" && dataExportHref ? (
              <a
                href={dataExportHref}
                className="body-normal group inline-flex items-center gap-1 text-foreground-default hover:underline"
              >
                {words[id]}
                <ChevronRightIcon className="size-3.5 text-foreground-muted" />
              </a>
            ) : (
              <span className="body-normal text-foreground-default">{words[id]}</span>
            )}
          </li>
        ))}
        {notIncluded.map((id) => (
          <li key={id} className="flex items-center gap-2.5 text-foreground-muted">
            <MinusIcon className="size-4 shrink-0" />
            <span className="body-normal">
              {words[id]}
              <span className="sr-only"> {t("Not included")}</span>
            </span>
          </li>
        ))}
      </ul>
    </SettingsCardGroup>
  );
}
