import { createClient } from "@/utils/supabase/server";
import { redirect } from "next/navigation";

export const verifyOtpAction = async (formData: FormData) => {
  const email = formData.get("email") as string;
  const token = formData.get("token") as string;
  const supabase = await createClient();

  console.log("email", email);
  console.log("token", token);

  if (!email || !token) {
    redirect("/error?message=Email and OTP are required");
  }

  const {
    data: { session },
    error,
  } = await supabase.auth.verifyOtp({
    email,
    token,
    type: "email",
  });

  if (error) {
    return redirect(`/verify-otp?error=${encodeURIComponent(error.message)}`);
  }

  return redirect("/protected/dashboard?message=OTP verified successfully");
};
