import { useQuery } from '@tanstack/react-query';
import { api } from './client';
import type {
  TenantResponse, LicenseResponse, MeResponse,
} from '../types/api';

// Tenant
export function useTenant(tenantId: string) {
  return useQuery({
    queryKey: ['tenant', tenantId],
    queryFn: () => api.get<TenantResponse>(`/tenants/${tenantId}`),
    enabled: !!tenantId,
  });
}

// License
export function useLicense(tenantId: string) {
  return useQuery({
    queryKey: ['license', tenantId],
    queryFn: () => api.get<LicenseResponse>(`/tenants/${tenantId}/license`),
    enabled: !!tenantId,
  });
}

// Platform
export function useMe() {
  return useQuery({
    queryKey: ['me'],
    queryFn: () => api.get<MeResponse>('/me'),
    staleTime: 60_000,
    retry: 3,
    retryDelay: 1000,
  });
}

export function useAllTenants() {
  return useQuery({
    queryKey: ['tenants', 'all'],
    queryFn: () => api.get<TenantResponse[]>('/tenants'),
  });
}
