import Title from "@/components/Title";
import styles from "./styles.module.scss";
import { DatePicker } from "@/components/DateTimePicker";
import { useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { actions, selectors } from "@/store";
import Button from "../../components/Button";
import routes from "@/routes";
import { useNavigate } from "react-router-dom";
import { useTranslations } from "@/hooks/translations";
import { ELocalesPlacement } from "@/locales";

function PotentialPartnerBirthdate() {
  const { translate } = useTranslations(ELocalesPlacement.CompatibilityV4);
  const navigate = useNavigate();
  const dispatch = useDispatch();
  const { potentialPartnerBirthdate: birthdateFromStore, whatAddToAnalysis, relationshipStatus } = useSelector(selectors.selectCompatibilityV4Answers);
  const isSingle = relationshipStatus === "single";
  const [birthdate, setBirthdate] = useState(birthdateFromStore || "");
  const [isDisabled, setIsDisabled] = useState(true);

  const handleValid = (_birthdate: string) => {
    setBirthdate(_birthdate);
    setIsDisabled(_birthdate === "");
  };

  const handleNext = () => {
    dispatch(actions.compatibilityV4Answers.update({ potentialPartnerBirthdate: birthdate }));
    if (whatAddToAnalysis.includes("former_partner") && !isSingle) {
      return navigate(routes.client.compatibilityV4FormerPartnerName());
    }
    navigate(routes.client.compatibilityV4Loading());
  };

  return (
    <div className={styles["page-container"]}>
      <Title variant="h2" className={styles.title}>
        {translate("/potential-partner-birthdate.title")}
      </Title>
      <p className={styles.description}>{translate("/potential-partner-birthdate.text")}</p>
      <DatePicker
        name="birthdate"
        value={birthdate}
        onValid={handleValid}
        onInvalid={() => setIsDisabled(true)}
        inputClassName="date-picker-input"
      />
      <Button
        className={styles.button}
        onClick={handleNext}
        disabled={isDisabled}
      >
        {translate("next")}
      </Button>
    </div>
  );
}

export default PotentialPartnerBirthdate;
