"use client";

import { useEffect } from "react";
import { useRouter, useParams } from "next/navigation";
import { useAuth } from "../../auth-context";

export default function SignupWithCodePage() {
  const params = useParams();
  const router = useRouter();
  const { user, isLoading } = useAuth();
  const code = params.code as string;

  useEffect(() => {
    // Wait for auth check to complete before redirecting
    if (isLoading) return;
    
    if (user) {
      // Already logged in, redirect to home
      router.replace("/");
    } else {
      // Redirect to signup with code as query param
      // Invite codes only contain [a-z0-9-] so no encoding needed
      router.replace(`/signup?code=${code}`);
    }
  }, [user, isLoading, code, router]);

  return (
    <main style={{ 
      minHeight: "100vh", 
      display: "flex", 
      alignItems: "center", 
      justifyContent: "center",
      background: "linear-gradient(135deg, #0f0f23 0%, #1a1a3e 50%, #0f0f23 100%)",
      color: "rgba(255,255,255,0.6)",
      fontFamily: "'DM Sans', system-ui, sans-serif",
    }}>
      Redirecting...
    </main>
  );
}
