"use client";

import { PlusIcon } from "lucide-react";
import Link from "next/link";
import { useRouter } from "next/navigation";
import { useMemo, useState } from "react";
import { toast } from "react-hot-toast";
import { useTranslation } from "react-i18next";
import {
  TFeedbackSourceImportMode,
  TFeedbackSourceType,
  TFeedbackSourceWithMappings,
  THubTargetField,
} from "@formbricks/types/feedback-source";
import { useWorkspace } from "@/app/(app)/workspaces/[workspaceId]/context/workspace-context";
import {
  createFeedbackSourceWithMappingsAction,
  deleteFeedbackSourceAction,
  importHistoricalResponsesAction,
  updateFeedbackSourceWithMappingsAction,
} from "@/lib/feedback-source/actions";
import { getFormattedErrorMessage } from "@/lib/utils/error-message";
import {
  hasRestrictedAttachedSurvey,
  isRestrictedSurveyPick,
} from "@/modules/survey/visibility/lib/outbound";
import { Alert, AlertButton, AlertDescription } from "@/modules/ui/components/alert";
import { Button } from "@/modules/ui/components/button";
import { PageContentWrapper } from "@/modules/ui/components/page-content-wrapper";
import { PageHeader } from "@/modules/ui/components/page-header";
import { UnifyConfigNavigation } from "../../components/unify-config-navigation";
import { TFieldMapping, TUnifySurvey, getTranslatedFeedbackSourceError } from "../types";
import {
  type TFeedbackImportTotals,
  getMappedSurveyIds,
  getSelectableQuestionIds,
  getSuggestedSurveys,
  notifyImportResult,
  sumImportTotals,
} from "../utils";
import { CreateFeedbackSourceModal } from "./create-feedback-source-modal";
import { CsvImportModal } from "./csv-import-modal";
import { EditFeedbackSourceModal } from "./edit-feedback-source-modal";
import { FeedbackSourcesTable } from "./feedback-sources-table";

interface FeedbackSourcesSectionProps {
  workspaceId: string;
  initialFeedbackSources: TFeedbackSourceWithMappings[];
  initialSurveys: TUnifySurvey[];
  directories: { id: string; name: string }[];
  isReadOnly: boolean;
  /** ENG-3395: the restricted-surveys gate. */
  surveyVisibilityEnabled: boolean;
}

