import { supabaseAdmin } from "../lib/supabase/admin";

export async function createNewUser(formData: {
  email: string;
  password: string;
  client: string;
  subscription: string;
  company_type: string;
  CMS?: string | undefined;
  Country?: string | undefined;
  Language?: string | undefined;
  site?: string | undefined;
  funnel1?: string | undefined;
  funnel2?: string | undefined;
  funnel3?: string | undefined;
  TLD?: string | undefined;
}) {
  const { email, password } = formData;

  const { data, error } = await supabaseAdmin.auth.admin.createUser({
    email,
    password,
    email_confirm: true,
  });

  if (error) {
    return { status: 500, message: error.message };
  }

  if (data.user) {
    const { email, password, ...newFormData } = formData;

    const { data: client, error: error_client } = await supabaseAdmin
      .from("Client")
      .insert([{ ...newFormData, auth_id: data.user.id, role: "user" }])
      .select();

    if (error_client) {
      return { status: 500, message: error_client.message };
    }

    return {
      status: 200,
      message: `User ${data.user.email} - ${client[0].client} created successfully. They can now log in.`,
    };
  }

  return {};
}
// export async function getAuthorizationUrl() {
//   const oauth2Client = getOauthClient();

//   const scopes = ["https://www.googleapis.com/auth/webmasters"];

//   const url = oauth2Client.generateAuthUrl({
//     access_type: "offline",
//     scope: scopes,
//     prompt: "consent",
//   });

//   redirect(url);
// }
