import type { AuditLog } from './dto/audit-log'

export type AuditLogFilters = {
  employee?: string
  from?: string
  to?: string
  entityType?: string[]
  action?: string[]
  username?: string
  ip?: string
  device?: string
  page?: number
  perPage?: number
}

export type AuditLogPage = {
  items: AuditLog[]
  total: number
  page: number
  perPage: number
}

export const fetchAuditLogs = async (filters: AuditLogFilters = {}): Promise<AuditLogPage> => {
  const api = useApi()
  const params: Record<string, string | string[]> = {}

  if (filters.employee && filters.employee.trim() !== '') params.employee = filters.employee.trim()
  if (filters.from) params.from = filters.from
  if (filters.to) params.to = filters.to
  if (filters.entityType && filters.entityType.length > 0) params['entityType[]'] = filters.entityType
  if (filters.action && filters.action.length > 0) params['action[]'] = filters.action
  if (filters.username && filters.username.trim() !== '') params.username = filters.username.trim()
  if (filters.ip && filters.ip.trim() !== '') params.ip = filters.ip.trim()
  if (filters.device && filters.device.trim() !== '') params.device = filters.device.trim()
  if (filters.page) params.page = String(filters.page)
  if (filters.perPage) params.perPage = String(filters.perPage)

  return api.get<AuditLogPage>('/audit-logs', params, { toast: false })
}
