"use client";
import Image from "next/image";
import { useRouter } from "next/navigation";
import { useMemo, useState } from "react";
import { IoClose } from "react-icons/io5";
import QuestionCard from "@/components/questions/question-card";
import RequiredStepsCard from "@/components/questions/required-steps-card";
import Button from "@/components/ui/button";
import InformationSheet from "@/components/ui/information-sheet";
import NavigationButton from "@/components/ui/navigation-button";
import { PageBackground } from "@/components/utils/page-background";
import {
getQuestionListItems,
isQuestionListItemVisibleForProfile,
isQuestionRequiredForProfile,
isQuestionVisibleForProfile,
type QuestionListItem,
} from "@/data/question-data";
import { hasQuestionAnswerValue } from "@/components/questions/question-answer-storage";
import { useStartMarriageMatchMutation } from "@/hooks/marriage/use-match-start";
import { useMarriageProfileQuery } from "@/hooks/marriage/use-profile-main";
import { useMarriageSectionsQuery } from "@/hooks/marriage/use-sections";
import { localizePath } from "@/i18n/config";
import { useI18n } from "@/i18n/provider";
import { toFrontendSlug } from "@/data/section-slug-map";
import SectionsRequest from "./sections-request";
import { getStoredAge, getLocalSectionProgress } from "@/components/questions/progress-helper";
export default function QuestionsListPage() {
const { dictionary: t, locale } = useI18n();
const router = useRouter();
const { data: profile } = useMarriageProfileQuery();
const { data: sections } = useMarriageSectionsQuery({
refetchOnMount: "always",
});
const startMatchMutation = useStartMarriageMatchMutation({
onSuccess: () => {
router.push(localizePath("/finding-match", locale));
},
});
const [isOptionalInfoSheetOpen, setIsOptionalInfoSheetOpen] = useState(false);
const [selectedSection, setSelectedSection] =
useState(null);
const questionListItems = useMemo(
() =>
getQuestionListItems(locale).filter((item) =>
isQuestionListItemVisibleForProfile(item, {
gender: profile?.gender,
}),
),
[locale, profile?.gender],
);
const sectionProgressBySlug = useMemo(() => {
const progressBySlug = new Map();
const age = getStoredAge();
sections?.forEach((section) => {
const frontendSlug = toFrontendSlug(section.slug);
const progress = Math.max(0, Math.min(100, Math.round(section.completion_percent)));
progressBySlug.set(frontendSlug, progress);
progressBySlug.set(section.slug, progress);
});
questionListItems.forEach((item) => {
const localProgress = getLocalSectionProgress(item, profile, age);
if (localProgress !== null) {
progressBySlug.set(item.slug, localProgress);
}
});
return progressBySlug;
}, [sections, questionListItems, profile]);
const allRequiredSectionsCompleted = useMemo(() => {
if (!sections?.length) {
return false;
}
return sections
.filter((section) => section.is_required)
.every((section) => {
const frontendSlug = toFrontendSlug(section.slug);
const progress = sectionProgressBySlug.get(frontendSlug) ?? Math.max(0, Math.min(100, Math.round(section.completion_percent)));
return progress >= 100;
});
}, [sections, sectionProgressBySlug]);
const profileStatus = profile?.status;
const isProfileSuspended = profileStatus === "suspended";
const canStartMatch =
profileStatus === "pending_info" &&
!isProfileSuspended &&
allRequiredSectionsCompleted;
const isStartMatchDisabled = startMatchMutation.isPending || !canStartMatch;
const hasIncompleteOptionalSections = useMemo(() => {
if (!sections?.length) {
return false;
}
return sections.some(
(section) => !section.is_required && (sectionProgressBySlug.get(toFrontendSlug(section.slug)) ?? section.completion_percent) < 100,
);
}, [sections, sectionProgressBySlug]);
const handleStartMatch = () => {
if (!canStartMatch) {
return;
}
startMatchMutation.mutate();
};
return (
<>
{isOptionalInfoSheetOpen ? (
{t.questions.optionalInfoPromptDescription}
}
onClose={() => setIsOptionalInfoSheetOpen(false)}
buttons={({ close }) => (
)}
/>
) : null}
{selectedSection ? (
(
{selectedSection.title}
)}
description={
{selectedSection.summary}
}
onClose={() => setSelectedSection(null)}
className="text-left"
/>
) : null}
{t.questions.profileRegistration}
{questionListItems.map((item) => (
setSelectedSection(section)}
/>
))}
>
);
}