/**
 * Audit Logs React Query hooks
 *
 * Provides React Query hooks for fetching and managing audit logs
 * across all microservices with caching and real-time updates.
 *
 * Last Updated: 2025-11-02
 * Status: ✅ Complete
 */

import { useQuery, UseQueryOptions } from '@tanstack/react-query';
import { auditLogsService } from '../services/auditLogs';
import {
  AuditLogResponse,
  AuditLogFilters,
  AuditLogListResponse,
  AuditLogStatsResponse,
  AggregatedAuditLog,
  AuditLogServiceName,
} from '../types/auditLogs';
import { ApiError } from '../client';

// Query Keys
export const auditLogKeys = {
  all: ['audit-logs'] as const,
  lists: () => [...auditLogKeys.all, 'list'] as const,
  list: (tenantId: string, filters?: AuditLogFilters) =>
    [...auditLogKeys.lists(), tenantId, filters] as const,
  serviceList: (tenantId: string, service: AuditLogServiceName, filters?: AuditLogFilters) =>
    [...auditLogKeys.lists(), 'service', tenantId, service, filters] as const,
  stats: () => [...auditLogKeys.all, 'stats'] as const,
  stat: (tenantId: string, filters?: { start_date?: string; end_date?: string }) =>
    [...auditLogKeys.stats(), tenantId, filters] as const,
  serviceStat: (
    tenantId: string,
    service: AuditLogServiceName,
    filters?: { start_date?: string; end_date?: string }
  ) => [...auditLogKeys.stats(), 'service', tenantId, service, filters] as const,
} as const;

/**
 * Hook to fetch audit logs from a single service
 */
export function useServiceAuditLogs(
  tenantId: string,
  serviceName: AuditLogServiceName,
  filters?: AuditLogFilters,
  options?: Omit<UseQueryOptions<AuditLogListResponse, ApiError>, 'queryKey' | 'queryFn'>
) {
  return useQuery<AuditLogListResponse, ApiError>({
    queryKey: auditLogKeys.serviceList(tenantId, serviceName, filters),
    queryFn: () => auditLogsService.getServiceAuditLogs(tenantId, serviceName, filters),
    enabled: !!tenantId,
    staleTime: 30000, // 30 seconds
    ...options,
  });
}

/**
 * Hook to fetch aggregated audit logs from ALL services
 */
export function useAllAuditLogs(
  tenantId: string,
  filters?: AuditLogFilters,
  options?: Omit<UseQueryOptions<AggregatedAuditLog[], ApiError>, 'queryKey' | 'queryFn'>
) {
  return useQuery<AggregatedAuditLog[], ApiError>({
    queryKey: auditLogKeys.list(tenantId, filters),
    queryFn: () => auditLogsService.getAllAuditLogs(tenantId, filters),
    enabled: !!tenantId,
    staleTime: 30000, // 30 seconds
    ...options,
  });
}

/**
 * Hook to fetch audit log statistics from a single service
 */
export function useServiceAuditLogStats(
  tenantId: string,
  serviceName: AuditLogServiceName,
  filters?: {
    start_date?: string;
    end_date?: string;
  },
  options?: Omit<UseQueryOptions<AuditLogStatsResponse, ApiError>, 'queryKey' | 'queryFn'>
) {
  return useQuery<AuditLogStatsResponse, ApiError>({
    queryKey: auditLogKeys.serviceStat(tenantId, serviceName, filters),
    queryFn: () => auditLogsService.getServiceAuditLogStats(tenantId, serviceName, filters),
    enabled: !!tenantId,
    staleTime: 60000, // 1 minute
    ...options,
  });
}

/**
 * Hook to fetch aggregated audit log statistics from ALL services
 */
export function useAllAuditLogStats(
  tenantId: string,
  filters?: {
    start_date?: string;
    end_date?: string;
  },
  options?: Omit<UseQueryOptions<AuditLogStatsResponse, ApiError>, 'queryKey' | 'queryFn'>
) {
  return useQuery<AuditLogStatsResponse, ApiError>({
    queryKey: auditLogKeys.stat(tenantId, filters),
    queryFn: () => auditLogsService.getAllAuditLogStats(tenantId, filters),
    enabled: !!tenantId,
    staleTime: 60000, // 1 minute
    ...options,
  });
}
