import client from "./client";

export interface UserOut {
  id: string;
  username: string;
  email: string | null;
  role: string;
  is_active: boolean;
  created_at: string | null;
  last_login: string | null;
}

export interface UserCreatePayload {
  username: string;
  email?: string;
  password: string;
  role: string;
}

export interface UserUpdatePayload {
  email?: string;
  role?: string;
  is_active?: boolean;
  password?: string;
}

/** Fetch all users (admin only). */
export async function getUsers(): Promise<UserOut[]> {
  const { data } = await client.get<UserOut[]>("/users");
  return data;
}

/** Create a new user (admin only). */
export async function createUser(payload: UserCreatePayload): Promise<UserOut> {
  const { data } = await client.post<UserOut>("/users", payload);
  return data;
}

/** Update a user (admin only). */
export async function updateUser(userId: string, payload: UserUpdatePayload): Promise<UserOut> {
  const { data } = await client.patch<UserOut>(`/users/${userId}`, payload);
  return data;
}
