import type { Event, EventsApiError } from '../types/event'
import { config } from '@/lib/config'

const API_BASE_URL = config.api.baseUrl || 'http://lnbits'
const API_KEY = config.api.key

export async function fetchEvents(allWallets = true): Promise<Event[]> {
  try {
    const response = await fetch(
      `${API_BASE_URL}/events/api/v1/events?all_wallets=${allWallets}`,
      {
        headers: {
          'accept': 'application/json',
          'X-API-KEY': API_KEY,
        },
      }
    )

    if (!response.ok) {
      const error: EventsApiError = await response.json()
      throw new Error(error.detail[0]?.msg || 'Failed to fetch events')
    }

    return await response.json() as Event[]
  } catch (error) {
    console.error('Error fetching events:', error)
    throw error
  }
} 