"use client";

import { useEffect, useRef } from "react";
import { useTranslation } from "react-i18next";
import {
  NAV_STATE_KEYS,
  useConsumedNavState,
  type PostingOutcome,
} from "@/lib/navigationState";
import Title from "@/components/shared/Title";
import { HomepageBannerClient } from "@/features/home";

export default function EventPostThankyou() {
  const { t } = useTranslation();
  const outcome = useConsumedNavState<PostingOutcome>(NAV_STATE_KEYS.postingOutcome);
  const published = outcome?.published === true;
  const contentRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const timer = setTimeout(() => {
      if (contentRef.current) {
        const offset = 300; // Adjust based on your header height
        const elementPosition =
          contentRef.current.getBoundingClientRect().top + window.scrollY;
        window.scrollTo({ top: elementPosition - offset, behavior: "smooth" });
      }
    }, 100); // Small delay for content rendering

    return () => clearTimeout(timer);
  }, []);

  return (
    <>
      <HomepageBannerClient />
      <div>
        <div className="2xl:w-[880px] laptopmain:w-[1300px] laptop:w-[1359px] lg:w-[90%] 2xl:pb-[70px] pb-[40px] w-[90%] mx-auto pt-0">
          <h2
            ref={contentRef}
            className="flex justify-center mobilescreen:justify-center"
          >
            <Title text={t("COMMON.THANK_YOU")} variant="default" />
          </h2>
          {/* The admin can turn content approval off, in which case the
              item is already live and "will be reviewed" is untrue. Nothing
              stashed (a reload, a direct visit) keeps the review wording. */}
          <div className="text-start">
            {(published
              ? ["COMMON.EVENT.POSTING.PUBLISHED.TITLE1", "COMMON.POSTING.PUBLISHED.FOLLOW_UP"]
              : outcome === null
              ? [] // first render, before the stash is read — avoids a flash of the other wording
              : ["COMMON.EVENT.POSTING.THANKYOU.TITLE1", "COMMON.EVENT.POSTING.THANKYOU.TITLE2", "COMMON.EVENT.POSTING.THANKYOU.TITLE3"]
            ).map((key) => (
              <p key={key} className="text-primary-5 text-lg mobilescreen:text-base 2xl:pb-7 pb-4">
                {t(key)}
              </p>
            ))}
          </div>

          <div className="font-bold text-primary-5 text-[25px] 2xl:pt-8 pt-4 mobilescreen:pt-6">
            {t("COMMON.FORSA_TEAM")}
          </div>
        </div>
      </div>
    </>
  );
}
