import { queryOptions } from "@tanstack/react-query";
import { jsonQuery } from "../apiAdapter";

export interface User {
  id: number;
  username: string;
  email: string;
  first_name: string;
  last_name: string;
  role_id: number;
  roles?: { id: number; name: string; display_name: string } | null;
  is_active: boolean;
}

export interface Role {
  id: number;
  name: string;
  display_name: string;
}

export const userListOptions = (permission?: string) =>
  queryOptions({
    queryKey: ["users", { permission }],
    queryFn: () => {
      const url = permission
        ? `/api/admin/users?permission=${encodeURIComponent(permission)}&limit=100`
        : "/api/admin/users?limit=100";
      return jsonQuery<User[]>(url);
    },
  });

export const roleListOptions = () =>
  queryOptions({
    queryKey: ["roles"],
    queryFn: () => jsonQuery<Role[]>("/api/admin/roles"),
    staleTime: 2 * 60_000,
  });
