import { apiClient } from '@/shared/api/client'

import type { ApiResponse, PaginatedResult } from '@/shared/types/types'

export interface AdminAuditLog {
  id: number
  actor_type: string
  actor_id: number
  actor_username: string
  actor_nickname: string
  action: string
  biz_type: string
  biz_id?: number
  ip: string
  user_agent: string
  detail?: Record<string, unknown> | null
  created_at: string
}

export interface AdminAuditQuery {
  actor_id?: string
  action?: string
  biz_type?: string
  page?: number
  page_size?: number
}

export async function fetchAdminAuditLogs(query: AdminAuditQuery = {}) {
  const params = Object.fromEntries(Object.entries(query).filter(([, value]) => value !== '' && value !== undefined))
  const { data } = await apiClient.get<ApiResponse<PaginatedResult<AdminAuditLog>>>('/admin/audit-logs', { params })
  return data.data
}
