import { apiClient } from '@/shared/api/client';
import type { AuditEventDetail, AuditListQuery, AuditListResponse } from '@/audit/types';

export async function fetchAuditEvents(query: AuditListQuery): Promise<AuditListResponse> {
  const params: Record<string, string | number> = {};
  if (query.from) params.from = query.from;
  if (query.to) params.to = query.to;
  if (query.actor_user_id) params.actor_user_id = query.actor_user_id;
  if (query.actor_email) params.actor_email = query.actor_email;
  if (query.entity_type) params.entity_type = query.entity_type;
  if (query.entity_id) params.entity_id = query.entity_id;
  if (query.action) params.action = query.action;
  if (query.request_id) params.request_id = query.request_id;
  if (query.page != null) params.page = query.page;
  if (query.page_size != null) params.page_size = query.page_size;
  if (query.sort) params.sort = query.sort;

  return apiClient.get<AuditListResponse>('/api/v1/admin/audit', { params });
}

export async function fetchAuditEventDetail(id: number): Promise<AuditEventDetail> {
  return apiClient.get<AuditEventDetail>(`/api/v1/admin/audit/${id}`);
}
