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 { ESourceAuthorization } from "@/api/resources/User";
import { useSession } from "@/hooks/session/useSession";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";

function ReadingExperience() {
  const { translate } = useTranslations(ELocalesPlacement.Chats);
  const { updateSession } = useSession();
  useUpdateStep(23);
  const navigate = useNavigate();
  const answer = useSelector(selectors.selectAnswers)?.readingExperience;

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

  const answers: IAnswer[] = [
    {
      id: 1,
      value: "yes",
      name: translate("yes"),
      questionId: "readingExperience",
    },
    {
      id: 2,
      value: "no",
      name: translate("no"),
      questionId: "readingExperience",
    },
  ];

  const descriptions: Record<string, IAnswerDescriptionProps> = {
    yes: {
      title: translate("/quiz/readingExperience.description_title_1"),
      description: translate("/quiz/readingExperience.description_text_1"),
    },
    no: {
      title: translate("/quiz/readingExperience.description_title_2"),
      description: translate("/quiz/readingExperience.description_text_2"),
    },
  };

  return (
    <>
      <h2 className={`chats-answers-title ${styles.title}`}>
        {translate("/quiz/readingExperience.title")}
      </h2>
      <Answers answers={answers} activeAnswer={answer} />
      {!!answer && (
        <AnswerDescription
          title={descriptions[answer]?.title}
          description={descriptions[answer]?.description}
          footer={
            <>
              {translate("/quiz/readingExperience.free_sessions", {
                strong: (
                  <strong>{translate("/quiz/readingExperience.free")}</strong>
                ),
              })}
            </>
          }
        />
      )}
      {!!answer && (
        <Button classNameContainer={styles.button} onClick={handleNext}>
          {translate("continue")}
        </Button>
      )}
    </>
  );
}

export default ReadingExperience;
