import Title from "@/components/Title";
import styles from "./styles.module.scss";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";
import { IAnswersSessionCompatibilityV2 } from "@/api/resources/Session";
import { useMemo } from "react";
import Answer from "../../components/Answer";
import { useSelector } from "react-redux";
import { actions, selectors } from "@/store";
import { useDispatch } from "react-redux";
import { useSession } from "@/hooks/session/useSession";
import { ESourceAuthorization } from "@/api/resources/User";
import routes from "@/routes";
import { useNavigate } from "react-router-dom";
import { sleep } from "@/services/date";
import { answerTimeOut } from "../../data";

function FearInRelationship() {
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const { updateSession } = useSession();
  const { translate } = useTranslations(ELocalesPlacement.CompatibilityV2);
  const { fearInRelationship } = useSelector(
    selectors.selectCompatibilityV2Answers
  );

  const answers: {
    id: IAnswersSessionCompatibilityV2["fear_in_relationship"];
    title: string;
  }[] = useMemo(
    () => [
      {
        id: "wasting_life_for_wrong_person",
        title: translate("/fear-in-relationship.answer1"),
      },
      {
        id: "stay_single",
        title: translate("/fear-in-relationship.answer2"),
      },
      {
        id: "skip_destiny",
        title: translate("/fear-in-relationship.answer3"),
      },
      {
        id: "love_without_respect_and_support",
        title: translate("/fear-in-relationship.answer4"),
      },
      {
        id: "not_find_truth_about_compatibility",
        title: translate("/fear-in-relationship.answer5"),
      },
    ],
    [translate]
  );

  const handleClick = async (
    id: IAnswersSessionCompatibilityV2["fear_in_relationship"]
  ) => {
    dispatch(actions.compatibilityV2Answers.update({ fearInRelationship: id }));
    updateSession(
      {
        answers: {
          fear_in_relationship: id,
        },
      },
      ESourceAuthorization["aura.compatibility.v2"]
    );
    if (id !== fearInRelationship) await sleep(answerTimeOut);
    navigate(routes.client.compatibilityV2ImportantStep());
  };

  return (
    <div className={styles.container}>
      <Title variant="h2" className={styles.title}>
        {translate("/fear-in-relationship.title")}
      </Title>
      {answers.map((answers, index) => (
        <Answer
          key={index}
          answer={answers}
          isSelected={fearInRelationship === answers.id}
          onClick={() => handleClick(answers.id)}
        />
      ))}
    </div>
  );
}

export default FearInRelationship;