export function FeedbackSourcesSection({
  workspaceId,
  initialFeedbackSources,
  initialSurveys,
  directories,
  isReadOnly,
  surveyVisibilityEnabled,
}: Readonly<FeedbackSourcesSectionProps>) {
  const { t } = useTranslation();
  const { workspace } = useWorkspace();
  const router = useRouter();
  const [isCreateModalOpen, setIsCreateModalOpen] = useState(false);
  // Survey to prefill the create modal with when opened from a "Select questions for import" suggestion.
  const [prefillSurveyId, setPrefillSurveyId] = useState<string | null>(null);
  const [editingFeedbackSource, setEditingFeedbackSource] = useState<TFeedbackSourceWithMappings | null>(
    null
  );
  const [csvImportFeedbackSource, setCsvImportFeedbackSource] = useState<TFeedbackSourceWithMappings | null>(
    null
  );
  const surveyNameById = useMemo(
    () => Object.fromEntries(initialSurveys.map((survey) => [survey.id, survey.name])),
    [initialSurveys]
  );
  // A survey can only back one feedback source, so surveys already connected are disabled in the picker.
  const connectedSurveyIds = useMemo(
    () =>
      initialFeedbackSources.flatMap((source) =>
        source.formbricksMappings.map((mapping) => mapping.surveyId)
      ),
    [initialFeedbackSources]
  );
  // Surveys that aren't backing a source yet (and aren't drafts) are surfaced as "Suggestions" below the table.
  const suggestedSurveys = useMemo(
    () => getSuggestedSurveys(initialSurveys, connectedSurveyIds),
    [initialSurveys, connectedSurveyIds]
  );
  // Sources that replay a restricted survey: the pipeline skips them and a historical import must not
  // copy the survey's responses out, so they are marked paused and cannot re-import.
  const restrictedSourceIds = new Set(
    initialFeedbackSources
      .filter((source) =>
        hasRestrictedAttachedSurvey(surveyVisibilityEnabled, getMappedSurveyIds(source), initialSurveys)
      )
      .map((source) => source.id)
  );
  const directoryNames = directories.map((directory) => directory.name).join(", ");
  const feedbackDirectoryAccessText =
    directories.length === 1
      ? t("workspace.unify.feedback_sources_directory_access_single", {
          directoryNames,
        })
      : t("workspace.unify.feedback_sources_directory_access_multiple", {
          directoryNames,
        });

  const handleCreateFeedbackSource = async (data: {
    name: string;
    type: TFeedbackSourceType;
    feedbackDirectoryId: string;
    importMode?: TFeedbackSourceImportMode;
    surveyMappings?: { surveyId: string; elementIds: string[] }[];
    fieldMappings?: TFieldMapping[];
  }): Promise<string | undefined> => {
    const result = await createFeedbackSourceWithMappingsAction({
      workspaceId: workspaceId,
      feedbackSourceInput: {
        name: data.name,
        type: data.type,
        feedbackDirectoryId: data.feedbackDirectoryId,
        importMode: data.importMode,
      },
      formbricksMappings:
        data.type === "formbricks_survey" && data.surveyMappings?.length ? data.surveyMappings : undefined,
      fieldMappings:
        data.type !== "formbricks_survey" && data.fieldMappings?.length
          ? data.fieldMappings.map((m) => ({
              sourceFieldId: m.sourceFieldId || "",
              targetFieldId: m.targetFieldId as THubTargetField,
              staticValue: m.staticValue,
            }))
          : undefined,
    });

    if (!result?.data) {
      toast.error(getTranslatedFeedbackSourceError(getFormattedErrorMessage(result), t));
      return undefined;
    }

    router.refresh();
    return result.data.id;
  };

  const handleUpdateFeedbackSource = async (data: {
    feedbackSourceId: string;
    workspaceId: string;
    name: string;
    importMode?: TFeedbackSourceImportMode;
    surveyMappings?: { surveyId: string; elementIds: string[] }[];
    fieldMappings?: TFieldMapping[];
  }): Promise<boolean> => {
    const result = await updateFeedbackSourceWithMappingsAction({
      feedbackSourceId: data.feedbackSourceId,
      workspaceId: workspaceId,
      feedbackSourceInput: {
        name: data.name,
        importMode: data.importMode,
      },
      formbricksMappings: data.surveyMappings?.length ? data.surveyMappings : undefined,
      fieldMappings: data.fieldMappings?.length
        ? data.fieldMappings.map((m) => ({
            sourceFieldId: m.sourceFieldId || "",
            targetFieldId: m.targetFieldId as THubTargetField,
            staticValue: m.staticValue,
          }))
        : undefined,
    });

    if (!result?.data) {
      toast.error(getTranslatedFeedbackSourceError(getFormattedErrorMessage(result), t));
      return false;
    }

    toast.success(t("workspace.unify.source_updated_successfully"));
    router.refresh();
    return true;
  };

  const handleDeleteFeedbackSource = async (feedbackSourceId: string): Promise<void> => {
    const result = await deleteFeedbackSourceAction({ feedbackSourceId, workspaceId: workspaceId });

    if (!result?.data) {
      toast.error(getTranslatedFeedbackSourceError(getFormattedErrorMessage(result), t));
      return;
    }

    toast.success(t("workspace.unify.source_deleted_successfully"));
    router.refresh();
  };

  // "Select questions for import": open the create modal prefilled with the suggested survey.
  const handleSelectQuestions = (survey: TUnifySurvey): void => {
    setPrefillSurveyId(survey.id);
    setIsCreateModalOpen(true);
  };

  // "Import responses": one-click create the source (all supported questions) and import historical data.
  const handleImportResponses = async (survey: TUnifySurvey): Promise<void> => {
    if (isRestrictedSurveyPick(surveyVisibilityEnabled, survey)) {
      toast.error(t("workspace.surveys.visibility.outbound_restricted_tooltip"));
      return;
    }
    const feedbackDirectoryId = directories[0]?.id;
    if (!feedbackDirectoryId) {
      toast.error(t("workspace.unify.no_feedback_directory_available"));
      return;
    }

    const elementIds = getSelectableQuestionIds(survey);
    if (elementIds.length === 0) {
      toast.error(t("workspace.unify.error_source_questions_required"));
      return;
    }

    // Explicitly completedOnly rather than leaning on the column default: this path imports
    // immediately with no chance to choose, and "all" pulls in answers respondents never submitted
    // and sends them to the AI enrichments. A one-click action must not opt someone into that
    // silently — picking partials is a decision, so it lives on the "Select questions for import"
    // route beside this button, which opens the modal with the choice.
    const feedbackSourceId = await handleCreateFeedbackSource({
      name: t("workspace.unify.source_connector_name", { surveyName: survey.name }),
      type: "formbricks_survey",
      feedbackDirectoryId,
      importMode: "completedOnly",
      surveyMappings: [{ surveyId: survey.id, elementIds }],
    });

    if (!feedbackSourceId) {
      return;
    }

    try {
      const importResult = await importHistoricalResponsesAction({
        feedbackSourceId,
        workspaceId,
        surveyId: survey.id,
      });

      if (importResult?.data) {
        announceImportResult(importResult.data);
      } else {
        // The source was created; only the historical import failed.
        toast.error(getTranslatedFeedbackSourceError(getFormattedErrorMessage(importResult), t));
      }
    } catch {
      toast.error(t("common.something_went_wrong"));
    }

    router.refresh();
  };

  /** Announces the totals, picking the toast by `failures` — see `notifyImportResult`. */
  const announceImportResult = (totals: TFeedbackImportTotals): void => {
    const { successes, failures, skipped } = totals;
    notifyImportResult(
      totals,
      t("workspace.unify.historical_import_complete", { successes, failures, skipped })
    );
  };

  /**
   * Replays the linked survey's historic responses into this source again, so questions added to the
   * mapping after the first import no longer need the source deleted and rebuilt (ENG-1889).
   *
   * Safe to run repeatedly: `importHistoricalResponses` reconciles rather than inserts, and carries
   * a `snapshotAt` so a record the live pipeline has since corrected is not reverted to this older
   * copy. It also honours the source's saved `importMode`, so a re-import never widens a
   * completed-only source to partials.
   *
   * What it does NOT repair: a question whose mapped `hubFieldType` changed. `field_type` and
   * `field_label` identify a record on create and are absent from Hub's update request (see
   * `UPDATE_FIELD_KEYS` in `lib/feedback-source/reconcile.ts`), so an already-imported record keeps
   * its original type and only gains the new value column. Fixing that belongs in `reconcile.ts`.
   */
  const handleReimportHistoricalData = async (feedbackSource: TFeedbackSourceWithMappings): Promise<void> => {
    // Unreachable from the menu, which disables the item for such a source; kept for any other caller.
    if (restrictedSourceIds.has(feedbackSource.id)) {
      toast.error(t("workspace.surveys.visibility.outbound_restricted_tooltip"));
      return;
    }
    const surveyIds = getMappedSurveyIds(feedbackSource);
    // Unreachable from the menu, which renders the item only under `canReimportHistoricalData` —
    // itself this same predicate. Kept so a future caller of `onReimport` that does not gate fails
    // loudly instead of running an import that reports "0 succeeded".
    if (surveyIds.length === 0) {
      toast.error(t("workspace.unify.reimport_no_survey_mapped"));
      return;
    }

    try {
      const totals: TFeedbackImportTotals[] = [];
      // Sequential rather than concurrent: each import walks every response of a survey in batches
      // and writes them into the same feedback directory, so running them in parallel would only
      // multiply the load on one directory. Normally there is a single survey anyway.
      for (const surveyId of surveyIds) {
        const importResult = await importHistoricalResponsesAction({
          feedbackSourceId: feedbackSource.id,
          workspaceId,
          surveyId,
        });

        if (!importResult?.data) {
          toast.error(getTranslatedFeedbackSourceError(getFormattedErrorMessage(importResult), t));
          return;
        }

        totals.push(importResult.data);
      }

      announceImportResult(sumImportTotals(totals));
    } catch {
      toast.error(t("common.something_went_wrong"));
      return;
    }

    router.refresh();
  };

  const handleToggleStatus = async (feedbackSource: TFeedbackSourceWithMappings): Promise<void> => {
    const newStatus = feedbackSource.status === "active" ? "paused" : "active";
    const result = await updateFeedbackSourceWithMappingsAction({
      feedbackSourceId: feedbackSource.id,
      workspaceId: workspaceId,
      feedbackSourceInput: { status: newStatus },
    });

    if (!result?.data) {
      toast.error(getTranslatedFeedbackSourceError(getFormattedErrorMessage(result), t));
      return;
    }

    toast.success(t("workspace.unify.source_status_updated_successfully"));
    router.refresh();
  };

  return (
    <PageContentWrapper>
      <PageHeader
        pageTitle={t("workspace.unify.feedback_data")}
        cta={
          isReadOnly ? undefined : (
            <Button size="sm" onClick={() => setIsCreateModalOpen(true)}>
              <PlusIcon className="mr-2 size-4" />
              {t("workspace.unify.add_feedback_source")}
            </Button>
          )
        }>
        <UnifyConfigNavigation workspaceId={workspaceId} activeId="sources" />
      </PageHeader>

      <FeedbackSourcesTable
        feedbackSources={initialFeedbackSources}
        surveyNameById={surveyNameById}
        suggestedSurveys={suggestedSurveys}
        workspaceId={workspaceId}
        onFeedbackSourceClick={setEditingFeedbackSource}
        onCsvImport={setCsvImportFeedbackSource}
        onReimport={handleReimportHistoricalData}
        onToggleStatus={handleToggleStatus}
        onDelete={handleDeleteFeedbackSource}
        onImportResponses={handleImportResponses}
        onSelectQuestions={handleSelectQuestions}
        isLoading={false}
        isReadOnly={isReadOnly}
        surveyVisibilityEnabled={surveyVisibilityEnabled}
        restrictedSourceIds={restrictedSourceIds}
      />
      {directories.length > 0 && (
        <Alert size="small" className="mt-4" alertRole="status">
          <AlertDescription>{feedbackDirectoryAccessText}</AlertDescription>
          {!isReadOnly && workspace?.organizationId && (
            <AlertButton asChild>
              <Link href={`/organizations/${workspace.organizationId}/settings/feedback-directories`}>
                {t("workspace.unify.manage_directories")}
              </Link>
            </AlertButton>
          )}
        </Alert>
      )}

      <CreateFeedbackSourceModal
        open={isCreateModalOpen}
        onOpenChange={(open) => {
          setIsCreateModalOpen(open);
          if (!open) setPrefillSurveyId(null);
        }}
        onCreateFeedbackSource={handleCreateFeedbackSource}
        surveys={initialSurveys}
        connectedSurveyIds={connectedSurveyIds}
        workspaceId={workspaceId}
        directories={directories}
        initialSurveyId={prefillSurveyId}
        showTrigger={false}
        surveyVisibilityEnabled={surveyVisibilityEnabled}
      />

      <EditFeedbackSourceModal
        feedbackSource={editingFeedbackSource}
        isReadOnly={isReadOnly}
        open={editingFeedbackSource !== null}
        onOpenChange={(open) => !open && setEditingFeedbackSource(null)}
        onUpdateFeedbackSource={handleUpdateFeedbackSource}
        surveys={initialSurveys}
        surveyVisibilityEnabled={surveyVisibilityEnabled}
        onOpenCsvImport={() => {
          if (editingFeedbackSource) {
            setCsvImportFeedbackSource(editingFeedbackSource);
          }
        }}
      />

      {csvImportFeedbackSource && (
        <CsvImportModal
          open={csvImportFeedbackSource !== null}
          onOpenChange={(open) => !open && setCsvImportFeedbackSource(null)}
          feedbackSourceId={csvImportFeedbackSource.id}
          workspaceId={csvImportFeedbackSource.workspaceId}
          fieldMappings={csvImportFeedbackSource.fieldMappings}
          onOpenEditFeedbackSource={() => {
            setEditingFeedbackSource(csvImportFeedbackSource);
          }}
        />
      )}
    </PageContentWrapper>
  );
}
