"use server"
import { getSession } from "@auth0/nextjs-auth0"
import { redirect } from "next/navigation"
import { deleteAPIKeyById, genAndStoreAPITokenHash } from "@codeflash-ai/common/token-functions"

export async function generateToken(keyName: string): Promise<string> {
  const user = await getSession()
  if (user == null) {
    redirect("/login")
  }
  const userId: string = user.user.sub

  const token: string = await genAndStoreAPITokenHash(keyName, userId)
  return token
}

export async function deleteAPIKey(id: number): Promise<void> {
  const user = await getSession()
  if (user == null) {
    redirect("/login")
    return
  }
  const userId: string = user.user.sub

  await deleteAPIKeyById(id, userId)
}
