import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { api } from './client';
import type { VendorTenantSummary, VendorTenantDetail, CreateTenantRequest, TenantResponse, LicenseResponse, AuditLogPage, AuditLogFilters } from '../types/api';

export function useVendorTenants() {
  return useQuery<VendorTenantSummary[]>({
    queryKey: ['vendor', 'tenants'],
    queryFn: () => api.get('/vendor/tenants'),
    refetchInterval: 30_000,
  });
}

export function useVendorTenant(
  id: string | null,
  options?: { refetchInterval?: number | false | ((query: { state: { data: VendorTenantDetail | undefined } }) => number | false) },
) {
  return useQuery<VendorTenantDetail>({
    queryKey: ['vendor', 'tenants', id],
    queryFn: () => api.get(`/vendor/tenants/${id}`),
    enabled: !!id,
    refetchInterval: options?.refetchInterval as any,
  });
}

export function useCreateTenant() {
  const qc = useQueryClient();
  return useMutation<TenantResponse, Error, CreateTenantRequest>({
    mutationFn: (req) => api.post('/vendor/tenants', req),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['vendor', 'tenants'] }),
  });
}

export function useSuspendTenant() {
  const qc = useQueryClient();
  return useMutation<TenantResponse, Error, string>({
    mutationFn: (id) => api.post(`/vendor/tenants/${id}/suspend`),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['vendor', 'tenants'] }),
  });
}

export function useActivateTenant() {
  const qc = useQueryClient();
  return useMutation<TenantResponse, Error, string>({
    mutationFn: (id) => api.post(`/vendor/tenants/${id}/activate`),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['vendor', 'tenants'] }),
  });
}

export function useDeleteTenant() {
  const qc = useQueryClient();
  return useMutation<void, Error, string>({
    mutationFn: (id) => api.delete(`/vendor/tenants/${id}`),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['vendor', 'tenants'] }),
  });
}

export function useRestartServer() {
  const qc = useQueryClient();
  return useMutation<void, Error, string>({
    mutationFn: (id) => api.post(`/vendor/tenants/${id}/restart`),
    onSuccess: (_, id) => qc.invalidateQueries({ queryKey: ['vendor', 'tenants', id] }),
  });
}

export function useRenewLicense() {
  const qc = useQueryClient();
  return useMutation<LicenseResponse, Error, string>({
    mutationFn: (tenantId) => api.post(`/vendor/tenants/${tenantId}/license`),
    onSuccess: (_, tenantId) => qc.invalidateQueries({ queryKey: ['vendor', 'tenants', tenantId] }),
  });
}

export function useVendorAuditLog(filters: AuditLogFilters) {
  const params = new URLSearchParams();
  if (filters.tenantId) params.set('tenantId', filters.tenantId);
  if (filters.action) params.set('action', filters.action);
  if (filters.result) params.set('result', filters.result);
  if (filters.search) params.set('search', filters.search);
  if (filters.from) params.set('from', filters.from);
  if (filters.to) params.set('to', filters.to);
  params.set('page', String(filters.page ?? 0));
  params.set('size', String(filters.size ?? 25));

  return useQuery<AuditLogPage>({
    queryKey: ['vendor', 'audit', filters],
    queryFn: () => api.get(`/vendor/audit?${params.toString()}`),
  });
}
