"use client"

import { useQuery } from "@tanstack/react-query"
import { apiClient } from "@/lib/api/client"
import type { AdminUserRole } from "@/lib/api/admin-types"
import type { AgendaOrgAgendaPublic } from "@/lib/agenda/agenda-settings-types"
import type { DriveOrgDrivePublic } from "@/lib/api/types"
import { useAuthReady } from "@/lib/api/use-auth-ready"

export type CurrentUser = {
  sub: string
  email: string
  name: string
  status: string
  platform_admin: boolean
  role: AdminUserRole
  groups?: string[]
  avatar_url?: string
  org_agenda?: AgendaOrgAgendaPublic | null
  org_drive?: DriveOrgDrivePublic | null
}

export function useCurrentUser() {
  const { ready, authenticated } = useAuthReady()

  return useQuery({
    queryKey: ["current-user"],
    queryFn: () => apiClient.get<CurrentUser>("/users/me"),
    staleTime: 60_000,
    enabled: ready && authenticated,
    retry: 1,
  })
}
