"use server";

import { SignInFormData } from "@/src/models/auth/sign-in.model";
import { authRepository } from "@/src/repositories/authentication.repository";

export async function signIn(
  data: SignInFormData
): Promise<{ success: boolean; message: string }> {
  try {
    await authRepository.signIn(data);
    return { success: true, message: "Check your email for the login link!" };
  } catch (error) {
    console.error("Authentication error:", error);
    return {
      success: false,
      message:
        error instanceof Error
          ? error.message
          : "Authentication failed. Please try again.",
    };
  }
}

export async function signOut(): Promise<{
  success: boolean;
  message: string;
}> {
  try {
    await authRepository.signOut();
    return { success: true, message: "You have been signed out." };
  } catch (error) {
    console.error("Sign out error:", error);
    return {
      success: false,
      message:
        error instanceof Error
          ? error.message
          : "Sign out failed. Please try again.",
    };
  }
}

export async function getUser() {
  try {
    const user = await authRepository.getUser();
    return { success: true, user };
  } catch (error) {
    console.error("Get user error:", error);
    return {
      success: false,
      message:
        error instanceof Error
          ? error.message
          : "Failed to get user information.",
    };
  }
}
