"use client";

import { ActionClass } from "@prisma/client";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { TSurvey } from "@formbricks/types/surveys/types";
import { ACTION_TYPE_ICON_LOOKUP } from "@/modules/projects/settings/(setup)/app-connection/utils";
import { ActionClassInfo } from "@/modules/ui/components/action-class-info";
import { Input } from "@/modules/ui/components/input";

interface SavedActionsTabProps {
  actionClasses: ActionClass[];
  localSurvey: TSurvey;
  setLocalSurvey: React.Dispatch<React.SetStateAction<TSurvey>>;
  setOpen: React.Dispatch<React.SetStateAction<boolean>>;
}

export const SavedActionsTab = ({
  actionClasses,
  localSurvey,
  setLocalSurvey,
  setOpen,
}: SavedActionsTabProps) => {
  const { t } = useTranslation();
  const availableActions = actionClasses.filter(
    (actionClass) => !localSurvey.triggers.some((trigger) => trigger.actionClass.id === actionClass.id)
  );
  const [filteredActionClasses, setFilteredActionClasses] = useState<ActionClass[]>(availableActions);

  const codeActions = filteredActionClasses.filter((actionClass) => actionClass.type === "code");
  const noCodeActions = filteredActionClasses.filter((actionClass) => actionClass.type === "noCode");

  const handleActionClick = (action: ActionClass) => {
    setLocalSurvey((prev) => ({
      ...prev,
      triggers: prev.triggers.concat({ actionClass: action }),
    }));
    setOpen(false);
  };

  const allActions = [...noCodeActions, ...codeActions];

  return (
    <div>
      <Input
        type="text"
        onChange={(e) => {
          const searchTerm = e.target.value.toLowerCase();
          setFilteredActionClasses(
            availableActions.filter(
              (actionClass) =>
                actionClass.name.toLowerCase().includes(searchTerm) ||
                actionClass.description?.toLowerCase().includes(searchTerm) ||
                actionClass.key?.toLowerCase().includes(searchTerm)
            )
          );
        }}
        className="mb-2 bg-white"
        placeholder="Search actions"
        id="search-actions"
      />
      <div className="max-h-96 overflow-y-auto">
        {!allActions.length && (
          <div className="pt-4 text-center">
            <span className="text-sm text-slate-500">No saved actions found</span>
          </div>
        )}
        {[noCodeActions, codeActions].map(
          (actions, i) =>
            actions.length > 0 && (
              <div key={i} className="me-4">
                <h2 className="mb-2 mt-4 font-semibold">
                  {i === 0 ? t("common.no_code") : t("common.code")}
                </h2>
                <div className="flex flex-col gap-2">
                  {actions.map((action) => (
                    <button
                      key={action.id}
                      className="flex cursor-pointer flex-col items-start rounded-md border border-slate-300 bg-white px-4 py-2 hover:bg-slate-100"
                      onClick={() => handleActionClick(action)}>
                      <div className="mt-1 flex items-center">
                        <div className="mr-1.5 h-4 w-4 text-slate-600">
                          {ACTION_TYPE_ICON_LOOKUP[action.type]}
                        </div>
                        <h4 className="text-sm font-semibold text-slate-600">{action.name}</h4>
                      </div>
                      <ActionClassInfo actionClass={action} />
                    </button>
                  ))}
                </div>
              </div>
            )
        )}
      </div>
    </div>
  );
};
