import { request } from '../request.js';
import type { AuditEvent, QueryAuditLogParams, PaginatedAuditEvents } from '../types.js';

/**
 * Client for the Audit Log service.
 * Covers querying the audit event list and fetching individual events.
 */
export class AuditClient {
  constructor(
    private readonly baseUrl: string,
    private readonly getToken: () => Promise<string>,
  ) {}

  /**
   * Query audit log events with optional filters and pagination.
   * Events are retained for 90 days. Requires `audit:read` scope.
   */
  async queryAuditLog(params: QueryAuditLogParams = {}): Promise<PaginatedAuditEvents> {
    const token = await this.getToken();
    return request<PaginatedAuditEvents>(this.baseUrl, {
      method: 'GET',
      path: '/api/v1/audit',
      token,
      query: {
        agentId: params.agentId,
        action: params.action,
        outcome: params.outcome,
        fromDate: params.fromDate,
        toDate: params.toDate,
        page: params.page,
        limit: params.limit,
      },
    });
  }

  /**
   * Get a single audit event by its eventId.
   * Requires `audit:read` scope.
   */
  async getAuditEvent(eventId: string): Promise<AuditEvent> {
    const token = await this.getToken();
    return request<AuditEvent>(this.baseUrl, {
      method: 'GET',
      path: `/api/v1/audit/${eventId}`,
      token,
    });
  }
}
