"use server"
import { prisma } from "./index"
import { users } from "@prisma/client"

export async function createOrUpdateUser(
  user_id: string,
  github_username: string,
  email: string | null,
  name: string | null,
): Promise<users> {
  return prisma.users.upsert({
    where: { user_id },
    update: { email, github_username, name },
    create: { user_id, email, github_username, name },
  })
}

export async function hasCompletedOnboarding(user_id: string): Promise<boolean> {
  const user = await prisma.users.findUnique({
    where: { user_id },
    select: { onboarding_completed: true },
  })
  return user?.onboarding_completed ?? false
}

export async function markUserCompletedOnboarding(user_id: string): Promise<void> {
  await prisma.users.update({
    where: { user_id },
    data: { onboarding_completed: true },
  })
}

export async function submitOnboardingQuestions(user_id: string, email: string): Promise<void> {
  await prisma.users.update({
    where: { user_id },
    data: { email, onboarding_completed: true },
  })
}
