import { apiClient } from '../api/api-client';
import type { AuditRepository } from '@/domain/repositories/audit.repository';
import type { AuditLog } from '@/domain/entities/audit-log';

export const apiAuditRepository: AuditRepository = {
  async list(params) {
    const query = params ? '?' + new URLSearchParams(params).toString() : '';
    return apiClient<AuditLog[]>(`/api/v1/audit/logs${query}`);
  },

  async getById(id) {
    return apiClient<AuditLog>(`/api/v1/audit/logs/${id}`);
  },

  async exportLogs(params) {
    const query = params ? '?' + new URLSearchParams(params).toString() : '';
    const response = await fetch(`${process.env.NEXT_PUBLIC_API_BASE_URL || '/api/proxy'}/api/v1/audit/logs/export${query}`, {
      headers: {
        Authorization: `Bearer ${localStorage.getItem('access_token') ?? ''}`,
      },
    });

    if (!response.ok) {
      throw new Error(`Export failed: ${response.status} ${response.statusText}`);
    }

    return response.blob();
  },
};
