import { redirect } from "next/navigation";
import { createClient } from "@/lib/supabase/server";
import { ProfileForm } from "./profile-form";

export default async function ProfilePage() {
  const supabase = await createClient();
  const {
    data: { user },
  } = await supabase.auth.getUser();

  if (!user) {
    redirect("/login");
  }

  const { data: profile } = await supabase
    .from("profiles")
    .select("pseudo, avatar_url, pseudo_locked, role")
    .eq("id", user.id)
    .single();

  return (
    <div className="mx-auto w-full max-w-sm px-4 py-8">
      <h1 className="mb-6 text-2xl font-semibold text-navy">Mon profil</h1>
      <div className="rounded-xl border border-navy/10 bg-white p-6 shadow-sm sm:p-8">
        <p className="mb-4 text-sm text-ink/60">{user.email}</p>
        <ProfileForm
          userId={user.id}
          isJudge={profile?.role === "judge"}
          initialPseudo={profile?.pseudo ?? ""}
          initialAvatarUrl={profile?.avatar_url ?? null}
          initialPseudoLocked={profile?.pseudo_locked ?? false}
        />
      </div>
    </div>
  );
}
