/**
 * Access Logs Query Hooks
 *
 * React Query hooks for fetching access log data.
 */

import { useQuery } from '@tanstack/react-query';
import axios from 'axios';
import { queryKeys } from '../../lib/queryClient';

interface AccessLog {
  id: string;
  user_email: string;
  action: string;
  entity_type: string;
  entity_id: string;
  ip_address: string;
  createdAt: string;
}

interface AccessLogListParams {
  limit?: number;
  offset?: number;
}

interface AccessLogListResponse {
  rows: AccessLog[];
  count: number;
}

/**
 * Fetch list of access logs
 */
export function useAccessLogsQuery(params?: AccessLogListParams) {
  const query = params
    ? `?limit=${params.limit || 100}&offset=${params.offset || 0}`
    : '';

  return useQuery({
    queryKey: queryKeys.accessLogs.list(params),
    queryFn: async (): Promise<AccessLog[]> => {
      const response = await axios.get<AccessLogListResponse>(
        `access_logs${query}`,
      );
      return response.data.rows;
    },
    staleTime: 1 * 60 * 1000, // Access logs change frequently
  });
}

export default useAccessLogsQuery;
