"use client";

import { WelcomeBlock } from "./dashboard/WelcomeBlock";
import { UnlockedStats } from "./dashboard/UnlockedStats";
import { TopicEbookShowcase } from "./landing/TopicEbookShowcase";

type DashboardPageProps = {
  userName: string | null;
  userEmail?: string | null;
};

function getStorage(key: string): string | null {
  if (typeof window === "undefined") return null;
  return localStorage.getItem(key);
}

function requiresEmailBinding(): boolean {
  const userId = getStorage("userId");
  const emailLinked = getStorage("emailLinked") === "1";
  const userEmail = getStorage("userEmail");
  const userName = getStorage("userName");

  return (
    !!userId &&
    /^user\d+$/.test(userId) &&
    !emailLinked &&
    !userEmail &&
    !userName?.includes("@")
  );
}

export function DashboardPage({ userName, userEmail }: DashboardPageProps) {
  const bindingRequired = requiresEmailBinding();

  return (
    <>
      <WelcomeBlock
        userName={userName}
        userEmail={userEmail}
        bindingRequired={bindingRequired}
      />
      <UnlockedStats />
      <TopicEbookShowcase />
    </>
  );
}
