import styles from "./styles.module.scss";
import { useNavigate } from "react-router-dom";
import { useUpdateStep } from "@/components/ChatsPath/hooks/useUpdateStep";
import routes from "@/routes";
import { IAnswer } from "@/components/ChatsPath/data";
import Answers from "@/components/ChatsPath/components/Answers";
import AnswerDescription, {
  IAnswerDescriptionProps,
} from "@/components/ChatsPath/components/AnswerDescription";
import { useSelector } from "react-redux";
import { selectors } from "@/store";
import Button from "@/components/ChatsPath/ui/Button";
import { useSession } from "@/hooks/session/useSession";
import { ESourceAuthorization } from "@/api/resources/User";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";

function DecisionGuidance() {
  const { translate } = useTranslations(ELocalesPlacement.Chats);
  const { updateSession } = useSession();
  useUpdateStep(18);
  const navigate = useNavigate();
  const answer = useSelector(selectors.selectAnswers)?.decisionGuidance;

  const handleNext = async () => {
    const _answer = answers.find((item) => item.value === answer);
    if (_answer) {
      await updateSession(
        {
          answers: {
            decisionGuidance: _answer.name as string,
          },
        },
        ESourceAuthorization["aura.chats"]
      );
    }
    navigate(routes.client.chatsQuizResonateLove());
  };

  const answers: IAnswer[] = [
    {
      id: 1,
      value: "yes",
      name: translate("yes"),
      questionId: "decisionGuidance",
    },
    {
      id: 2,
      value: "sometimes",
      name: translate("sometimes"),
      questionId: "decisionGuidance",
    },
    {
      id: 3,
      value: "not_sure",
      name: translate("not_sure"),
      questionId: "decisionGuidance",
    },
  ];

  const descriptions: Record<string, IAnswerDescriptionProps> = {
    yes: {
      title: translate("/quiz/decisionGuidance.description_title_1"),
      description: translate("/quiz/decisionGuidance.description_text_1"),
    },
    sometimes: {
      title: translate("/quiz/decisionGuidance.description_title_2"),
      description: translate("/quiz/decisionGuidance.description_text_2"),
    },
    not_sure: {
      title: translate("/quiz/decisionGuidance.description_title_3"),
      description: translate("/quiz/decisionGuidance.description_text_3"),
    },
  };

  return (
    <>
      <h2 className={`chats-answers-title ${styles.title}`}>
        {translate("/quiz/decisionGuidance.title")}
      </h2>
      <Answers activeAnswer={answer} answers={answers} />
      {!!answer && (
        <AnswerDescription
          title={descriptions[answer]?.title}
          description={descriptions[answer]?.description}
        />
      )}
      {!!answer && (
        <Button classNameContainer={styles.button} onClick={handleNext}>
          Continue
        </Button>
      )}
    </>
  );
}

export default DecisionGuidance;
