import { get } from '@/utils/request'
import type { IResponse, IBaseResponse } from '@/types'

// 审计日志类型
export interface AuditLog {
  id: string
  user_id: string
  username?: string
  action: string
  resource: string
  resource_id?: string
  details?: string
  parsedDetails?: Record<string, any>
  ip_address?: string
  user_agent?: string
  created_at: number
}

// 查询参数
export interface AuditLogQueryParams {
  action?: string
  resource?: string
  user_id?: string
  start_date?: string
  end_date?: string
  page?: number
  pageSize?: number
}

// 统计数据
export interface AuditLogStats {
  period_days: number
  total: number
  by_action: Array<{ action: string; count: number }>
  by_resource: Array<{ resource: string; count: number }>
  by_user: Array<{ user_id: string; username: string; count: number }>
  daily: Array<{ date: string; count: number }>
}

// 获取审计日志列表
export function getAuditLogs(params?: AuditLogQueryParams): Promise<IResponse<AuditLog>> {
  return get('/audit-logs', params)
}

// 获取审计日志详情
export function getAuditLogDetail(id: string): Promise<IBaseResponse<AuditLog>> {
  return get(`/audit-logs/${id}`)
}

// 获取审计日志统计
export function getAuditLogStats(days = 7): Promise<IBaseResponse<AuditLogStats>> {
  return get('/audit-logs/stats/summary', { days })
}

// 获取用户操作历史
export function getUserAuditLogs(
  userId: string,
  params?: { page?: number; pageSize?: number }
): Promise<IResponse<AuditLog>> {
  return get(`/audit-logs/user/${userId}`, params)
}

// 获取资源操作历史
export function getResourceAuditLogs(
  resource: string,
  resourceId: string,
  params?: { page?: number; pageSize?: number }
): Promise<IResponse<AuditLog>> {
  return get(`/audit-logs/resource/${resource}/${resourceId}`, params)
}
