import { createClient } from "@/utils/supabase/server";
import { SignInFormData } from "../models/auth/sign-in.model";

export class AuthRepository {
  async signIn({ email }: SignInFormData) {
    const supabase = await createClient();
    const { data, error } = await supabase.auth.signInWithOtp({
      email,
      options: {
        emailRedirectTo: `${window.location.origin}/auth/callback`,
      },
    });

    if (error) {
      throw new Error(error.message);
    }

    return data;
  }

  async signOut() {
    const supabase = await createClient();

    const { error } = await supabase.auth.signOut();
    if (error) {
      throw new Error(error.message);
    }
  }

  async getUser() {
    const supabase = await createClient();
    const {
      data: { user },
    } = await supabase.auth.getUser();
    return user;
  }
}

export const authRepository = new AuthRepository();
