import { useQuery, useMutation, useQueryClient } from '@tanstack/react-query';
import { api } from './client';
import type { DashboardData, TenantLicenseData, TenantSettings, AuditLogPage, AuditLogFilters, SsoConnector, CreateSsoConnectorRequest, SsoTestResult } from '../types/api';

export function useTenantDashboard() {
  return useQuery<DashboardData>({
    queryKey: ['tenant', 'dashboard'],
    queryFn: () => api.get('/tenant/dashboard'),
    refetchInterval: 30_000,
  });
}

export function useRestartServer() {
  const qc = useQueryClient();
  return useMutation<void, Error, void>({
    mutationFn: () => api.post('/tenant/server/restart'),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['tenant', 'dashboard'] }),
  });
}

export function useUpgradeServer() {
  const qc = useQueryClient();
  return useMutation<void, Error, void>({
    mutationFn: () => api.post('/tenant/server/upgrade'),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['tenant', 'dashboard'] }),
  });
}

export function useTenantLicense() {
  return useQuery<TenantLicenseData>({
    queryKey: ['tenant', 'license'],
    queryFn: () => api.get('/tenant/license'),
  });
}

// SSO connector hooks
export function useSsoConnectors() {
  return useQuery<SsoConnector[]>({
    queryKey: ['tenant', 'sso'],
    queryFn: () => api.get('/tenant/sso'),
  });
}

export function useSsoConnector(id: string | null) {
  return useQuery<SsoConnector>({
    queryKey: ['tenant', 'sso', id],
    queryFn: () => api.get(`/tenant/sso/${id}`),
    enabled: !!id,
  });
}

export function useCreateSsoConnector() {
  const qc = useQueryClient();
  return useMutation<SsoConnector, Error, CreateSsoConnectorRequest>({
    mutationFn: (req) => api.post('/tenant/sso', req),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['tenant', 'sso'] }),
  });
}

export function useUpdateSsoConnector() {
  const qc = useQueryClient();
  return useMutation<SsoConnector, Error, { id: string; updates: Record<string, unknown> }>({
    mutationFn: ({ id, updates }) => api.patch(`/tenant/sso/${id}`, updates),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['tenant', 'sso'] }),
  });
}

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

export function useTestSsoConnector() {
  return useMutation<SsoTestResult, Error, string>({
    mutationFn: (id) => api.post(`/tenant/sso/${id}/test`),
  });
}

export function useTenantTeam() {
  return useQuery<Array<Record<string, unknown>>>({
    queryKey: ['tenant', 'team'],
    queryFn: () => api.get('/tenant/team'),
  });
}

export function useInviteTeamMember() {
  const qc = useQueryClient();
  return useMutation<{ userId: string }, Error, { email: string; roleId: string }>({
    mutationFn: (body) => api.post('/tenant/team/invite', body),
    onSuccess: () => qc.invalidateQueries({ queryKey: ['tenant', 'team'] }),
  });
}

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

export function useResetServerAdminPassword() {
  return useMutation<void, Error, string>({
    mutationFn: (password) => api.post('/tenant/server/admin-password', { password }),
  });
}

export function useChangeOwnPassword() {
  return useMutation<void, Error, string>({
    mutationFn: (password) => api.post('/tenant/password', { password }),
  });
}

export function useResetTeamMemberPassword() {
  return useMutation<void, Error, { userId: string; password: string }>({
    mutationFn: ({ userId, password }) =>
      api.post(`/tenant/team/${userId}/password`, { password }),
  });
}

export function useTenantSettings() {
  return useQuery<TenantSettings>({
    queryKey: ['tenant', 'settings'],
    queryFn: () => api.get('/tenant/settings'),
  });
}

export function useTenantAuditLog(filters: Omit<AuditLogFilters, 'tenantId'>) {
  const params = new URLSearchParams();
  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: ['tenant', 'audit', filters],
    queryFn: () => api.get(`/tenant/audit?${params.toString()}`),
  });
}
