import { redirect } from 'next/navigation';
import { getSignupsOpen } from './actions';
import SignupForm from './SignupForm';
import { getCurrentUser } from '@/lib/auth';

/**
 * Server component wrapper. Reads the InstanceSettings.signupsOpen flag
 * before rendering so closed instances don't even paint a form. Already-
 * authenticated users get bounced to the dashboard rather than seeing
 * sign-up.
 */
export default async function SignupPage() {
  const user = await getCurrentUser();
  if (user) {
    redirect('/main/dashboard');
  }

  const open = await getSignupsOpen();

  return (
    <div className="min-h-screen bg-[#0A0A0A] flex items-center justify-center px-4">
      <div className="w-full max-w-md">
        <div className="bg-zinc-900 rounded border border-zinc-800 shadow-2xl">
          <div className="flex flex-col space-y-2 p-8 text-center">
            <h1 className="text-3xl font-bold leading-none tracking-tight text-white">
              Proof of Work
            </h1>
            <p className="text-xs text-zinc-500 mt-2 uppercase tracking-widest">
              {open ? 'Create an account' : 'Sign-ups are closed'}
            </p>
          </div>

          <div className="p-8 pt-6">
            {open ? (
              <SignupForm />
            ) : (
              <div className="space-y-4">
                <p className="text-sm text-zinc-400">
                  This Proof of Work instance isn&apos;t accepting new
                  sign-ups right now. Ask the admin to enable sign-ups
                  from Settings (or via the StartOS Action) and try again.
                </p>
                <a
                  href="/auth/login"
                  className="block w-full text-center py-2.5 px-4 rounded border border-zinc-700 text-white font-bold text-sm uppercase tracking-wider hover:bg-zinc-800 transition-all"
                >
                  Back to sign in
                </a>
              </div>
            )}
          </div>
        </div>
      </div>
    </div>
  );
}
