"use server";
import { eq, not , asc} from "drizzle-orm";
import { revalidatePath } from "next/cache";
import { db } from "@db/index";
import { usersMerged } from "@schemas/schema";

export const getData = async () => {
  const data = await db.select().from(usersMerged).orderBy(asc(usersMerged.email));
  return data;
};

export const addUser = async ( first_name:  string, last_name: string, username: string,  email: string, password_hash : string) => {
   await db.insert(usersMerged).values({
    first_name : first_name, last_name: last_name, username: username, email: email, password_hash : password_hash 
  }); 
};

export const deleteUser = async (id: string) => {
  await db.delete(usersMerged).where(eq(usersMerged.id, id));
  revalidatePath("/");
};

export const editUser = async (id: string, first_name: string, last_name: string, username: string, email : string, password_hash: string) => {
  await db
    .update(usersMerged)
    .set({
      first_name : first_name, 
      last_name: last_name, 
      username: username,
      email: email, 
      password_hash: password_hash
    })
    .where(eq(usersMerged.id, id));
  revalidatePath("/");
};