"use client";

import type { TFunction } from "i18next";
import type { ReactNode } from "react";
import { Trans, useTranslation } from "react-i18next";
import { Tooltip, TooltipContent, TooltipProvider, TooltipTrigger } from "@/modules/ui/components/tooltip";
import type { TResponseOverageDisplay } from "../lib/stripe-billing-catalog";

type TResponsePricingPlan = "pro" | "scale";

const formatTierUnit = (value: number, locale: string) => {
  return value.toLocaleString(locale);
};

const formatPerUnitPrice = (perUnitCents: number, currency: string, locale: string) => {
  return new Intl.NumberFormat(locale, {
    style: "currency",
    currency: currency.toUpperCase(),
    minimumFractionDigits: 2,
    maximumFractionDigits: 2,
  }).format(perUnitCents / 100);
};

const ResponsePricingTable = ({
  overage,
  locale,
}: Readonly<{ overage: TResponseOverageDisplay; locale: string }>) => {
  const { t } = useTranslation();

  return (
    <table className="w-full text-left text-xs">
      <thead>
        <tr className="border-b border-slate-200 text-slate-500">
          <th className="pr-4 pb-2 font-medium">
            {t("workspace.settings.billing.dynamic_pricing_first_unit")}
          </th>
          <th className="pr-4 pb-2 font-medium">
            {t("workspace.settings.billing.dynamic_pricing_last_unit")}
          </th>
          <th className="pb-2 font-medium">{t("workspace.settings.billing.dynamic_pricing_per_unit")}</th>
        </tr>
      </thead>
      <tbody>
        {overage.tiers.map((tier) => (
          <tr key={`${tier.firstUnit}-${tier.lastUnit ?? "unlimited"}`} className="text-slate-700">
            <td className="py-1 pr-4 tabular-nums">{formatTierUnit(tier.firstUnit, locale)}</td>
            <td className="py-1 pr-4 tabular-nums">
              {tier.lastUnit === null
                ? t("workspace.settings.billing.dynamic_pricing_unlimited")
                : formatTierUnit(tier.lastUnit, locale)}
            </td>
            <td className="py-1 tabular-nums">
              {formatPerUnitPrice(tier.perUnitCents, overage.currency, locale)}
            </td>
          </tr>
        ))}
      </tbody>
    </table>
  );
};

interface PlanResponseFeatureProps {
  plan: TResponsePricingPlan;
  locale: string;
  overage: TResponseOverageDisplay | null;
  t: TFunction;
}

// Composed from the tooltip primitives rather than `TooltipRenderer`, whose `<span>` trigger the
// keyboard cannot reach. A `<button>` takes focus, and Radix opens the tooltip on focus, so Tab
// reaches the rate table too.
const PricingTiersTooltip = ({
  overage,
  locale,
  children,
}: Readonly<{ overage: TResponseOverageDisplay; locale: string; children?: ReactNode }>) => (
  <TooltipProvider delayDuration={0}>
    <Tooltip>
      <TooltipTrigger asChild>
        <button type="button" className="cursor-help border-b border-dotted border-slate-400">
          {children}
        </button>
      </TooltipTrigger>
      <TooltipContent className="max-w-none p-3">
        <ResponsePricingTable overage={overage} locale={locale} />
      </TooltipContent>
    </Tooltip>
  </TooltipProvider>
);

// The <span /> placeholder child is replaced by react-i18next's <Trans> with
// the translated text inside the <dynamicPricing> tag.
const dynamicPricingComponent = (overage: TResponseOverageDisplay | null, locale: string) => {
  if (!overage) {
    return <span />;
  }

  return (
    <PricingTiersTooltip overage={overage} locale={locale}>
      <span />
    </PricingTiersTooltip>
  );
};

export const PlanResponseFeature = ({ plan, locale, overage, t }: Readonly<PlanResponseFeatureProps>) => {
  const i18nKey =
    plan === "pro"
      ? t("workspace.settings.billing.plan_pro_feature_responses")
      : t("workspace.settings.billing.plan_scale_feature_responses");

  return (
    <Trans i18nKey={i18nKey} components={{ dynamicPricing: dynamicPricingComponent(overage, locale) }} />
  );
};

interface ResponseOverageNoteProps {
  limit: number;
  locale: string;
  overage: TResponseOverageDisplay;
}

// Shown under the Responses usage bar on plans with metered overage, so the bar's limit reads as an
// included volume rather than a hard cap.
export const ResponseOverageNote = ({ limit, locale, overage }: Readonly<ResponseOverageNoteProps>) => {
  return (
    <p className="text-sm text-slate-500">
      <Trans
        i18nKey="workspace.settings.billing.responses_above_limit_billed_per_response"
        values={{ limit: limit.toLocaleString(locale) }}
        components={{ dynamicPricing: dynamicPricingComponent(overage, locale) }}
      />
    </p>
  );
};

interface PlanWorkflowRunsFeatureProps {
  locale: string;
  overage: TResponseOverageDisplay | null;
  t: TFunction;
}

// Workflow runs are metered graduated like responses (Scale only). Same tier tooltip, sourced from
// the workflow price's tiers so the shown pricing matches what Stripe charges (ENG-2194).
export const PlanWorkflowRunsFeature = ({ locale, overage, t }: Readonly<PlanWorkflowRunsFeatureProps>) => {
  return (
    <Trans
      i18nKey={t("workspace.settings.billing.plan_scale_feature_workflow_runs")}
      components={{ dynamicPricing: dynamicPricingComponent(overage, locale) }}
    />
  );
};
