import { type Dispatch, type SetStateAction } from "react";
import { ActionClass, OrganizationRole } from "@formbricks/database/prisma-browser";
import { TContactAttributeKey } from "@formbricks/types/contact-attribute-key";
import { TSurveyQuota } from "@formbricks/types/quota";
import { TSegment } from "@formbricks/types/segment";
import { TSurvey } from "@formbricks/types/surveys/types";
import { TUserLocale } from "@formbricks/types/user";
import { TargetingCard } from "@/modules/ee/contacts/segments/components/targeting-card";
import type { TSurveyLinkedCreative } from "@/modules/ee/creatives/lib/survey-side";
import { getCreativeOpenedMode } from "@/modules/ee/creatives/lib/survey-side";
import { SurveyRetentionCard } from "@/modules/ee/data-retention/components/survey/survey-retention-card";
import type { TSurveyDataRetentionContext } from "@/modules/ee/data-retention/types";
import { QuotasCard } from "@/modules/ee/quotas/components/quotas-card";
import { TTeamPermission } from "@/modules/ee/teams/workspace-teams/types/team";
import { HowToSendCard } from "@/modules/survey/editor/components/how-to-send-card";
import { RecontactOptionsCard } from "@/modules/survey/editor/components/recontact-options-card";
import { ResponseOptionsCard } from "@/modules/survey/editor/components/response-options-card";
import { SurveyPlacementCard } from "@/modules/survey/editor/components/survey-placement-card";
import { TargetingLockedCard } from "@/modules/survey/editor/components/targeting-locked-card";
import { WhenToSendCard } from "@/modules/survey/editor/components/when-to-send-card";
import { type TSurveySchedulingConfig } from "@/modules/survey/scheduling/lib/config";
import type { TSharedSegmentEdit } from "../lib/shared-segment-edit";

interface SettingsViewProps {
  localSurvey: TSurvey;
  setLocalSurvey: Dispatch<SetStateAction<TSurvey>>;
  actionClasses: ActionClass[];
  contactAttributeKeys: TContactAttributeKey[];
  segments: TSegment[];
  responseCount: number;
  finishedResponseCount: number;
  membershipRole?: OrganizationRole;
  isUserTargetingAllowed?: boolean;
  isSpamProtectionAllowed: boolean;
  workspacePermission: TTeamPermission | null;
  isFormbricksCloud: boolean;
  isQuotasAllowed: boolean;
  quotas: TSurveyQuota[];
  surveySchedulingConfig: TSurveySchedulingConfig;
  locale: TUserLocale;
  appSetupCompleted: boolean;
  enterpriseLicenseRequestFormUrl: string;
  /** A save or publish was blocked because the survey has no trigger (ENG-2581). */
  hasTriggerError?: boolean;
  /** Null when the organisation isn't entitled to data retention. */
  dataRetention?: TSurveyDataRetentionContext;
  /** The creatives that open this survey (the editor reads them once for every card). */
  linkedCreatives: TSurveyLinkedCreative[];
  /** The shared-segment edit open in the targeting card (ENG-6957). */
  sharedSegmentEdit: TSharedSegmentEdit | null;
  setSharedSegmentEdit: Dispatch<SetStateAction<TSharedSegmentEdit | null>>;
}

export const SettingsView = ({
  localSurvey,
  setLocalSurvey,
  actionClasses,
  contactAttributeKeys,
  segments,
  responseCount,
  finishedResponseCount,
  membershipRole,
  isUserTargetingAllowed = false,
  isSpamProtectionAllowed,
  isQuotasAllowed,
  workspacePermission,
  isFormbricksCloud,
  quotas,
  surveySchedulingConfig,
  locale,
  appSetupCompleted,
  enterpriseLicenseRequestFormUrl,
  hasTriggerError = false,
  dataRetention = null,
  linkedCreatives,
  sharedSegmentEdit,
  setSharedSegmentEdit,
}: Readonly<SettingsViewProps>) => {
  const isAppSurvey = localSurvey.type === "app";

  return (
    <div className="mt-12 space-y-3 p-5">
      <HowToSendCard
        localSurvey={localSurvey}
        setLocalSurvey={setLocalSurvey}
        appSetupCompleted={appSetupCompleted}
      />

      {localSurvey.type === "app" ? (
        <div>
          {isUserTargetingAllowed ? (
            <div className="relative">
              <div className="blur-none">
                <TargetingCard
                  key={localSurvey.segment?.id}
                  localSurvey={localSurvey}
                  setLocalSurvey={setLocalSurvey}
                  contactAttributeKeys={contactAttributeKeys}
                  segments={segments}
                  initialSegment={segments.find((segment) => segment.id === localSurvey.segment?.id)}
                  sharedSegmentEdit={sharedSegmentEdit}
                  setSharedSegmentEdit={setSharedSegmentEdit}
                />
              </div>
            </div>
          ) : (
            <TargetingLockedCard
              isFormbricksCloud={isFormbricksCloud}
              enterpriseLicenseRequestFormUrl={enterpriseLicenseRequestFormUrl}
            />
          )}
        </div>
      ) : null}

      <WhenToSendCard
        localSurvey={localSurvey}
        setLocalSurvey={setLocalSurvey}
        workspaceId={localSurvey.workspaceId}
        propActionClasses={actionClasses}
        membershipRole={membershipRole}
        workspacePermission={workspacePermission}
        hasError={hasTriggerError}
        openedByCreatives={linkedCreatives}
      />
      <QuotasCard
        localSurvey={localSurvey}
        isQuotasAllowed={isQuotasAllowed}
        isFormbricksCloud={isFormbricksCloud}
        quotas={quotas}
        hasResponses={responseCount > 0}
        enterpriseLicenseRequestFormUrl={enterpriseLicenseRequestFormUrl}
      />

      <ResponseOptionsCard
        localSurvey={localSurvey}
        setLocalSurvey={setLocalSurvey}
        finishedResponseCount={finishedResponseCount}
        isSpamProtectionAllowed={isSpamProtectionAllowed}
        surveySchedulingConfig={surveySchedulingConfig}
        locale={locale}
        linkedCreatives={linkedCreatives}
      />

      <RecontactOptionsCard
        localSurvey={localSurvey}
        setLocalSurvey={setLocalSurvey}
        creativeOpenedMode={getCreativeOpenedMode({
          triggers: localSurvey.triggers,
          creatives: linkedCreatives,
        })}
      />

      {dataRetention ? (
        <SurveyRetentionCard
          surveyId={localSurvey.id}
          surveyName={localSurvey.name}
          context={dataRetention}
        />
      ) : null}

      {isAppSurvey && <SurveyPlacementCard localSurvey={localSurvey} setLocalSurvey={setLocalSurvey} />}
    </div>
  );
};
