import "server-only";
import { prisma } from "@/lib/prisma";

/**
 * FETCH: Get user by Email
 * Used for authorization checks in actions.
 */
export async function getUserByEmail(email: string) {
  return await prisma.user.findUnique({
    where: { email },
    select: { id: true, email: true, role: true }
  });
}

/**
 * FETCH: Get user by ID
 */
export async function getUserById(id: string) {
  return await prisma.user.findUnique({
    where: { id },
    select: { id: true, email: true, role: true }
  });
}

/**
 * UPDATE: Update user role
 */
export async function updateUserRole(id: string, role: "ADMIN" | "USER") {
  return await prisma.user.update({
    where: { id },
    data: { role }
  });
}

/**
 * FETCH: List all users (for the settings table)
 */
export async function getAllUsers() {
  return await prisma.user.findMany({
    orderBy: { email: 'asc' }
  });
}