import bcrypt from "bcryptjs";
import { userRepo } from "@/server/repositories/user.repo";

export const authService = {
  async register(data: {
    name: string;
    email: string;
    password: string;
    acceptedTermsAndPrivacy: boolean;
  }) {
    const existing = await userRepo.findByEmail(data.email);
    if (existing) {
      throw new Error("Email sudah terdaftar");
    }

    const hashedPassword = await bcrypt.hash(data.password, 12);

    const user = await userRepo.create({
      name: data.name,
      email: data.email,
      password: hashedPassword,
      acceptedTermsAndPrivacy: data.acceptedTermsAndPrivacy,
      acceptedAt: data.acceptedTermsAndPrivacy ? new Date() : null,
    });

    return { id: user.id, name: user.name, email: user.email };
  },
};
