"use client";

import { useEffect, useState } from "react";
import Link from "next/link";
import { ArrowRight, Check } from "lucide-react";
import { createClient } from "@/lib/supabase/client";
import { getProfilePath } from "@/lib/constants";
import UsernameClaimForm from "@/app/UsernameClaimForm";

export default function LandingHeroContent({
  initialUser,
  initialUsername,
}: {
  initialUser?: { id: string } | null;
  initialUsername?: string;
}) {
  const [user, setUser] = useState(initialUser);
  const [username, setUsername] = useState(initialUsername || "");

  useEffect(() => {
    if (initialUser) {
      setUser(initialUser);
      if (initialUsername) setUsername(initialUsername);
      return;
    }

    // Hydrate from active browser Supabase session if server rendered logged-out
    const supabase = createClient();
    supabase.auth.getSession().then(async ({ data }) => {
      if (data.session?.user) {
        setUser({ id: data.session.user.id });
        const { data: prof } = await supabase
          .from("profiles")
          .select("username")
          .eq("id", data.session.user.id)
          .maybeSingle();
        if (prof?.username) {
          setUsername(prof.username);
        }
      }
    });
  }, [initialUser, initialUsername]);

  if (user) {
    if (username) {
      return (
        <>
          <h1>
            <span className="landingHeroAccent">@{username}</span> is live.
          </h1>
          <p className="landingLead">
            Open your dashboard to fine-tune your page, customize modules, or explore the community.
          </p>
          <div style={{ display: "flex", gap: "16px", marginTop: "28px" }}>
            <Link href="/dashboard" className="landingPrimaryBtn">
              Open dashboard
            </Link>
            <Link
              href={getProfilePath(username)}
              className="landingTextLink"
              style={{ fontSize: "13px" }}
            >
              View profile <ArrowRight size={14} />
            </Link>
          </div>
        </>
      );
    }

    return (
      <>
        <h1>Claim your handle.</h1>
        <p className="landingLead">
          Pick your unique name now — your profile goes live the moment you claim it.
        </p>
        <div style={{ marginTop: "28px" }}>
          <Link href="/claim" className="landingPrimaryBtn">
            Claim username
          </Link>
        </div>
      </>
    );
  }

  return (
    <>
      <h1>
        A little extra.
        <br />
        <span>A lot more you.</span>
      </h1>
      <p className="landingLead">
        Claim your handle, add your links, audio, video backgrounds, and live Discord presence, and share it anywhere. The aesthetic bio link platform, free forever.
      </p>
      <UsernameClaimForm />
      <div className="landingHeroTrust">
        <span>
          <Check size={13} /> Free forever
        </span>
        <span>
          <Check size={13} /> No card required
        </span>
        <span>
          <Check size={13} /> Live in seconds
        </span>
      </div>
    </>
  );
}
