"use client";

import { useEffect, useMemo, useState } from "react";
import { IoClose } from "react-icons/io5";
import Button from "@/components/Componentes/button";
import InformationSheet from "@/components/Componentes/information-sheet";
import type { FormOverviewSection } from "@/hooks/marriage/use-form-schema";

const bookingTerms = [
  "All provided information is held in strict confidence.",
  "Data is utilized exclusively to ensure optimal matchmaking accuracy.",
];

const FIRST_ENTRY_TERMS_SEEN_KEY = "marriage:first-entry-terms-seen";

const FIRST_ENTRY_TERMS = [
  ...bookingTerms,
  "Do you struggle to understand the underlying psychological drivers that dictate your interpersonal connections and communication barriers?",
  "Taking this test is not mandatory, but it will help you better search for a spouse. The Kettle test is a test for self-knowledge and better understanding of your spouse.",
  'Do you operate based on superficial behavioral adaptations, or are you aware of the deep "source traits" that fundamentally control your decision-making processes?',
  "Do you struggle to understand the underlying psychological drivers that dictate your interpersonal connections and communication barriers?",
  "Taking this test is not mandatory, but it will help you better search for a spouse. The Kettle test is a test for self-knowledge and better understanding of your spouse.",
  'Do you operate based on superficial behavioral adaptations, or are you aware of the deep "source traits" that fundamentally control your decision-making processes?',
] as const;

export default function SectionsRequest({
  sections,
}: {
  sections: FormOverviewSection[] | undefined;
}) {
  const [hasSeenSheet, setHasSeenSheet] = useState(true);

  const hasNoProgression = useMemo(() => {
    if (!sections?.length) {
      return false;
    }

    return sections.every(
      (section) =>
        section.progress.current_step <= 0 &&
        section.progress.completion_percent <= 0,
    );
  }, [sections]);

  const isOpen = Boolean(sections) && hasNoProgression && !hasSeenSheet;

  useEffect(() => {
    try {
      const seenLocal =
        window.localStorage.getItem(FIRST_ENTRY_TERMS_SEEN_KEY) === "true";
      const seenSession =
        window.sessionStorage.getItem(FIRST_ENTRY_TERMS_SEEN_KEY) === "true";
      setHasSeenSheet(seenLocal || seenSession);
    } catch (e) {
      console.warn("Storage is not accessible:", e);
      setHasSeenSheet(false);
    }
  }, []);

  useEffect(() => {
    if (!isOpen) {
      return;
    }

    try {
      window.localStorage.setItem(FIRST_ENTRY_TERMS_SEEN_KEY, "true");
      window.sessionStorage.setItem(FIRST_ENTRY_TERMS_SEEN_KEY, "true");
    } catch (e) {
      console.warn("Storage is not accessible:", e);
    }
  }, [isOpen]);

  if (!isOpen) {
    return null;
  }

  return (
    <InformationSheet
      icon={null}
      title={({ close }) => (
        <span className="flex w-full items-start justify-between gap-3 text-left">
          <span className="text-[14px] leading-5 font-bold tracking-normal text-[#8B8B8B]">
            Terms &amp; Conditions
          </span>
          <button
            type="button"
            aria-label="Close terms and conditions"
            className="-mt-0.5 flex size-6 shrink-0 items-center justify-center text-[#8F8F8F]"
            onClick={close}
          >
            <IoClose aria-hidden="true" className="text-[22px]" />
          </button>
        </span>
      )}
      description={
        <ul className="max-h-[60dvh] space-y-2 overflow-y-auto pl-4 text-left text-[12px] leading-[1.35] font-medium list-disc text-[#4C4C4C] marker:text-[#2B2B2B]">
          {FIRST_ENTRY_TERMS.map((item, index) => (
            <li key={`${index}-${item}`}>{item}</li>
          ))}
        </ul>
      }
      buttons={({ close }) => (
        <Button className="rounded-[8px]" onClick={close}>
          Got it
        </Button>
      )}
      className="text-left"
    />
  );
}
