"use client"

import { useMutation, useQuery } from "@tanstack/react-query"
import { apiClient } from "@/lib/api/client"
import { useAuthReady } from "@/lib/api/use-auth-ready"

export type MeetConfig = {
  enabled: boolean
  public_url: string
  brand_name: string
  transcription_enabled?: boolean
  transcription_mode?: "live" | "queued"
  auto_start_transcription?: boolean
}

export type MeetRoomToken = {
  token: string
  room: string
  domain: string
  meet_url: string
}

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

  return useQuery({
    queryKey: ["meet", "config"],
    queryFn: () => apiClient.get<MeetConfig>("/meet/config"),
    staleTime: 60_000,
    enabled: ready && authenticated,
    retry: 1,
  })
}

export function useCreateMeetRoom() {
  return useMutation({
    mutationFn: (input?: { name?: string }) =>
      apiClient.post<MeetRoomToken>("/meet/rooms", input?.name ? { name: input.name } : {}),
  })
}

export function useMeetRoomToken() {
  return useMutation({
    mutationFn: (roomID: string) =>
      apiClient.post<MeetRoomToken>(`/meet/rooms/${encodeURIComponent(roomID)}/token`, {}),
  })
}
