"use client";

import { type ReactNode, useState } from "react";
import { useTranslation } from "react-i18next";
import { Tabs, TabsContent, TabsList, TabsTrigger } from "@/modules/ui/components/tabs";

interface QuestionTabsProps {
  /** The question's form: headline, options and the other things a respondent sees. */
  content: ReactNode;
  /** The Settings tab body: order, display options, validation rules, Recode. */
  settings: ReactNode;
  /** A code problem lives on the Settings tab: the tabs open there, and flag it while Content is showing. */
  hasSettingsIssue?: boolean;
}

/**
 * Content / Settings tabs of one question, replacing the old "Show question settings" collapsible.
 * Both tabs take ready-made nodes, so a later ticket can add to either without touching the tabs.
 */
export const QuestionTabs = ({
  content,
  settings,
  hasSettingsIssue = false,
}: Readonly<QuestionTabsProps>) => {
  const { t } = useTranslation();
  // The tabs mount when the question is opened, so a question opened with a code problem starts on
  // Settings, where the Recode field shows it (the publish check scrolls to the card, not into it).
  const [tab, setTab] = useState(hasSettingsIssue ? "settings" : "content");

  return (
    <Tabs value={tab} onValueChange={setTab} className="mt-2 gap-4">
      <TabsList variant="underline" className="w-full">
        <TabsTrigger value="content" variant="underline" className="py-2">
          {t("workspace.surveys.edit.question_tab_content")}
        </TabsTrigger>
        <TabsTrigger value="settings" variant="underline" className="py-2">
          {t("common.settings")}
          {hasSettingsIssue && (
            <>
              <span aria-hidden="true" className="ml-1.5 inline-block size-2 rounded-full bg-red-500" />
              <span className="sr-only">{t("workspace.surveys.edit.code_needs_attention")}</span>
            </>
          )}
        </TabsTrigger>
      </TabsList>
      <TabsContent value="content" className="[&_form]:space-y-1.5">
        {content}
      </TabsContent>
      <TabsContent value="settings">{settings}</TabsContent>
    </Tabs>
  );
};
